@@ -4,88 +4,88 @@ import { JWT } from "next-auth/jwt";
44import jwt from "jsonwebtoken" ;
55
66function requestRefreshAccessToken ( token : JWT ) {
7- return fetch ( `${ process . env . KEYCLOAK_ISSUER } /protocol/openid-connect/token` , {
8- headers : { "Content-Type" : "application/x-www-form-urlencoded" } ,
9- // @ts -ignore
10- body : new URLSearchParams ( {
11- client_id : process . env . KEYCLOAK_CLIENT_ID ,
12- client_secret : process . env . KEYCLOAK_CLIENT_SECRET ,
13- grant_type : "refresh_token" ,
14- refresh_token : token . refreshToken ,
15- } ) ,
16- method : "POST" ,
17- cache : "no-store" ,
18- } ) ;
7+ return fetch ( `${ process . env . KEYCLOAK_ISSUER } /protocol/openid-connect/token` , {
8+ headers : { "Content-Type" : "application/x-www-form-urlencoded" } ,
9+ // @ts -ignore
10+ body : new URLSearchParams ( {
11+ client_id : process . env . KEYCLOAK_CLIENT_ID ,
12+ client_secret : process . env . KEYCLOAK_CLIENT_SECRET ,
13+ grant_type : "refresh_token" ,
14+ refresh_token : token . refreshToken ,
15+ } ) ,
16+ method : "POST" ,
17+ cache : "no-store" ,
18+ } ) ;
1919}
2020
2121const authOptions : NextAuthOptions = {
22- secret : process . env . NEXTAUTH_SECRET ,
23- providers : [
24- KeycloakProvider ( {
25- clientId : process . env . KEYCLOAK_CLIENT_ID ?? "" ,
26- clientSecret : process . env . KEYCLOAK_CLIENT_SECRET ?? "" ,
27- issuer : process . env . KEYCLOAK_ISSUER ,
28- httpOptions : {
29- timeout : 10000 ,
30- } ,
31- } ) ,
32- ] ,
33- session : {
34- maxAge : 60 * 30 ,
35- } ,
36- callbacks : {
37- async jwt ( { token, account } ) {
38- if ( account ) {
39- token . idToken = account . id_token ;
40- token . accessToken = account . access_token ;
41- token . refreshToken = account . refresh_token ;
42- token . expiresAt = account . expires_at ;
22+ secret : process . env . NEXTAUTH_SECRET ,
23+ providers : [
24+ KeycloakProvider ( {
25+ clientId : process . env . KEYCLOAK_CLIENT_ID ?? "" ,
26+ clientSecret : process . env . KEYCLOAK_CLIENT_SECRET ?? "" ,
27+ issuer : process . env . KEYCLOAK_ISSUER ,
28+ httpOptions : {
29+ timeout : 10000 ,
30+ } ,
31+ } ) ,
32+ ] ,
33+ session : {
34+ maxAge : 60 * 30 ,
35+ } ,
36+ callbacks : {
37+ async jwt ( { token, account } ) {
38+ if ( account ) {
39+ token . idToken = account . id_token ;
40+ token . accessToken = account . access_token ;
41+ token . refreshToken = account . refresh_token ;
42+ token . expiresAt = account . expires_at ;
4343
44- if ( account . access_token ) {
45- const decodedToken = jwt . decode ( account . access_token ) ;
46- // @ts -ignore
47- token . roles = decodedToken . resource_access . account . roles ;
48- }
49- return token ;
50- }
51- // @ts -ignore
52- if ( Date . now ( ) < token . expiresAt * 1000 - 60 * 1000 ) {
53- return token ;
54- } else {
55- try {
56- const response = await requestRefreshAccessToken ( token ) ;
44+ if ( account . access_token ) {
45+ const decodedToken = jwt . decode ( account . access_token ) ;
46+ // @ts -ignore
47+ token . roles = decodedToken . resource_access . account . roles ;
48+ }
49+ return token ;
50+ }
51+ // @ts -ignore
52+ if ( Date . now ( ) < token . expiresAt * 1000 - 60 * 1000 ) {
53+ return token ;
54+ } else {
55+ try {
56+ const response = await requestRefreshAccessToken ( token ) ;
5757
58- const tokens : TokenSet = await response . json ( ) ;
58+ const tokens : TokenSet = await response . json ( ) ;
5959
60- if ( ! response . ok ) throw tokens ;
60+ if ( ! response . ok ) throw tokens ;
6161
62- const updatedToken : JWT = {
63- ...token ,
64- idToken : tokens . id_token ,
65- accessToken : tokens . access_token ,
66- expiresAt : Math . floor (
67- Date . now ( ) / 1000 + ( tokens . expires_in as number ) ,
68- ) ,
69- refreshToken : tokens . refresh_token ?? token . refreshToken ,
70- } ;
71- return updatedToken ;
72- } catch ( error ) {
73- console . error ( "Error refreshing access token" , error ) ;
74- return { ...token , error : "RefreshAccessTokenError" } ;
75- }
76- }
77- } ,
78- async session ( { session, token } ) {
79- // @ts -ignore
80- session . accessToken = token . accessToken ;
81- // @ts -ignore
82- session . refreshToken = token . refreshToken ;
83- // @ts -ignore
84- session . roles = token . roles ;
85- return session ;
86- } ,
62+ const updatedToken : JWT = {
63+ ...token ,
64+ idToken : tokens . id_token ,
65+ accessToken : tokens . access_token ,
66+ expiresAt : Math . floor (
67+ Date . now ( ) / 1000 + ( tokens . expires_in as number ) ,
68+ ) ,
69+ refreshToken : tokens . refresh_token ?? token . refreshToken ,
70+ } ;
71+ return updatedToken ;
72+ } catch ( error ) {
73+ console . error ( "Error refreshing access token" , error ) ;
74+ return { ...token , error : "RefreshAccessTokenError" } ;
75+ }
76+ }
77+ } ,
78+ async session ( { session, token } ) {
79+ // @ts -ignore
80+ session . accessToken = token . accessToken ;
81+ // @ts -ignore
82+ session . refreshToken = token . refreshToken ;
83+ // @ts -ignore
84+ session . roles = token . roles ;
85+ return session ;
8786 } ,
87+ } ,
8888} ;
8989const handler = NextAuth ( authOptions ) ;
9090
91- export { handler as GET , handler as POST } ;
91+ export { handler as GET , handler as POST } ;
0 commit comments