debug: Add logging to trace token storage issue
This commit is contained in:
@@ -232,16 +232,23 @@ export class AuthService implements OnDestroy {
|
||||
}
|
||||
|
||||
// Validate response token
|
||||
console.log('[AUTH DEBUG] Response received:', { hasToken: !!response.accessToken, hasUser: !!response.user });
|
||||
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');
|
||||
}
|
||||
|
||||
// Validate response user data
|
||||
if (!response.user || !response.user.id) {
|
||||
console.error('[AUTH DEBUG] User validation failed:', response.user);
|
||||
throw new Error('Invalid user data in response');
|
||||
}
|
||||
|
||||
console.log('[AUTH DEBUG] About to store token...');
|
||||
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 =
|
||||
response.user.role === 'ADMIN' ? 'ADMIN' :
|
||||
|
||||
@@ -27,18 +27,20 @@ export class StorageService {
|
||||
* Get token with validation
|
||||
*/
|
||||
getToken(): string | null {
|
||||
console.log('[STORAGE DEBUG] getToken called, key:', 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
|
||||
if (token && !TokenValidator.isValidFormat(token)) {
|
||||
console.warn('Invalid token format detected, clearing...');
|
||||
console.warn('[STORAGE DEBUG] Invalid token format detected, clearing...');
|
||||
this.removeToken();
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if token is expired (with 60 second buffer for clock skew)
|
||||
if (token && TokenValidator.isExpired(token, 60)) {
|
||||
console.warn('Token expired, clearing...');
|
||||
console.warn('[STORAGE DEBUG] Token expired, clearing...');
|
||||
this.removeToken();
|
||||
return null;
|
||||
}
|
||||
@@ -50,14 +52,15 @@ export class StorageService {
|
||||
* Set token with validation
|
||||
*/
|
||||
setToken(token: string): void {
|
||||
console.log('[STORAGE DEBUG] setToken called, token length:', token?.length);
|
||||
if (!token || typeof token !== 'string') {
|
||||
console.error('Invalid token provided');
|
||||
console.error('[STORAGE DEBUG] Invalid token provided');
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate token format
|
||||
if (!TokenValidator.isValidFormat(token)) {
|
||||
console.error('Token format validation failed');
|
||||
console.error('[STORAGE DEBUG] Token format validation failed');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,7 +68,9 @@ export class StorageService {
|
||||
// Clock skew between client and server could cause valid tokens to appear expired.
|
||||
// Expiration is checked when retrieving the token instead.
|
||||
|
||||
console.log('[STORAGE DEBUG] Storing token with key:', environment.tokenStorageKey);
|
||||
this.tokenStorage.setItem(environment.tokenStorageKey, token);
|
||||
console.log('[STORAGE DEBUG] Token stored, sessionStorage contents:', Object.keys(sessionStorage));
|
||||
}
|
||||
|
||||
removeToken(): void {
|
||||
|
||||
Reference in New Issue
Block a user