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
This commit is contained in:
GHOSCHT 2024-05-19 22:26:43 +02:00
parent 399d76026a
commit 383b84d103
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82

View file

@ -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 }
}
}
});