@@ -6,7 +6,6 @@ import lnd from '@/api/lnd'
6
6
import typeDefs from '@/api/typeDefs'
7
7
import { getServerSession } from 'next-auth/next'
8
8
import { getAuthOptions } from './auth/[...nextauth]'
9
- import { decode as decodeJWT , encode as encodeJWT } from 'next-auth/jwt'
10
9
import search from '@/api/search'
11
10
import {
12
11
ApolloServerPluginLandingPageLocalDefault ,
@@ -68,8 +67,8 @@ export default startServerAndCreateNextHandler(apolloServer, {
68
67
session = { user : { ...sessionFields , apiKey : true } }
69
68
}
70
69
} else {
71
- req = await multiAuthMiddleware ( req )
72
- session = await getServerSession ( req , res , getAuthOptions ( req ) )
70
+ req = multiAuthMiddleware ( req )
71
+ session = await getServerSession ( req , res , getAuthOptions ( req , res ) )
73
72
}
74
73
return {
75
74
models,
@@ -83,15 +82,14 @@ export default startServerAndCreateNextHandler(apolloServer, {
83
82
}
84
83
} )
85
84
86
- export async function multiAuthMiddleware ( request ) {
85
+ export function multiAuthMiddleware ( request ) {
87
86
// switch next-auth session cookie with multi_auth cookie if cookie pointer present
88
87
89
88
// is there a cookie pointer?
90
89
const cookiePointerName = 'multi_auth.user-id'
91
90
const hasCookiePointer = ! ! request . cookies [ cookiePointerName ]
92
91
93
- // for development purposes, TODO REMOVE THIS
94
- const secure = process . env . NODE_ENV === 'development'
92
+ const secure = process . env . NODE_ENV === 'production'
95
93
96
94
// is there a session?
97
95
const sessionCookieName = secure ? '__Secure-next-auth.session-token' : 'next-auth.session-token'
@@ -117,30 +115,9 @@ export async function multiAuthMiddleware (request) {
117
115
118
116
if ( userJWT ) {
119
117
// use JWT found in cookie pointed to by cookie pointer
120
- // refresh JWT if possible
121
- request . cookies [ sessionCookieName ] = await refreshJWT ( userJWT )
118
+ request . cookies [ sessionCookieName ] = userJWT
122
119
return request
123
120
}
124
121
125
122
return request
126
123
}
127
-
128
- async function refreshJWT ( userJWT ) {
129
- try {
130
- const secret = process . env . NEXTAUTH_SECRET
131
- const decodedJWT = await decodeJWT ( { token : userJWT , secret } )
132
- // check if JWT is almost expired
133
- const timestampNow = Math . floor ( Date . now ( ) / 1000 )
134
- const tokenExpiry = decodedJWT . exp || 0
135
- const refreshThreshold = 60 * 60 * 24 // 24 hours
136
- if ( tokenExpiry - timestampNow < refreshThreshold ) {
137
- console . log ( 'refreshing almost expired JWT' )
138
- const refreshedJWT = await encodeJWT ( { token : decodedJWT , secret } )
139
- return refreshedJWT
140
- }
141
- return userJWT
142
- } catch ( e ) {
143
- console . error ( 'error refreshing JWT' , e )
144
- return userJWT
145
- }
146
- }
0 commit comments