This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #211 from cosmik-network/fix/cookie-clearing-logging

Fix/cookie clearing logging

author
Wesley Finck
committer
GitHub
date (Nov 11, 2025, 11:16 AM -0500) commit f02c25de parent d3635d4b
+64 -1
+13
src/shared/infrastructure/http/services/CookieService.ts
··· 4 4 Environment, 5 5 } from '../../config/EnvironmentConfigService'; 6 6 7 + const ENABLE_AUTH_LOGGING = true; 8 + 7 9 export interface CookieOptions { 8 10 httpOnly: boolean; 9 11 secure: boolean; ··· 114 116 * Clear both access and refresh token cookies 115 117 */ 116 118 public clearTokens(res: Response): void { 119 + if (ENABLE_AUTH_LOGGING) { 120 + console.log( 121 + '[CookieService] Clearing both access and refresh token cookies', 122 + ); 123 + } 117 124 const cookieOptions = { 118 125 ...this.getBaseCookieOptions(), 119 126 maxAge: 0, // Expire immediately ··· 127 134 * Clear access token cookie only 128 135 */ 129 136 public clearAccessToken(res: Response): void { 137 + if (ENABLE_AUTH_LOGGING) { 138 + console.log('[CookieService] Clearing access token cookie'); 139 + } 130 140 const cookieOptions = { 131 141 ...this.getBaseCookieOptions(), 132 142 maxAge: 0, ··· 139 149 * Clear refresh token cookie only 140 150 */ 141 151 public clearRefreshToken(res: Response): void { 152 + if (ENABLE_AUTH_LOGGING) { 153 + console.log('[CookieService] Clearing refresh token cookie'); 154 + } 142 155 const cookieOptions = { 143 156 ...this.getBaseCookieOptions(), 144 157 maxAge: 0,
+12
src/webapp/app/api/auth/logout/route.ts
··· 1 1 import { NextRequest, NextResponse } from 'next/server'; 2 2 3 + const ENABLE_AUTH_LOGGING = true; 4 + 3 5 export async function POST(request: NextRequest) { 4 6 try { 5 7 // Proxy to backend to handle token revocation and cookie deletion ··· 28 30 console.warn( 29 31 'Backend logout failed, clearing cookies in Next.js as fallback', 30 32 ); 33 + if (ENABLE_AUTH_LOGGING) { 34 + console.log( 35 + '[auth/logout] Clearing cookies as fallback after backend failure', 36 + ); 37 + } 31 38 32 39 const response = NextResponse.json({ 33 40 success: true, ··· 44 51 console.error('Logout error:', error); 45 52 46 53 // Network error - fallback to clearing cookies in Next.js 54 + if (ENABLE_AUTH_LOGGING) { 55 + console.log( 56 + '[auth/logout] Clearing cookies as fallback after network error', 57 + ); 58 + } 47 59 const response = NextResponse.json({ 48 60 success: true, 49 61 message: 'Logged out successfully (fallback)',
+21 -1
src/webapp/app/api/auth/me/route.ts
··· 88 88 } 89 89 90 90 // For other errors, clear cookies manually 91 + if (ENABLE_AUTH_LOGGING) { 92 + console.log('[auth/me] Clearing cookies due to token refresh error'); 93 + } 91 94 const response = NextResponse.json<AuthResult>( 92 95 { isAuth: false }, 93 96 { status: 500 }, ··· 132 135 ); 133 136 } 134 137 // Clear cookies on auth failure 138 + if (ENABLE_AUTH_LOGGING) { 139 + console.log( 140 + '[auth/me] Clearing cookies due to profile fetch failure', 141 + ); 142 + } 135 143 const response = NextResponse.json<AuthResult>( 136 144 { isAuth: false }, 137 145 { status: profileResponse.status }, ··· 151 159 } catch (error) { 152 160 console.error('Profile fetch error:', error); 153 161 // Clear cookies on fetch error too 162 + if (ENABLE_AUTH_LOGGING) { 163 + console.log('[auth/me] Clearing cookies due to profile fetch error'); 164 + } 154 165 const response = NextResponse.json<AuthResult>( 155 166 { isAuth: false }, 156 167 { status: 500 }, ··· 162 173 } catch (error) { 163 174 console.error('Auth me error:', error); 164 175 refreshPromise = null; // Reset on error 165 - return NextResponse.json<AuthResult>({ isAuth: false }, { status: 500 }); 176 + if (ENABLE_AUTH_LOGGING) { 177 + console.log('[auth/me] Clearing cookies due to unexpected auth error'); 178 + } 179 + const response = NextResponse.json<AuthResult>( 180 + { isAuth: false }, 181 + { status: 500 }, 182 + ); 183 + response.cookies.delete('accessToken'); 184 + response.cookies.delete('refreshToken'); 185 + return response; 166 186 } 167 187 } 168 188
+5
src/webapp/hooks/useAuth.tsx
··· 8 8 import { verifySessionOnClient } from '@/lib/auth/dal'; 9 9 import { usePathname } from 'next/navigation'; 10 10 11 + const ENABLE_AUTH_LOGGING = true; 12 + 11 13 interface AuthContextType { 12 14 user: GetProfileResponse | null; 13 15 isAuthenticated: boolean; ··· 28 30 }; 29 31 30 32 const logout = async () => { 33 + if (ENABLE_AUTH_LOGGING) { 34 + console.log('[useAuth] Initiating logout process'); 35 + } 31 36 await ClientCookieAuthService.clearTokens(); 32 37 queryClient.clear(); 33 38 router.push('/login');
+13
src/webapp/services/auth/CookieAuthService.client.ts
··· 1 1 const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://127.0.0.1:4000'; 2 2 3 + const ENABLE_AUTH_LOGGING = true; 4 + 3 5 export class ClientCookieAuthService { 4 6 // Note: With HttpOnly cookies, we cannot read tokens from document.cookie 5 7 // The browser automatically sends cookies with requests using credentials: 'include' ··· 7 9 8 10 // Clear cookies via API (logout) 9 11 static async clearTokens(): Promise<void> { 12 + if (ENABLE_AUTH_LOGGING) { 13 + console.log( 14 + '[ClientCookieAuthService] Initiating token clearing via logout API', 15 + ); 16 + } 10 17 try { 11 18 const response = await fetch(`${appUrl}/api/auth/logout`, { 12 19 method: 'POST', ··· 17 24 console.warn( 18 25 'Logout API call failed, but continuing with client-side logout', 19 26 ); 27 + } else { 28 + if (ENABLE_AUTH_LOGGING) { 29 + console.log( 30 + '[ClientCookieAuthService] Tokens cleared successfully via logout API', 31 + ); 32 + } 20 33 } 21 34 } catch (error) { 22 35 console.error('Logout API call failed:', error);