我使用 ngx admin 创建使用 nebular 的管理面板,我按照这个文档添加 authGard:
docs
这可以自定义登录:
docs2
我使用的代码:
@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' },
];