成功登录后未定义NbAuthJWTToken

时间:2019-05-27 06:00:19

标签: angular nebular ngx-admin

我正在使用具有自定义登录名的ngx-admin。使用API​​成功登录后,控件将重定向到仪表板,但未定义NbAuthJWTToken。当我刷新页面时,页面显示错误而不重新加载页面。我该如何解决?请帮我。 NbAuthJWTToken Undefined Error on refreshing page

password-strategy-options.ts [auth / strategies / password /]

token?: NbPasswordStrategyToken = {
    class: NbAuthJWTToken,
    //class: NbAuthSimpleToken,
    key: 'data.token',
    getter: (module: string, res: HttpResponse<Object>, options: NbPasswordAuthStrategyOptions) => getDeepFromObject(
      res.body,
      options.token.key,
    ),
  };

auth-guard.service.ts [在app.module.ts附近]

export class AuthGuardService implements CanActivate {
  constructor(private authService: NbAuthService, private router: Router) {  }
  canActivate() {
    return this.authService.isAuthenticated()
      .pipe(
        tap(authenticated => {
          if (!authenticated) {
            this.router.navigate(['/login']);
          }
        }),
      );
  }
}

app.module.ts

@NgModule({
  declarations: [AppComponent],
  imports: [..........
    NbAuthModule,
    NbAuthModule.forRoot({
      strategies: [
        NbPasswordAuthStrategy.setup({
          name: 'email',
          baseEndpoint: 'api/',
          login: {
            endpoint: 'login',
          },
          token: {
            class: NbAuthJWTToken,
            key: 'token',
          }
        }),
      ],
      forms: {},
    }),
  ],
  bootstrap: [AppComponent],
  providers: [
    { provide: APP_BASE_HREF, useValue: '/' },
    AuthGuardService,
  ],
})

app-routing.module.ts

const routes: Routes = [
  { 
    path: 'pages', 
    canActivate: [AuthGuardService],
    loadChildren: 'app/pages/pages.module#PagesModule' 
  },
]

1 个答案:

答案 0 :(得分:0)

在ng-admin中,成功登录意味着服务器以100 http代码响应。又名没有错误。因此,如果您发送没有令牌的空响应,它将仍然登录。我相信它在发送时找不到令牌,因为它的设置不正确。请分享您的资源。