@@ -6,24 +6,46 @@ import CredentialsProvider from 'next-auth/providers/credentials';
6
6
// Helper function to refresh the token
7
7
async function refreshAccessToken ( token : JWT ) {
8
8
try {
9
+ console . log ( 'Token before refresh attempt:' , token ) ;
10
+
11
+ // Check if accessToken exists
12
+ if ( ! token . accessToken ) {
13
+ console . error ( 'No access token available for refresh' ) ;
14
+ return {
15
+ ...token ,
16
+ error : 'RefreshAccessTokenError' ,
17
+ } ;
18
+ }
19
+
9
20
const response = await fetch (
10
21
`${ process . env . NEXT_PUBLIC_API_URL } /api/auth/refresh-token` ,
11
22
{
12
23
method : 'POST' ,
13
- headers : { 'Content-Type' : 'application/json' } ,
24
+ headers : {
25
+ 'Content-Type' : 'application/json' ,
26
+ Authorization : `Bearer ${ token . accessToken } ` ,
27
+ } ,
14
28
body : JSON . stringify ( {
15
29
token : token . accessToken ,
16
30
} ) ,
17
31
} ,
18
32
) ;
19
33
34
+ // Handle error response properly
20
35
if ( ! response . ok ) {
21
- throw new Error ( 'Failed to refresh token' ) ;
36
+ const errorData = await response . json ( ) ;
37
+ console . error ( 'Refresh token error:' , errorData ) ;
38
+ return {
39
+ ...token ,
40
+ error : 'RefreshAccessTokenError' ,
41
+ } ;
22
42
}
23
43
44
+ // Only try to read the body if we haven't read it yet
24
45
const refreshedTokens = await response . json ( ) ;
46
+ console . log ( 'Refreshed tokens response:' , refreshedTokens ) ;
25
47
26
- // Update permissions from new token if needed
48
+ // Update permissions from a new token if needed
27
49
let permissions = [ ] ;
28
50
if ( refreshedTokens . token ) {
29
51
try {
@@ -41,12 +63,11 @@ async function refreshAccessToken(token: JWT) {
41
63
accessToken : refreshedTokens . token ,
42
64
expiration : refreshedTokens . expiration ,
43
65
permissions : permissions ,
44
- roles : refreshedTokens . roles || token . roles , // Keep existing roles if not in response
66
+ roles : refreshedTokens . roles || token . roles ,
45
67
} ;
46
68
} catch ( error ) {
47
69
console . error ( 'Error refreshing token:' , error ) ;
48
70
49
- // Return the original token with an expired flag
50
71
return {
51
72
...token ,
52
73
error : 'RefreshAccessTokenError' ,
@@ -138,15 +159,25 @@ export const authOptions: NextAuthOptions = {
138
159
token . roles = user . roles ;
139
160
token . permissions = user . permissions ;
140
161
token . expiration = user . expiration ;
141
-
142
162
return token ;
143
163
}
144
164
145
165
// Return the previous token if the access token has not expired yet
146
166
if ( token . expiration && new Date ( token . expiration ) > new Date ( ) ) {
167
+ console . log ( 'Token not expired, returning existing token' ) ;
147
168
return token ;
148
169
}
149
170
171
+ if ( ! token . accessToken ) {
172
+ console . error ( 'No access token available for refresh' ) ;
173
+ return {
174
+ ...token ,
175
+ error : 'RefreshAccessTokenError' ,
176
+ } ;
177
+ }
178
+
179
+ console . log ( 'Token expired, attempting refresh' ) ;
180
+
150
181
// Access token has expired, try to refresh it
151
182
return refreshAccessToken ( token ) ;
152
183
} ,
@@ -161,7 +192,7 @@ export const authOptions: NextAuthOptions = {
161
192
// @ts -ignore
162
193
session . roles = token . roles ;
163
194
// @ts -ignore
164
- session . permissions = token . permissions ; // Add this line
195
+ session . permissions = token . permissions ;
165
196
// @ts -ignore
166
197
session . expiration = token . expiration ;
167
198
@@ -176,8 +207,6 @@ export const authOptions: NextAuthOptions = {
176
207
} ,
177
208
async signOut ( { token } : any ) {
178
209
try {
179
- // You could add server-side logout logic here if needed
180
- // For example, invalidating the token on your backend
181
210
if ( token ?. accessToken ) {
182
211
await fetch ( `${ process . env . NEXT_PUBLIC_API_URL } /api/auth/logout` , {
183
212
method : 'POST' ,
0 commit comments