debug: Add logging to trace token storage issue

This commit is contained in:
Mahi
2026-02-09 14:52:27 -04:00
parent 24d5625d17
commit df5210a3c8
2 changed files with 16 additions and 4 deletions

View File

@@ -232,16 +232,23 @@ export class AuthService implements OnDestroy {
} }
// Validate response token // Validate response token
console.log('[AUTH DEBUG] Response received:', { hasToken: !!response.accessToken, hasUser: !!response.user });
if (!response.accessToken || !TokenValidator.isValidFormat(response.accessToken)) { if (!response.accessToken || !TokenValidator.isValidFormat(response.accessToken)) {
console.error('[AUTH DEBUG] Token validation failed:', response.accessToken?.substring(0, 50));
throw new Error('Invalid authentication response'); throw new Error('Invalid authentication response');
} }
// Validate response user data // Validate response user data
if (!response.user || !response.user.id) { if (!response.user || !response.user.id) {
console.error('[AUTH DEBUG] User validation failed:', response.user);
throw new Error('Invalid user data in response'); throw new Error('Invalid user data in response');
} }
console.log('[AUTH DEBUG] About to store token...');
this.storage.setToken(response.accessToken); this.storage.setToken(response.accessToken);
console.log('[AUTH DEBUG] Token stored, verifying...');
const storedToken = this.storage.getToken();
console.log('[AUTH DEBUG] Token retrieved:', storedToken ? 'EXISTS' : 'NULL');
const userType: UserType = const userType: UserType =
response.user.role === 'ADMIN' ? 'ADMIN' : response.user.role === 'ADMIN' ? 'ADMIN' :

View File

@@ -27,18 +27,20 @@ export class StorageService {
* Get token with validation * Get token with validation
*/ */
getToken(): string | null { getToken(): string | null {
console.log('[STORAGE DEBUG] getToken called, key:', environment.tokenStorageKey);
const token = this.tokenStorage.getItem(environment.tokenStorageKey); const token = this.tokenStorage.getItem(environment.tokenStorageKey);
console.log('[STORAGE DEBUG] Raw token from storage:', token ? `${token.substring(0, 30)}...` : 'NULL');
// Validate token format before returning // Validate token format before returning
if (token && !TokenValidator.isValidFormat(token)) { if (token && !TokenValidator.isValidFormat(token)) {
console.warn('Invalid token format detected, clearing...'); console.warn('[STORAGE DEBUG] Invalid token format detected, clearing...');
this.removeToken(); this.removeToken();
return null; return null;
} }
// Check if token is expired (with 60 second buffer for clock skew) // Check if token is expired (with 60 second buffer for clock skew)
if (token && TokenValidator.isExpired(token, 60)) { if (token && TokenValidator.isExpired(token, 60)) {
console.warn('Token expired, clearing...'); console.warn('[STORAGE DEBUG] Token expired, clearing...');
this.removeToken(); this.removeToken();
return null; return null;
} }
@@ -50,14 +52,15 @@ export class StorageService {
* Set token with validation * Set token with validation
*/ */
setToken(token: string): void { setToken(token: string): void {
console.log('[STORAGE DEBUG] setToken called, token length:', token?.length);
if (!token || typeof token !== 'string') { if (!token || typeof token !== 'string') {
console.error('Invalid token provided'); console.error('[STORAGE DEBUG] Invalid token provided');
return; return;
} }
// Validate token format // Validate token format
if (!TokenValidator.isValidFormat(token)) { if (!TokenValidator.isValidFormat(token)) {
console.error('Token format validation failed'); console.error('[STORAGE DEBUG] Token format validation failed');
return; return;
} }
@@ -65,7 +68,9 @@ export class StorageService {
// Clock skew between client and server could cause valid tokens to appear expired. // Clock skew between client and server could cause valid tokens to appear expired.
// Expiration is checked when retrieving the token instead. // Expiration is checked when retrieving the token instead.
console.log('[STORAGE DEBUG] Storing token with key:', environment.tokenStorageKey);
this.tokenStorage.setItem(environment.tokenStorageKey, token); this.tokenStorage.setItem(environment.tokenStorageKey, token);
console.log('[STORAGE DEBUG] Token stored, sessionStorage contents:', Object.keys(sessionStorage));
} }
removeToken(): void { removeToken(): void {