Angular 4和laravel jwt-auth错误

时间:2017-11-15 04:35:01

标签: angular laravel-5 jwt

我的api路线

Route::group(['prefix' => 'v1', 'middleware' => ['cors','jwt.auth']], function () {
    Route::get('/test', function () {
        return ['name' => 'anil'];
    });
});

我已登录laravel jwt token generate

login(user: Object): Observable<any> {
  return this.http.post(SettingsComponent.API_ENDPOINT+'api/v1/login', user)
  .map((response: Response) => {
    let token = response.json().token;
    let userid = response.json().userid;
    console.log("Response token:" + userid);
    if (token) {
      this.token = token;
      this.userid = userid;
      localStorage.setItem('currentUser', JSON.stringify({ userid: userid, token: token }));
      return true;
    } else {
      return false;
    }
  })
  .catch((error: any) => Observable.throw(error.json().error || { message: "Server Error" }));
}

我保存本地存储令牌

如何在nexr url send get方法中使用此标记

getdata() {
    let headers = new Headers({ 'Authorization': 'Bearer ' + 
         this.authService.token });
    return this.http.get(SettingsComponent.API_ENDPOINT+'api/v1/test',headers )
      .map((response: Response) => response.json())
      .catch((error: any) => Observable.throw(error.json().error || { message: "Server Error" }));
  }

我得到的错误:

  

{“error”:“token_not_provided”}

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

将令牌分开设置在本地存储中 然后 你可以这样做:

return localStorage.getItem('token') ? JSON.parse(atob(localStorage.getItem(token'))) : null;