add: access token refresh

This commit is contained in:
Elie Baier 2024-03-24 03:22:46 +01:00
parent 4e62e5f00b
commit 6377e46c5c
1 changed files with 53 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import { PrismaAdapter } from "@auth/prisma-adapter" import { PrismaAdapter } from "@auth/prisma-adapter"
import NextAuth, { NextAuthOptions, User } from "next-auth" import NextAuth, { NextAuthOptions, TokenSet, User } from "next-auth"
import Github from "next-auth/providers/github" import Github from "next-auth/providers/github"
import prisma from "./prisma" import prisma from "./prisma"
import { Adapter } from "next-auth/adapters" import { Adapter } from "next-auth/adapters"
@ -15,7 +15,7 @@ export const authOptions: NextAuthOptions = {
name: profile.name, name: profile.name,
email: profile.email, email: profile.email,
image: profile.avatar_url, image: profile.avatar_url,
username: profile.login, username: profile.username,
} as User; } as User;
}, },
}), }),
@ -28,15 +28,55 @@ export const authOptions: NextAuthOptions = {
strategy: "jwt", strategy: "jwt",
}, },
callbacks: { callbacks: {
session: ({ session, token }) => { session: async ({ session, token }) => {
return { const [github] = await prisma.account.findMany({
...session, where: { userId: session.user.id, provider: "github" },
user: { })
...session.user, if ((github.expires_at || 0) * 1000 < Date.now()) {
id: token.id, // If the access token has expired, try to refresh it
username: token.username, try {
}, const response = await fetch("https://github.com/login/oauth/access_token", {
}; headers: { "Content-Type": "application/json" },
body: JSON.stringify({
client_id: process.env.GITHUB_ID as string,
client_secret: process.env.GITHUB_SECRET as string,
grant_type: "refresh_token",
refresh_token: github.refresh_token as string,
}),
method: "POST",
})
const tokens: TokenSet = await response.json()
if (!response.ok) throw tokens
await prisma.account.update({
data: {
access_token: tokens.access_token,
expires_at: Math.floor(Date.now() / 1000 + (tokens.expires_at as number)),
refresh_token: tokens.refresh_token ?? github.refresh_token,
},
where: {
provider_providerAccountId: {
provider: "github",
providerAccountId: github.providerAccountId,
},
},
})
} catch (error) {
console.error("Error refreshing access token", error)
// The error property will be used client-side to handle the refresh token error
session.error = "RefreshAccessTokenError"
}
}
return {
...session,
user: {
...session.user,
id: token.id,
username: token.username,
},
}
}, },
jwt: ({ token, user }) => { jwt: ({ token, user }) => {
if (user) { if (user) {
@ -47,7 +87,8 @@ export const authOptions: NextAuthOptions = {
username: u.username, username: u.username,
}; };
} }
return token; console.log(token);
return { ...token, username: token.username };
}, },
}, },
events: { events: {