1
1
import payloadConfig from "@payload-config" ;
2
2
import NextAuth from "next-auth" ;
3
+ import type { DiscordProfile } from "next-auth/providers/discord" ;
4
+ import type { GitHubProfile } from "next-auth/providers/github" ;
3
5
import { getPayload } from "payload" ;
4
6
import { withPayload } from "payload-authjs" ;
5
7
import { nodeAuthConfig } from "./node.config" ;
@@ -11,14 +13,28 @@ export const { handlers, signIn, signOut, auth } = NextAuth(
11
13
/**
12
14
* Update user on every sign in
13
15
*/
14
- signIn : async ( { adapter, user, profile } ) => {
16
+ signIn : async ( { adapter, user, account , profile } ) => {
15
17
if ( ! user . id || ! profile ) {
16
18
return ;
17
19
}
18
20
await adapter . updateUser ! ( {
19
21
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
+ } ) ,
22
38
additionalUserDatabaseField : `Create by signIn event at ${ new Date ( ) . toISOString ( ) } ` ,
23
39
} ) ;
24
40
} ,
0 commit comments