2017-05-30 113 views
1

我是ng2的新手,尝试使用Laravel作为我的后端,并使用ng2作为前端来设置auth系统。我设法在登录用户时获得令牌,但是我用localStorage存储它的代码只能工作一次并停止工作(在我刷新它后不会再设置)。localStorage不适用于Angular

auth.service.ts:

import { Injectable } from "@angular/core"; 
import { Http, Headers, Response } from "@angular/http"; 
import { Observable } from 'rxjs'; 
import 'rxjs/Rx'; 

@Injectable() 
export class AuthService { 

constructor(private http: Http){ 

} 

signup(username: string, email: string, password: string){ 
    return this.http.post('http://localhost:8000/api/user', 
     {name: username, email:email, password:password}, 
     {headers: new Headers({'X-Requested-With': 'XMLHttpRequest'})}); 
} 

signin(email: string, password: string) { 

    return this.http.post('http://localhost:8000/api/user/signin', 
     {email: email, password: password}, 
     {headers: new Headers({'X-Requested-With': 'XMLHttpRequest'})}) 
      .map(
       (response: Response) => { 
        const token = response.json().token; 
        const base64Url = token.split('.')[1]; 
        const base64 = base64Url.replace('-','+').replace('_','/'); 
        return {token: token, decoded: JSON.parse(window.atob(base64))}; 
       } 
      ).do(

       tokenData => { 
        localStorage.setItem('token', tokenData.token); 
       } 

      ); 
} 
} 

signin.component.ts:

import { Component, OnInit } from '@angular/core'; 
import { AuthService } from '../auth.service'; 
import { NgForm } from '@angular/forms'; 


@Component({ 
    selector: 'app-signin', 
    templateUrl: './signin.component.html', 
    styleUrls: ['./signin.component.css'] 
}) 
export class SigninComponent implements OnInit { 

    constructor(private authService: AuthService) { } 

    ngOnInit() { 
    } 

    onSignin(form: NgForm){ 
    this.authService.signin(form.value.email, form.value.password) 
     .subscribe(
      tokenData => console.log(tokenData), 
      error => console.log(error) 
     ); 
    } 

} 

signin.component.html:

<form (ngSubmit)="onSignin(f)" #f="ngForm"> 
<div class="form-group"> 
    <label for="email">Email</label> 
    <input type="email" id="email" name="email" ngModel class="form-control" required> 
</div> 
<div class="form-group"> 
    <label for="password">Password</label> 
    <input type="password" id="password" name="password" ngModel class="form-control" required> 
</div> 
<button class="btn btn-primary" type="submit" [disabled]="!f.valid">Signin</button> 

+0

控制台中是否有任何错误? –

+0

@Mr_Perfect nope,我从Laravel应用程序中正确获取令牌,但它不保存到localStorage。 – Tom

回答

0

我有一个类似的应用,但我简单地使用的localStorage的如下:

signin(email: string, password: string) { 

    return this.http.post('http://localhost:8000/api/user/signin', 
     {email: email, password: password}, 
     {headers: new Headers({'X-Requested-With': 'XMLHttpRequest'})}) 
      .map(
       (response: Response) => { 
        const token = response.json().token; 
        const base64Url = token.split('.')[1]; 
        const base64 = base64Url.replace('-','+').replace('_','/'); 
        localStorage.setItem('token', token); 
        return {token: token, decoded: JSON.parse(window.atob(base64))}; 
       } 
      ); 
} 

所以显然的do(){}部则永远不会调用。

+0

这是有道理的,但我已经尝试过,它没有工作..返回块的作品,但localStorage仍然没有。当我尝试从隐身模式(chrome)运行脚本时,我看到存储在localStorage中的变量,但刷新后消失。 – Tom

+0

@Tom,隐身会话无论如何都应该这样做,我相信如果它能够在隐身模式下工作,它也应该能够在平常的情况下工作 - 所以我相信问题不在这里,而是在其他地方! – Digvijay

+0

我也在普通浏览器中运行了代码(不是隐身模式),它在刷新后仍然清除,并且在下次试用时不会再次设置。问题在哪里呢? – Tom