Skip to content

Commit

Permalink
next-auth forward profile to session jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Nov 4, 2023
1 parent 5c154e9 commit 987742d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ const handler = NextAuth({
}
},
}
]
],
callbacks: {
async jwt({ token, account, profile }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (account && profile) {
token.id = profile.id,
token.name = profile.name_zh,
token.name_en = profile.name_en,
token.inschool = profile.inschool,
token.email = profile.email
}
return token
}
}
})

export { handler as GET, handler as POST }
Expand Down
22 changes: 22 additions & 0 deletions src/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import NextAuth, { DefaultSession } from "next-auth"

declare module "next-auth" {
interface Profile {
id: string,
inschool: boolean,
name_zh: string,
name_en: string,
email: string
}
interface Session {
user: {
id: string,
inschool: boolean,
name_zh: string,
name_en: string,
email: string
} & DefaultSession["user"]
}
}


0 comments on commit 987742d

Please sign in to comment.