✨ Added ngx-cookie-service for cookie management and replaced the old local storage service. Updated imports and dependencies in the relevant files.
parent
5a000cc03c
commit
53cb0d72d2
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue