diff --git a/lib/nextauth.ts b/lib/nextauth.ts index d17507a..451163b 100644 --- a/lib/nextauth.ts +++ b/lib/nextauth.ts @@ -15,7 +15,7 @@ export const authOptions: NextAuthOptions = { name: profile.name, email: profile.email, image: profile.avatar_url, - username: profile.username, + username: profile.login, } as User; }, }), @@ -35,25 +35,28 @@ export const authOptions: NextAuthOptions = { 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({ + + const params = new URLSearchParams({ 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, - }), + }); + + const response = await fetch("https://github.com/login/oauth/access_token?" + params, { + headers: { "Accept": "application/json" }, method: "POST", }) - - const tokens: TokenSet = await response.json() + + const tok = await response.json() + const tokens: TokenSet = tok; 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)), + expires_at: Math.floor(Date.now() / 1000 + (tokens.expires_in as number)), refresh_token: tokens.refresh_token ?? github.refresh_token, }, where: { @@ -87,7 +90,6 @@ export const authOptions: NextAuthOptions = { username: u.username, }; } - console.log(token); return { ...token, username: token.username }; }, },