【问题标题】:Set Jwt Token in Local Storage - Angular在本地存储中设置 Jwt 令牌 - Angular
【发布时间】:2017-12-25 17:01:03
【问题描述】:

我在支持HttpInterceptor 的分支v1.0 上使用auth0/angular-jwt 库。

这是我的app.module.ts

  imports: [
    BrowserModule,
    HttpClientModule,
    HttpModule,
    JwtModule.forRoot({
      config: {
        authScheme: 'Bearer',
        headerName: 'Authorization',
        tokenGetter: () => {
          return localStorage.getItem('access_token');
        },
      }
    }),


I return from my backend side:
{
    "access_token": "token - I removed it for the example)",
    "expires_in": "2017-12-25T07:26:30Z",
    "id": "19a6609f-4ed9-4804-adad-a83ss670c7ba"
}

问题是令牌没有保存在localStorage 中。我错过了什么吗?

【问题讨论】:

标签: angular angular2-jwt


【解决方案1】:

从 HTTP \ 后端服务获取令牌后,您需要将其写入本地存储,如下所示,其中result 等于来自服务器的 JSON 响应。

localStorage.setItem ('token', result.token);

我昨天刚刚在The Complete Angular Course 的课程中​​介绍了这个,值得一看,它涵盖了授权和身份验证。这是一门非常好的课程,将无止境的帮助。

【讨论】:

    猜你喜欢
    • 2020-10-25
    • 2016-12-29
    • 2018-11-12
    • 2018-11-15
    • 2019-03-24
    • 2019-09-27
    • 2022-01-15
    • 2018-04-01
    • 1970-01-01
    相关资源
    最近更新 更多