Estou criando autenticação com o angular e estou com problemas de permissão. Já verifiquei pelo backend e consigo acessar perfeitamente após a realização do login. Já no front recebo: ' GET http://localhost:4200/licencas 403 (Forbidden)'. No backend estou usando spring security com token jwt.
minhas rotas:
{
path: 'login',
component: LoginFormComponent,
},
{
path: 'home',
component: HomeComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'licencas',
component: ListLicencasComponent,
canActivate: [ AuthGuardService ],
},
{
path: 'nova-licenca',
component: CrudLicencasComponent,
canActivate: [ AuthGuardService ],
},
{
path: 'editar-licenca/:id',
component: CrudLicencasComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'excluir-licenca/:id',
component: CrudLicencasComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'licencas/monitor',
component: MonitorPageComponent,
canActivate: [ AuthGuardService ],
},
{
path: 'licencas/enviarEmailNotificacao/:id',
component: MonitorComponentComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'usuarios',
component: ListUsuariosComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'novo-usuario',
component: CrudUsuariosComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'editar-usuario/:id',
component: CrudUsuariosComponent,
canActivate: [ AuthGuardService ]
},
{
path: 'excluir-usuario/:id',
component: CrudUsuariosComponent,
canActivate: [ AuthGuardService ]
},
{
path: '**',
redirectTo: 'login'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true }), DxDataGridModule, DxFormModule],
providers: [AuthGuardService],
exports: [RouterModule],
declarations: []
})
export class AppRoutingModule { }
authservice:
export class AuthService {
private readonly API = 'login';
private currentUser: Usuarios | null = null;
constructor(private http: HttpClient, private router: Router) { }
login(email: string, senha: string): Observable<Usuarios> {
return this.http.post<Usuarios>(`${this.API}`, { email: email, senha: senha }).pipe(
tap(response => {
this.setCurrentUser(response);
localStorage.setItem('token', response.token);
})
);
}
getRole() {
const token = localStorage.getItem('token');
if (token) {
const tokenPayload = JSON.parse(atob(token.split('.')[1]));
console.log('Role: ', tokenPayload);
return tokenPayload.roles;
}
return null;
}
async logOut() {
this.currentUser = null;
localStorage.removeItem('token');
this.router.navigate(['/login']);
}
setCurrentUser(user: Usuarios): void {
this.currentUser = user;
}
getCurrentUser(): Usuarios | null {
return this.currentUser;
}
isLoggedIn(): boolean {
return !!this.getCurrentUser();
}
isAdmin(): boolean {
const userRoles = this.getRole();
const isAdmin = !!((userRoles && (userRoles.includes('ROLE_ADMIN'.trim()) || userRoles.includes('ROLE_USER'.trim()))) ?? false);
return isAdmin;
}
}
guard:
export class AuthGuardService implements CanActivate {
constructor(private router: Router, private authService: AuthService) {}
canActivate(): boolean {
if (this.authService.isLoggedIn() && this.authService.isAdmin()) {
return true;
} else {
this.router.navigate(['/login']);
return false;
}
}
}
e por fim meu login component:
loading = false;
formData: any = {};
constructor(private authService: AuthService, private router: Router) { }
async onSubmit(e: Event) {
e.preventDefault();
const { email, senha } = this.formData;
this.loading = true;
try {
const user = await this.authService.login(email, senha).toPromise();
if (user && user.token) {
notify('Login efetuado com sucesso', 'success', 2000);
this.router.navigate(['/home']);
} else {
notify('Credenciais inválidas', 'error', 4000);
}
} catch (error) {
notify('Erro ao processar a solicitação', 'error', 4000);
} finally {
this.loading = false;
}
}
}
Não consegui encontrar onde esta meu problema.