Skip to content

Commit abed173

Browse files
committed
refresh JWT via setMultiAuthCookies
1 parent 025c646 commit abed173

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

pages/api/auth/[...nextauth].js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function getCallbacks (req, res) {
9595
*/
9696
async jwt ({ token, user, account, profile, isNewUser }) {
9797
if (user) {
98+
console.log('user', user)
9899
// token won't have an id on it for new logins, we add it
99100
// note: token is what's kept in the jwt
100101
token.id = Number(user.id)
@@ -199,7 +200,7 @@ async function pubkeyAuth (credentials, req, res, pubkeyColumnName) {
199200
let user = await prisma.user.findUnique({ where: { [pubkeyColumnName]: pubkey } })
200201

201202
// make following code aware of cookie pointer for account switching
202-
req = await multiAuthMiddleware(req)
203+
req = multiAuthMiddleware(req)
203204
// token will be undefined if we're not logged in at all or if we switched to anon
204205
const token = await getToken({ req })
205206
if (!user) {

pages/api/graphql.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import lnd from '@/api/lnd'
66
import typeDefs from '@/api/typeDefs'
77
import { getServerSession } from 'next-auth/next'
88
import { getAuthOptions } from './auth/[...nextauth]'
9-
import { decode as decodeJWT, encode as encodeJWT } from 'next-auth/jwt'
109
import search from '@/api/search'
1110
import {
1211
ApolloServerPluginLandingPageLocalDefault,
@@ -68,8 +67,8 @@ export default startServerAndCreateNextHandler(apolloServer, {
6867
session = { user: { ...sessionFields, apiKey: true } }
6968
}
7069
} 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))
7372
}
7473
return {
7574
models,
@@ -83,15 +82,14 @@ export default startServerAndCreateNextHandler(apolloServer, {
8382
}
8483
})
8584

86-
export async function multiAuthMiddleware (request) {
85+
export function multiAuthMiddleware (request) {
8786
// switch next-auth session cookie with multi_auth cookie if cookie pointer present
8887

8988
// is there a cookie pointer?
9089
const cookiePointerName = 'multi_auth.user-id'
9190
const hasCookiePointer = !!request.cookies[cookiePointerName]
9291

93-
// for development purposes, TODO REMOVE THIS
94-
const secure = process.env.NODE_ENV === 'development'
92+
const secure = process.env.NODE_ENV === 'production'
9593

9694
// is there a session?
9795
const sessionCookieName = secure ? '__Secure-next-auth.session-token' : 'next-auth.session-token'
@@ -117,30 +115,9 @@ export async function multiAuthMiddleware (request) {
117115

118116
if (userJWT) {
119117
// 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
122119
return request
123120
}
124121

125122
return request
126123
}
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

Comments
 (0)