From 53cb0d72d2072b8820503406a4b3b81fcf18b53f Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Sat, 7 Jun 2025 10:59:03 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Added=20ngx-cookie-service=20for=20?= =?UTF-8?q?cookie=20management=20and=20replaced=20the=20old=20local=20stor?= =?UTF-8?q?age=20service.=20Updated=20imports=20and=20dependencies=20in=20?= =?UTF-8?q?the=20relevant=20files.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 14 ++++ package.json | 1 + src/app/app.config.ts | 6 +- src/app/auth.guard.spec.ts | 2 +- .../login-page/login-page.component.ts | 2 +- src/app/services/auth/auth.service.ts | 2 +- src/app/services/cookies/cookies.service.ts | 74 +++++++++++++++++++ .../local-storage/local-storage.service.ts | 63 ---------------- src/app/services/pin/pin.service.ts | 6 +- 9 files changed, 96 insertions(+), 74 deletions(-) create mode 100644 src/app/services/cookies/cookies.service.ts delete mode 100644 src/app/services/local-storage/local-storage.service.ts diff --git a/package-lock.json b/package-lock.json index d24c0f0..72bfd7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "flowbite": "^2.5.2", "intro.js": "^7.2.0", "leaflet": "^1.9.4", + "ngx-cookie-service": "^19.0.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.15.0" @@ -9783,6 +9784,19 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/ngx-cookie-service": { + "version": "19.0.0", + "resolved": "https://registry.npmjs.org/ngx-cookie-service/-/ngx-cookie-service-19.0.0.tgz", + "integrity": "sha512-itxGY1BlIRoEjEtDsSsRKnJuiQteTMLKPNHrykiH06tjUQ1bi3orE7YKU1D210VBqVy1jNrB7hKuGOOIQtQJDA==", + "license": "MIT", + "dependencies": { + "tslib": "^2.8.0" + }, + "peerDependencies": { + "@angular/common": "^19.0.0", + "@angular/core": "^19.0.0" + } + }, "node_modules/node-addon-api": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-6.1.0.tgz", diff --git a/package.json b/package.json index 7b7c5bf..2a70cc6 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "flowbite": "^2.5.2", "intro.js": "^7.2.0", "leaflet": "^1.9.4", + "ngx-cookie-service": "^19.0.0", "rxjs": "~7.8.0", "tslib": "^2.3.0", "zone.js": "~0.15.0" diff --git a/src/app/app.config.ts b/src/app/app.config.ts index 2f45d0b..5edb5f9 100644 --- a/src/app/app.config.ts +++ b/src/app/app.config.ts @@ -8,6 +8,7 @@ import { import { provideRouter } from '@angular/router'; import { provideServiceWorker } from '@angular/service-worker'; import { routes } from './app.routes'; +import { CookieService } from 'ngx-cookie-service'; export const appConfig: ApplicationConfig = { providers: [ @@ -19,9 +20,6 @@ export const appConfig: ApplicationConfig = { enabled: !isDevMode(), registrationStrategy: 'registerWhenStable:30000', }), - provideServiceWorker('ngsw-worker.js', { - enabled: !isDevMode(), - registrationStrategy: 'registerWhenStable:30000', - }), + CookieService, ], }; diff --git a/src/app/auth.guard.spec.ts b/src/app/auth.guard.spec.ts index 884e432..ede04a8 100644 --- a/src/app/auth.guard.spec.ts +++ b/src/app/auth.guard.spec.ts @@ -1,7 +1,7 @@ import { TestBed } from '@angular/core/testing'; import { Router } from '@angular/router'; import { AuthGuard } from './auth.guard'; -import { LocalStorageService } from './services/local-storage/local-storage.service'; +import { LocalStorageService } from './services/cookies/cookies.service'; import { ModalService } from './services/modal/modal.service'; describe('AuthGuard', () => { diff --git a/src/app/components/login-page/login-page.component.ts b/src/app/components/login-page/login-page.component.ts index 873afcd..918317b 100644 --- a/src/app/components/login-page/login-page.component.ts +++ b/src/app/components/login-page/login-page.component.ts @@ -10,7 +10,7 @@ import { import { Router } from '@angular/router'; import { Subscription } from 'rxjs'; import { User } from '../../model/User'; -import { LocalStorageService } from '../../services/local-storage/local-storage.service'; +import { LocalStorageService } from '../../services/cookies/cookies.service'; import { ModalService } from '../../services/modal/modal.service'; import { AuthService } from '../../services/auth/auth.service'; diff --git a/src/app/services/auth/auth.service.ts b/src/app/services/auth/auth.service.ts index 63d1110..b23036a 100644 --- a/src/app/services/auth/auth.service.ts +++ b/src/app/services/auth/auth.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { BehaviorSubject, Observable, tap } from 'rxjs'; import { environment } from '../../../environment'; -import { LocalStorageService } from '../local-storage/local-storage.service'; +import { LocalStorageService } from '../cookies/cookies.service'; import { AuthResponse } from '../../model/AuthResponse'; @Injectable({ diff --git a/src/app/services/cookies/cookies.service.ts b/src/app/services/cookies/cookies.service.ts new file mode 100644 index 0000000..bd49639 --- /dev/null +++ b/src/app/services/cookies/cookies.service.ts @@ -0,0 +1,74 @@ +import { Injectable } from '@angular/core'; +import { Router } from '@angular/router'; +import { ModalService } from '../modal/modal.service'; +import { CookieService } from 'ngx-cookie-service'; + +@Injectable({ + providedIn: 'root', +}) +export class LocalStorageService { + private readonly AUTH_TOKEN_KEY = 'auth_token'; + private readonly USERNAME_KEY = 'username'; + private readonly IS_ADMIN_KEY = 'isAdmin'; + private readonly COOKIE_OPTIONS = { + path: '/', + domain: window.location.hostname, + secure: true, + sameSite: 'Strict' as const + }; + + constructor( + private router: Router, + private modalService: ModalService, + private cookieService: CookieService + ) {} + + setToken(token: string): void { + this.cookieService.set(this.AUTH_TOKEN_KEY, token, this.COOKIE_OPTIONS); + } + + setUsername(username: string): void { + this.cookieService.set(this.USERNAME_KEY, username, this.COOKIE_OPTIONS); + } + + getUsername(): string | null { + return this.cookieService.get(this.USERNAME_KEY) || null; + } + + removeUsername(): void { + this.cookieService.delete(this.USERNAME_KEY, this.COOKIE_OPTIONS.path, this.COOKIE_OPTIONS.domain); + } + + getToken(): string | null { + const token = this.cookieService.get(this.AUTH_TOKEN_KEY); + if (token) { + const payload = JSON.parse(atob(token.split('.')[1])); + const expirationDate = new Date(payload.exp * 1000); + if (expirationDate < new Date()) { + this.removeToken(); + this.router.navigate(['/']).then(() => { + this.modalService.openModal('login-modal'); + }); + return null; + } + } + console.log("Token", token); + return token || null; + } + + removeToken(): void { + this.cookieService.delete(this.AUTH_TOKEN_KEY, this.COOKIE_OPTIONS.path, this.COOKIE_OPTIONS.domain); + } + + setIsAdmin(isAdmin: boolean): void { + this.cookieService.set(this.IS_ADMIN_KEY, isAdmin.toString(), this.COOKIE_OPTIONS); + } + + getIsAdmin(): string | null { + return this.cookieService.get(this.IS_ADMIN_KEY) || null; + } + + removeIsAdmin(): void { + this.cookieService.delete(this.IS_ADMIN_KEY, this.COOKIE_OPTIONS.path, this.COOKIE_OPTIONS.domain); + } +} diff --git a/src/app/services/local-storage/local-storage.service.ts b/src/app/services/local-storage/local-storage.service.ts deleted file mode 100644 index 020bb1b..0000000 --- a/src/app/services/local-storage/local-storage.service.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Injectable } from '@angular/core'; -import { Router } from '@angular/router'; -import { ModalService } from '../modal/modal.service'; - -@Injectable({ - providedIn: 'root', -}) -export class LocalStorageService { - private readonly AUTH_TOKEN_KEY = 'auth_token'; - private readonly USERNAME_KEY = 'username'; - private readonly IS_ADMIN_KEY = 'isAdmin'; - - constructor(private router: Router, private modalService: ModalService) {} - - setToken(token: string): void { - localStorage.setItem(this.AUTH_TOKEN_KEY, token); - } - - setUsername(username: string): void { - localStorage.setItem(this.USERNAME_KEY, username); - } - - getUsername(): string | null{ - return localStorage.getItem(this.USERNAME_KEY); - } - - removeUsername(): void { - localStorage.removeItem(this.USERNAME_KEY); - } - - getToken(): string | null { - // Check if token is expired - const token = localStorage.getItem(this.AUTH_TOKEN_KEY); - if (token) { - const payload = JSON.parse(atob(token.split('.')[1])); - const expirationDate = new Date(payload.exp * 1000); - if (expirationDate < new Date()) { - this.removeToken(); // Remove expired token - this.router.navigate(['/']).then(() => { - this.modalService.openModal('login-modal'); - }); - return null; - } - } - return localStorage.getItem(this.AUTH_TOKEN_KEY); - } - - removeToken(): void { - localStorage.removeItem(this.AUTH_TOKEN_KEY); - } - - setIsAdmin(isAdmin: boolean): void { - localStorage.setItem(this.IS_ADMIN_KEY, isAdmin.toString()); - } - - getIsAdmin(): string | null { - return localStorage.getItem(this.IS_ADMIN_KEY); - } - - removeIsAdmin(): void { - localStorage.removeItem(this.IS_ADMIN_KEY); - } -} diff --git a/src/app/services/pin/pin.service.ts b/src/app/services/pin/pin.service.ts index 097b4ee..e80b671 100644 --- a/src/app/services/pin/pin.service.ts +++ b/src/app/services/pin/pin.service.ts @@ -58,10 +58,8 @@ export class PinService { getPinShares(pinId: string) { const url = `${this.apiURL}/pin/${pinId}/shares`; - const headers = new HttpHeaders({ - 'Content-Type': 'application/json', - Authorization: 'Bearer ' + localStorage.getItem('auth_token'), - }); + const headers = this.authService.getAuthHeaders(); + headers.set('Content-Type', 'application/json'); return this.http.get(url, { headers }); } }