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:
akastijn 2025-07-06 19:10:17 +02:00
parent 54e747118c
commit 04310e1cce
6 changed files with 28 additions and 43 deletions

View File

@ -1,26 +1,29 @@
import {ApplicationConfig, provideZoneChangeDetection} from '@angular/core'; import {ApplicationConfig, importProvidersFrom, provideZoneChangeDetection} from '@angular/core';
import {provideRouter} from '@angular/router'; import {provideRouter} from '@angular/router';
import {JwtHelperService, JwtModule} from '@auth0/angular-jwt'; import {JwtHelperService, JwtModule} from '@auth0/angular-jwt';
import {CookieService} from 'ngx-cookie-service'; import {CookieService} from 'ngx-cookie-service';
import {routes} from './app.routes'; import {routes} from './app.routes';
// Function to get the JWT token from cookies
export function jwtTokenGetter() { export function jwtTokenGetter() {
const cookieService = new CookieService(document, null); return localStorage.getItem('jwt');
return cookieService.check('jwt') ? cookieService.get('jwt') : null;
} }
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
provideZoneChangeDetection({eventCoalescing: true}), provideZoneChangeDetection({eventCoalescing: true}),
provideRouter(routes), provideRouter(routes),
{ provide: CookieService, useClass: CookieService }, CookieService,
{ provide: JwtHelperService, useClass: JwtHelperService }, {
{ provide: JwtModule, useValue: JwtModule.forRoot({ provide: JwtHelperService,
useValue: new JwtHelperService()
},
importProvidersFrom(
JwtModule.forRoot({
config: { config: {
tokenGetter: jwtTokenGetter tokenGetter: jwtTokenGetter
} }
})} })
)
] ]
}; };

View File

@ -6,6 +6,7 @@ import {CommonModule, NgOptimizedImage} from '@angular/common';
import {HeaderComponent} from '@header/header.component'; import {HeaderComponent} from '@header/header.component';
import {CopyIpComponent} from '@shared-components/copy-ip/copy-ip.component'; import {CopyIpComponent} from '@shared-components/copy-ip/copy-ip.component';
import {RouterLink} from '@angular/router'; import {RouterLink} from '@angular/router';
import {JwtHelperService} from '@auth0/angular-jwt';
@Component({ @Component({
standalone: true, standalone: true,
@ -16,6 +17,9 @@ import {RouterLink} from '@angular/router';
RouterLink, RouterLink,
NgOptimizedImage NgOptimizedImage
], ],
providers: [
{provide: JwtHelperService, useFactory: () => new JwtHelperService()}
],
selector: 'app-home', selector: 'app-home',
templateUrl: './home.component.html', templateUrl: './home.component.html',
styleUrl: './home.component.scss' styleUrl: './home.component.scss'

View File

@ -1,10 +1,10 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {LoginService} from '@api'; import {LoginService} from '@api';
import {CookieService} from 'ngx-cookie-service';
import {BehaviorSubject, Observable, throwError} from 'rxjs'; import {BehaviorSubject, Observable, throwError} from 'rxjs';
import {catchError, tap} from 'rxjs/operators'; import {catchError, tap} from 'rxjs/operators';
import {MatSnackBar} from '@angular/material/snack-bar'; import {MatSnackBar} from '@angular/material/snack-bar';
import {JwtHelperService} from '@auth0/angular-jwt'; import {JwtHelperService} from '@auth0/angular-jwt';
import {JwtClaims} from '@custom-types/jwt_interface'
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -18,7 +18,6 @@ export class AuthService {
constructor( constructor(
private loginService: LoginService, private loginService: LoginService,
private cookieService: CookieService,
private snackBar: MatSnackBar, private snackBar: MatSnackBar,
private jwtHelper: JwtHelperService private jwtHelper: JwtHelperService
) { ) {
@ -46,7 +45,7 @@ export class AuthService {
* Log the user out by removing the JWT * Log the user out by removing the JWT
*/ */
public logout(): void { public logout(): void {
this.cookieService.delete('jwt', '/'); localStorage.removeItem('jwt');
this.isAuthenticatedSubject.next(false); this.isAuthenticatedSubject.next(false);
this.userClaimsSubject.next(null); 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 { 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 { private saveJwt(jwt: string): void {
this.cookieService.set('jwt', jwt, { localStorage.setItem('jwt', jwt);
path: '/',
secure: true,
sameSite: 'Strict'
});
const claims = this.extractJwtClaims(jwt); const claims = this.extractJwtClaims(jwt);
this.userClaimsSubject.next(claims); this.userClaimsSubject.next(claims);
} }
/** /**

View File

@ -3,7 +3,7 @@
<form [formGroup]="loginForm"> <form [formGroup]="loginForm">
<mat-form-field appearance="fill" style="width: 100%"> <mat-form-field appearance="fill" style="width: 100%">
<mat-label>Enter your code</mat-label> <mat-label>Enter your code</mat-label>
<input matInput formControlName="code" type="text" (keydown)="onKeyDown($event)"> <input matInput formControlName="code" type="text" (keyup)="onKeyUp()">
<mat-error *ngIf="loginForm.get('code')?.errors?.['required']"> <mat-error *ngIf="loginForm.get('code')?.errors?.['required']">
Code is required Code is required
</mat-error> </mat-error>

View File

@ -65,23 +65,7 @@ export class LoginDialogComponent {
}); });
} }
onKeyDown(event: KeyboardEvent): void { onKeyUp(): void {
if (event.key.length === 1 && event.key >= 'a' && event.key <= 'z') { this.loginForm.patchValue({'code': this.loginForm.get('code')?.value.toUpperCase()})
event.preventDefault();
const input = event.target as HTMLInputElement;
const start = input.selectionStart || 0;
const end = input.selectionEnd || 0;
const value = this.loginForm.get('code')?.value || '';
const newValue =
value.substring(0, start) +
event.key.toUpperCase() +
value.substring(end);
this.loginForm.get('code')?.setValue(newValue);
setTimeout(() => {
input.setSelectionRange(start + 1, start + 1);
});
}
} }
} }

View File

@ -1,4 +1,4 @@
interface JwtClaims { export interface JwtClaims {
iss?: string; iss?: string;
sub?: string; sub?: string;
iat?: number; iat?: number;