@@ -4,31 +4,31 @@ import { BehaviorSubject, map, Observable, of, tap } from 'rxjs';
44import { TokenService } from './token.service' ;
55import { JwtHelperService } from '@auth0/angular-jwt' ;
66import { UserStoreService } from './user-store.service' ;
7+ import { Router } from '@angular/router' ;
78
89type Role = {
910 authority : string ;
1011} ;
1112
12-
1313@Injectable ( {
1414 providedIn : 'root' ,
1515} )
1616export class AuthService {
1717 private _userPayload : any ;
1818 private _authStatus$ ! : BehaviorSubject < boolean > ;
1919 private _logoutMessage$ = new BehaviorSubject < string | null > ( null ) ;
20- public logoutMessage$ = this . _logoutMessage$ . asObservable ( ) ;
20+ public logoutMessage$ = this . _logoutMessage$ . asObservable ( ) ;
2121
2222 constructor (
2323 private _http : HttpClient ,
2424 private _tokenService : TokenService ,
25- private _userStore : UserStoreService
25+ private _userStore : UserStoreService ,
26+ private _router : Router
2627 ) {
27- this . _authStatus$ = new BehaviorSubject < boolean > ( this . _tokenService . isLogged ( ) ) ;
28+ this . _authStatus$ = new BehaviorSubject < boolean > ( this . _tokenService . isLogged ( ) ) ;
2829 this . _userPayload = this . _decodeToken ( ) ;
2930 console . log ( 'User Payload après décode :' , this . _userPayload ) ;
3031 }
31-
3232
3333 public register$ ( email : string , password : string ) : Observable < boolean > {
3434 return this . _http . post < boolean > ( 'http://localhost:8080/auth/register' , { email, password } ) ;
@@ -57,6 +57,15 @@ public logoutMessage$ = this._logoutMessage$.asObservable();
5757 this . _authStatus$ . next ( true ) ;
5858
5959 console . log ( 'les roles' ) ;
60+ const roles = this . getRoleFromToken ( ) ;
61+
62+ if ( roles . includes ( 'ROLE_PROVIDER' ) ) {
63+ this . _router . navigate ( [ '/providers' ] ) ;
64+ } else if ( roles . includes ( 'ROLE_ADMIN' ) ) {
65+ this . _router . navigate ( [ '/products' ] ) ;
66+ } else {
67+ this . _router . navigate ( [ '/' ] ) ;
68+ }
6069 } ) ,
6170 map ( res => res . token )
6271 ) ;
@@ -87,7 +96,7 @@ public logoutMessage$ = this._logoutMessage$.asObservable();
8796 this . _tokenService . clearToken ( ) ;
8897 this . _userPayload = null ;
8998 this . _authStatus$ . next ( false ) ;
90- this . _logoutMessage$ . next ( 'Vous avez été déconnecté(e).' )
99+ this . _logoutMessage$ . next ( 'Vous avez été déconnecté(e).' ) ;
91100 }
92101
93102 private _decodeToken ( ) : unknown {
0 commit comments