From 383b84d103eeb4daa4fa76385b1869677c6afb39 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Sun, 19 May 2024 22:26:43 +0200 Subject: [PATCH] Use right access token in session after refresh previously the old token was still being used in the session even though a new one has been saved in the database --- auth.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/auth.ts b/auth.ts index 2d11832..9ebd7c1 100644 --- a/auth.ts +++ b/auth.ts @@ -35,6 +35,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ const [authentikAccount] = await prisma.account.findMany({ where: { userId: user.id, provider: "authentik" }, }) + let newAccessToken; if (!authentikAccount.expires_at || !authentikAccount.refresh_token) { throw "Expiry time or refresh token not set"; @@ -57,6 +58,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ const tokens = await response.json() if (!response.ok) throw tokens + newAccessToken = tokens.access_token; await prisma.account.update({ data: { access_token: tokens.access_token, @@ -77,7 +79,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ session.error = "RefreshAccessTokenError" } } - return { ...session, access_token: authentikAccount.access_token } + return { ...session, access_token: newAccessToken ?? authentikAccount.access_token } } } });