diff --git a/src/app/app.interceptors.ts b/src/app/app.interceptors.ts index 2a52735..da6ef32 100644 --- a/src/app/app.interceptors.ts +++ b/src/app/app.interceptors.ts @@ -9,6 +9,7 @@ import { Observable, throwError } from 'rxjs'; import { EnvService } from './services/env.service'; import { AuthService } from './services/authentication.service'; import { catchError } from 'rxjs/operators'; +import { v4 as uuidv4 } from 'uuid'; @Injectable() export class AuthInterceptor implements HttpInterceptor { @@ -67,6 +68,22 @@ export class TokenInterceptor implements HttpInterceptor { }); } + // for guest users, add a custom header with a unique id + if (!this.authSvc.isLoggedIn()) { + // Get (or create of needed) the guestUserID in local storage + let guestUuid = localStorage.getItem('guestUuid'); + + if (!guestUuid) { + guestUuid = uuidv4(); + localStorage.setItem('guestUuid', guestUuid); + } + request = request.clone({ + setHeaders: { + 'X-Guest-UUID': guestUuid, + }, + }); + } + return next.handle(request); } }