Switch to localStorage for JWT handling and simplify case transformation logic in LoginComponent. Update app.config.ts and related services to align with the new token management method. Mark JwtClaims interface as exported.
This commit is contained in:
1 parent
54e747118c
commit
04310e1cce
6 files changed
+28
-43
No files matched your search
@@ -1,10 +1,10 @@
|
||||
import {Injectable} from '@angular/core';
|
||||
import {LoginService} from '@api';
|
||||
import {CookieService} from 'ngx-cookie-service';
|
||||
import {BehaviorSubject, Observable, throwError} from 'rxjs';
|
||||
import {catchError, tap} from 'rxjs/operators';
|
||||
import {MatSnackBar} from '@angular/material/snack-bar';
|
||||
import {JwtHelperService} from '@auth0/angular-jwt';
|
||||
import {JwtClaims} from '@custom-types/jwt_interface'
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -18,7 +18,6 @@ export class AuthService {
|
||||
|
||||
constructor(
|
||||
private loginService: LoginService,
|
||||
private cookieService: CookieService,
|
||||
private snackBar: MatSnackBar,
|
||||
private jwtHelper: JwtHelperService
|
||||
) {
|
||||
@@ -46,7 +45,7 @@ export class AuthService {
|
||||
* Log the user out by removing the JWT
|
||||
*/
|
||||
public logout(): void {
|
||||
this.cookieService.delete('jwt', '/');
|
||||
localStorage.removeItem('jwt');
|
||||
this.isAuthenticatedSubject.next(false);
|
||||
this.userClaimsSubject.next(null);
|
||||
}
|
||||
@@ -76,25 +75,20 @@ export class AuthService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the JWT from cookies
|
||||
* Get the JWT from localStorage
|
||||
*/
|
||||
public getJwt(): string | null {
|
||||
return this.cookieService.check('jwt') ? this.cookieService.get('jwt') : null;
|
||||
return localStorage.getItem('jwt');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the JWT to cookies
|
||||
* Save the JWT to localStorage
|
||||
*/
|
||||
private saveJwt(jwt: string): void {
|
||||
this.cookieService.set('jwt', jwt, {
|
||||
path: '/',
|
||||
secure: true,
|
||||
sameSite: 'Strict'
|
||||
});
|
||||
localStorage.setItem('jwt', jwt);
|
||||
|
||||
const claims = this.extractJwtClaims(jwt);
|
||||
this.userClaimsSubject.next(claims);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user