Skip to content
This repository was archived by the owner on Nov 28, 2025. It is now read-only.

Commit 737393c

Browse files
committed
✨ ajout de la gestion du token via un service
1 parent cee4f2d commit 737393c

File tree

6 files changed

+82
-27
lines changed

6 files changed

+82
-27
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Injectable } from '@angular/core';
2+
import {
3+
HttpEvent,
4+
HttpInterceptor,
5+
HttpHandler,
6+
HttpRequest
7+
} from '@angular/common/http';
8+
import { Observable } from 'rxjs';
9+
10+
import { AccessTokenService } from '../services/access-token.service';
11+
import { environment } from 'environments/environment';
12+
13+
@Injectable()
14+
export class AuthInterceptor implements HttpInterceptor {
15+
16+
constructor(private accessTokenService: AccessTokenService) { }
17+
18+
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
19+
const isApiUrl = request.url.startsWith(environment.apiUrl);
20+
const accessToken = this.accessTokenService.getAccessToken();
21+
22+
if (accessToken && isApiUrl) {
23+
request = request.clone({
24+
setHeaders: { Authorization: `Bearer ${accessToken}` }
25+
});
26+
}
27+
28+
return next.handle(request);
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'root'
5+
})
6+
export class AccessTokenService {
7+
constructor() { }
8+
9+
public setAccessToken(accessToken: string): void {
10+
localStorage.setItem('accessToken', accessToken);
11+
}
12+
13+
public getAccessToken(): string | null {
14+
return localStorage.getItem('accessToken');
15+
}
16+
17+
public removeAccessToken(): void {
18+
localStorage.removeItem('accessToken');
19+
}
20+
21+
public setLocalStorage(key: string, value: any): void {
22+
localStorage.setItem(key, value);
23+
}
24+
25+
payload(token: string): any {
26+
const tokenPayload = token.split('.')[1];
27+
return JSON.parse(atob(tokenPayload));
28+
}
29+
}

src/app/modules/authentication/services/auth.service.spec.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/app/modules/authentication/services/auth.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44

5-
import { User } from '@app/modules/user/interfaces/user.interface';
5+
import { AuthResponse, User } from '@app/modules/user/interfaces/user.interface';
66
import { Credentials } from '../interfaces/credentials.interface';
77
import { environment } from 'environments/environment';
88

@@ -12,8 +12,8 @@ import { environment } from 'environments/environment';
1212
export class AuthService {
1313
constructor(private http: HttpClient) { }
1414

15-
public authenticate(credentials: Credentials): Observable<User> {
16-
return this.http.post<User>(
15+
public authenticate(credentials: Credentials): Observable<AuthResponse> {
16+
return this.http.post<AuthResponse>(
1717
`${environment.apiUrl}/login`,
1818
credentials
1919
);

src/app/modules/authentication/store/auth.effects.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { catchError, map, switchMap } from 'rxjs/operators';
77
import * as AuthActions from './auth.actions';
88
import { AuthService } from '../services/auth.service';
99
import { Credentials } from '../interfaces/credentials.interface';
10-
import { User } from '@app/modules/user/interfaces/user.interface';
10+
import { AuthResponse, User } from '@app/modules/user/interfaces/user.interface';
11+
import { AccessTokenService } from '../services/access-token.service';
1112

1213
@Injectable()
1314
export class AuthEffects {
@@ -16,10 +17,10 @@ export class AuthEffects {
1617
ofType(AuthActions.authenticateAction),
1718
switchMap(({ credentials }: { credentials: Credentials }) =>
1819
this.authService.authenticate(credentials).pipe(
19-
map((user: User) => {
20-
console.log(user);
20+
map((authResponse: AuthResponse) => {
21+
this.accessTokenService.setAccessToken(authResponse.data.access_token);
2122
this.router.navigateByUrl('/dashboard');
22-
return AuthActions.fetchAuthenticateSuccessAction({ user });
23+
return AuthActions.fetchAuthenticateSuccessAction({ user: authResponse.data.user });
2324
}),
2425
catchError((error) => {
2526
return of(
@@ -56,6 +57,7 @@ export class AuthEffects {
5657
ofType(AuthActions.logoutAction),
5758
switchMap(() => this.authService.logout().pipe(
5859
map(() => {
60+
this.accessTokenService.removeAccessToken();
5961
this.router.navigateByUrl('/auth/login');
6062
return AuthActions.logoutSuccessAction();
6163
}),
@@ -67,6 +69,7 @@ export class AuthEffects {
6769
constructor(
6870
private actions$: Actions,
6971
private authService: AuthService,
72+
private accessTokenService: AccessTokenService,
7073
private router: Router
7174
) {}
7275
}

src/app/modules/user/interfaces/user.interface.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
export interface AuthResponse {
2-
user: User;
3-
access_token: string;
4-
token_type: string;
5-
expires_at: Date;
2+
message: string;
3+
data: {
4+
user: User;
5+
access_token: string;
6+
token_type: string;
7+
expires_at: FromDate;
8+
}
9+
}
10+
11+
export interface FromDate {
12+
date: Date;
13+
timezone_type: number;
14+
timezone: string;
615
}
716

817
export interface User {

0 commit comments

Comments
 (0)