11import payloadConfig from "@payload-config" ;
22import NextAuth from "next-auth" ;
3+ import type { DiscordProfile } from "next-auth/providers/discord" ;
4+ import type { GitHubProfile } from "next-auth/providers/github" ;
35import { getPayload } from "payload" ;
46import { withPayload } from "payload-authjs" ;
57import { nodeAuthConfig } from "./node.config" ;
@@ -11,14 +13,28 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
1113 /**
1214 * Update user on every sign in
1315 */
14- signIn : async ( { adapter, user, profile } ) => {
16+ signIn : async ( { adapter, user, account , profile } ) => {
1517 if ( ! user . id || ! profile ) {
1618 return ;
1719 }
1820 await adapter . updateUser ! ( {
1921 id : user . id ,
20- name : profile . name ?? ( profile . login as string | undefined ) ,
21- image : profile . avatar_url as string | undefined ,
22+ ...( account ?. provider === "github" && {
23+ name :
24+ ( profile as unknown as GitHubProfile ) . name ??
25+ ( profile as unknown as GitHubProfile ) . login ,
26+ email : profile . email ?? undefined ,
27+ image : ( profile as unknown as GitHubProfile ) . avatar_url ,
28+ } ) ,
29+ ...( account ?. provider === "keycloak" && {
30+ name : profile . name ,
31+ email : profile . email ?? undefined ,
32+ } ) ,
33+ ...( account ?. provider === "discord" && {
34+ name : ( profile as unknown as DiscordProfile ) . global_name ,
35+ email : profile . email ?? undefined ,
36+ image : ( profile as unknown as DiscordProfile ) . image_url ,
37+ } ) ,
2238 additionalUserDatabaseField : `Create by signIn event at ${ new Date ( ) . toISOString ( ) } ` ,
2339 } ) ;
2440 } ,
0 commit comments