src
modules
user
application
infrastructure
webapp
···
58
58
// Extract user ID from the new access token for logging
59
59
try {
60
60
const payload = JSON.parse(
61
61
-
Buffer.from(tokenResult.value.accessToken.split('.')[1], 'base64').toString(),
61
61
+
Buffer.from(
62
62
+
tokenResult.value.accessToken.split('.')[1]!,
63
63
+
'base64',
64
64
+
).toString(),
62
65
);
63
66
const userId = payload.sub || payload.did || 'unknown';
64
67
console.log(
···
60
60
try {
61
61
const decoded = jwt.verify(token, this.jwtSecret) as { did: string };
62
62
if (ENABLE_AUTH_LOGGING) {
63
63
-
console.log(`[JwtTokenService] Token validation successful for user: ${decoded.did}`);
63
63
+
console.log(
64
64
+
`[JwtTokenService] Token validation successful for user: ${decoded.did}`,
65
65
+
);
64
66
}
65
67
return ok(decoded.did);
66
68
} catch (error: any) {
67
69
if (ENABLE_AUTH_LOGGING) {
68
68
-
console.log(`[JwtTokenService] Token validation failed: ${error.message}`);
70
70
+
console.log(
71
71
+
`[JwtTokenService] Token validation failed: ${error.message}`,
72
72
+
);
69
73
}
70
74
return ok(null); // Token is invalid or expired
71
75
}
···
35
35
if (ENABLE_AUTH_LOGGING) {
36
36
const tokenPreview = '...' + refreshToken.slice(-8);
37
37
const accessTokenStatus = !accessToken ? 'missing' : 'expiring soon';
38
38
-
38
38
+
39
39
// Try to extract user ID from access token if available
40
40
let userContext = '';
41
41
if (accessToken) {
···
47
47
// Continue without user context if token parsing fails
48
48
}
49
49
}
50
50
-
50
50
+
51
51
console.log(
52
52
`[auth/me] Access token ${accessTokenStatus}${userContext}, attempting refresh with token: ${tokenPreview}`,
53
53
);
···
107
107
try {
108
108
const payload = JSON.parse(atob(accessToken.split('.')[1]));
109
109
const userDid = payload.did || 'unknown';
110
110
-
console.log(`[auth/me] Using valid access token for user: ${userDid}`);
110
110
+
console.log(
111
111
+
`[auth/me] Using valid access token for user: ${userDid}`,
112
112
+
);
111
113
} catch {
112
114
// Continue without logging user ID if token parsing fails
113
115
}
···
125
127
126
128
if (!profileResponse.ok) {
127
129
if (ENABLE_AUTH_LOGGING) {
128
128
-
console.log(`[auth/me] Profile fetch failed with status: ${profileResponse.status}`);
130
130
+
console.log(
131
131
+
`[auth/me] Profile fetch failed with status: ${profileResponse.status}`,
132
132
+
);
129
133
}
130
134
// Clear cookies on auth failure
131
135
const response = NextResponse.json<AuthResult>(
···
139
143
140
144
const user = await profileResponse.json();
141
145
if (ENABLE_AUTH_LOGGING) {
142
142
-
console.log(`[auth/me] Profile fetched successfully for user: ${user.handle} (${user.id})`);
146
146
+
console.log(
147
147
+
`[auth/me] Profile fetched successfully for user: ${user.handle} (${user.id})`,
148
148
+
);
143
149
}
144
150
return NextResponse.json<AuthResult>({ isAuth: true, user });
145
151
} catch (error) {
···
209
215
210
216
const user = await profileResponse.json();
211
217
if (ENABLE_AUTH_LOGGING) {
212
212
-
console.log(`[auth/me] Token refresh and profile fetch successful for user: ${user.handle} (${user.id})`);
218
218
+
console.log(
219
219
+
`[auth/me] Token refresh and profile fetch successful for user: ${user.handle} (${user.id})`,
220
220
+
);
213
221
}
214
222
// Return user profile with backend's Set-Cookie headers
215
223
return new Response(JSON.stringify({ isAuth: true, user }), {
···
24
24
const expiry = payload.exp * 1000;
25
25
const bufferTime = bufferSeconds * 1000;
26
26
const isExpiring = Date.now() >= expiry - bufferTime;
27
27
-
27
27
+
28
28
if (isExpiring && ENABLE_AUTH_LOGGING) {
29
29
-
console.log(`[isTokenExpiringSoon] Token expiring soon for user: ${userDid}`);
29
29
+
console.log(
30
30
+
`[isTokenExpiringSoon] Token expiring soon for user: ${userDid}`,
31
31
+
);
30
32
}
31
31
-
33
33
+
32
34
return isExpiring;
33
35
} catch {
34
36
return true;
···
15
15
16
16
if (!accessToken || ServerCookieAuthService.isTokenExpired(accessToken)) {
17
17
if (ENABLE_AUTH_LOGGING) {
18
18
-
const reason = !accessToken ? 'No access token' : 'Access token expired';
18
18
+
const reason = !accessToken
19
19
+
? 'No access token'
20
20
+
: 'Access token expired';
19
21
console.log(`[serverAuth] Authentication failed: ${reason}`);
20
22
}
21
23
return {
···
39
41
40
42
if (!response.ok) {
41
43
if (ENABLE_AUTH_LOGGING) {
42
42
-
console.log(`[serverAuth] Profile API request failed with status: ${response.status}`);
44
44
+
console.log(
45
45
+
`[serverAuth] Profile API request failed with status: ${response.status}`,
46
46
+
);
43
47
}
44
48
return {
45
49
isAuthenticated: false,
···
50
54
51
55
const user: UserProfile = await response.json();
52
56
if (ENABLE_AUTH_LOGGING) {
53
53
-
console.log(`[serverAuth] Server-side authentication successful for user: ${user.handle} (${user.id})`);
57
57
+
console.log(
58
58
+
`[serverAuth] Server-side authentication successful for user: ${user.handle} (${user.id})`,
59
59
+
);
54
60
}
55
61
56
62
return {
···
60
66
};
61
67
} catch (error: any) {
62
68
if (ENABLE_AUTH_LOGGING) {
63
63
-
console.log(`[serverAuth] Authentication error: ${error.message || 'Unknown error'}`);
69
69
+
console.log(
70
70
+
`[serverAuth] Authentication error: ${error.message || 'Unknown error'}`,
71
71
+
);
64
72
}
65
73
return {
66
74
isAuthenticated: false,
···
25
25
if (!token) return true;
26
26
27
27
try {
28
28
-
const payload = JSON.parse(Buffer.from(token.split('.')[1], 'base64').toString());
28
28
+
const payload = JSON.parse(
29
29
+
Buffer.from(token.split('.')[1], 'base64').toString(),
30
30
+
);
29
31
const userDid = payload.did || 'unknown';
30
32
const expiry = payload.exp * 1000;
31
33
const bufferTime = bufferMinutes * 60 * 1000;