I use ngx admin to create admin panel that is use nebular, I follow this docs to add authGard:
docs
and this to customize login:
docs2
all the things work true and I get success message :
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:
token: {
key: 'data.access_token',
}