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:
parent
399d76026a
commit
383b84d103
1 changed files with 3 additions and 1 deletions
4
auth.ts
4
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 }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue