From e96e282d45ab721613248ad176ec5044027bcebb Mon Sep 17 00:00:00 2001 From: Valtteri Karesto Date: Fri, 19 Nov 2021 15:26:22 +0200 Subject: [PATCH] Github login --- pages/api/auth/[...nextauth].ts | 9 +++++---- types/next-auth.d.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 types/next-auth.d.ts diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index ca16b48..28d73de 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -20,10 +20,10 @@ export default NextAuth({ token: "https://github.com/login/oauth/access_token", userinfo: "https://api.github.com/user", profile(profile) { - console.log(profile) return { id: profile.id.toString(), name: profile.name || profile.login, + username: profile.login, email: profile.email, image: profile.avatar_url, } @@ -34,15 +34,16 @@ export default NextAuth({ ], callbacks: { async jwt({ token, user, account, profile, isNewUser }) { - console.log('jwt', { token, account }) if (account && account.access_token) { token.accessToken = account.access_token; + token.username = user?.username || ''; } return token }, async session({ session, token }) { - console.log('session', { token, session }) - session.accessToken = token.accessToken; + session.accessToken = token.accessToken as string; + const user = { ...session.user, username: token.username }; + session['user']['username'] = token.username as string; return session } }, diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts new file mode 100644 index 0000000..2dd79ba --- /dev/null +++ b/types/next-auth.d.ts @@ -0,0 +1,13 @@ +import NextAuth, { User } from "next-auth" + +declare module "next-auth" { + /** + * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context + */ + interface Session { + user: User & { + username: string; + } + accessToken?: string; + } +} \ No newline at end of file