简体   繁体   English

AuthGuard 在星云中总是错误的

[英]AuthGuard always false in nebular

I use ngx admin to create admin panel that is use nebular, I follow this docs to add authGard:我使用 ngx admin 创建使用 nebular 的管理面板,我按照此文档添加 authGard:
docs 文档

and this to customize login:这是自定义登录:
docs2 文档2

all the things work true and I get success message :一切正常,我收到成功消息: 图片

but I get false in autogard:但我在 autogard 中得到错误:
图像2

Code that I use:我使用的代码:

 @NgModule({ declarations: [ AppComponent, ], providers: [ AuthGuard, ], imports: [ *** ], bootstrap: [AppComponent], }) export class AppModule { } //********** @Injectable() export class AuthGuard implements CanActivate { constructor(private authService: NbAuthService, private router: Router) { } canActivate() { console.log(this.authService.isAuthenticated()); return this.authService.isAuthenticated() .pipe( tap(authenticated => { if (!authenticated) { console.log(authenticated); this.router.navigate(['auth/login']); } }), ); } } //********** const routes: Routes = [ { path: 'pages', canActivate: [AuthGuard], loadChildren: () => import('./pages/pages.module') .then(m => m.PagesModule), }, { path: 'auth', loadChildren: () => import('./auth/auth.module').then(m => m.NgxAuthModule), }, { path: '', redirectTo: 'pages', pathMatch: 'full' }, { path: '**', redirectTo: 'pages' }, ];

Problem solved.问题解决了。
Document said that response should be like this:文档说响应应该是这样的:

{
data: {
 token: 'some-jwt-token'
  } 
}

my response is:我的回答是:

{
data: {
  access_token: 'some-jwt-token'
  }
}

And in document write that we can change (token) to other things like this:在文档中,我们可以将(令牌)更改为其他类似的内容:

 token: {
           key: 'access_token', // this parameter tells where to look for the token
         }

But it's not true and we should use this in NbAuthModule.forRoot:但事实并非如此,我们应该在 NbAuthModule.forRoot 中使用它:

 token: {
           key: 'data.access_token',
         }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM