javascript - AuthGuard 在星云中总是 false

标签 javascript angular nebular ngx-admin

我使用 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/

相关文章:

angular - NbChat 组件在控制台中不显示和 NullInjectorError,不知道如何修复

javascript - jquery : how to jump to position without scrolling

javascript - Angular 6 - 单击外部菜单

javascript - Backbone 集合 url 未定义

加载第一个子路由后,Angular2 路由不起作用

angular - 在 angular2 中使用 ng2-charts 的水平条形图

ngx-admin - Nebular 以编程方式关闭带有表单的弹出窗口

angular - 从 Nebular NbTreeGridComponent 更改列的宽度

javascript - 我想使用 JS onclick 方法从同一文档获取 url

javascript - 如何使用 Greasemonkey 设计 XML 片段的样式?