gpt4 book ai didi

javascript - AuthGuard 在星云中总是 false

转载 作者:行者123 更新时间:2023-12-02 21:23:51 25 4
gpt4 key购买 nike

我使用 ngx admin 创建使用 nebular 的管理面板,我按照此文档添加 authGard:
docs

这用于自定义登录:
docs2

所有事情都正常,我收到成功消息: img

但我在 autogard 中得到错误:
img2

我使用的代码:

@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' },
];

最佳答案

问题已解决。
文档说响应应该是这样的:

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

我的回复是:

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

并在文档中写入我们可以将( token )更改为其他内容,如下所示:

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

但这不是真的,我们应该在 NbAuthModule.forRoot 中使用它:

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

关于javascript - AuthGuard 在星云中总是 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60786419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com