add: access token refresh
This commit is contained in:
parent
4e62e5f00b
commit
6377e46c5c
|
@ -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,7 +28,47 @@ export const authOptions: NextAuthOptions = {
|
||||||
strategy: "jwt",
|
strategy: "jwt",
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
session: ({ session, token }) => {
|
session: async ({ session, token }) => {
|
||||||
|
const [github] = await prisma.account.findMany({
|
||||||
|
where: { userId: session.user.id, provider: "github" },
|
||||||
|
})
|
||||||
|
if ((github.expires_at || 0) * 1000 < Date.now()) {
|
||||||
|
// If the access token has expired, try to refresh it
|
||||||
|
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 {
|
return {
|
||||||
...session,
|
...session,
|
||||||
user: {
|
user: {
|
||||||
|
@ -36,7 +76,7 @@ export const authOptions: NextAuthOptions = {
|
||||||
id: token.id,
|
id: token.id,
|
||||||
username: token.username,
|
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: {
|
||||||
|
|
Loading…
Reference in New Issue