@@ -4,7 +4,7 @@ import prisma from './prisma';
44import { admin , createAuthMiddleware , oneTimeToken } from 'better-auth/plugins' ;
55import { CORS_ORIGIN_STRINGIFIED } from './utils/originConfig' ;
66import { getNameFromEmail } from './helpers/email' ;
7- import type { MicrosoftEntraIDProfile } from 'better-auth/social-providers' ;
7+ import type { GithubProfile , MicrosoftEntraIDProfile } from 'better-auth/social-providers' ;
88import Logger from './utils/logger' ;
99import { getIo , notify } from './socketIoServer' ;
1010import User from './models/User' ;
@@ -27,6 +27,19 @@ const getNameFromMsftProfile = (profile: MicrosoftEntraIDProfile) => {
2727 return getNameFromEmail ( profile . email || profile . preferred_username ) ;
2828} ;
2929
30+ const getNameFromGithubProfile = ( profile : GithubProfile ) => {
31+ if ( profile . name ) {
32+ const parts = profile . name . split ( ', ' ) [ 0 ] ?. split ( ' ' ) || [ ] ;
33+ if ( parts . length > 1 ) {
34+ const firstName = parts . pop ( ) ! ;
35+ const lastName = parts . join ( ' ' ) ;
36+ return { firstName, lastName } ;
37+ }
38+ }
39+ const { firstName, lastName } = getNameFromEmail ( profile . email ) ;
40+ return { firstName : firstName ?? profile . login , lastName : lastName ?? profile . login } ;
41+ } ;
42+
3043const HAS_PROVIDER_GH = ! ! process . env . BETTER_AUTH_GITHUB_ID && ! ! process . env . BETTER_AUTH_GITHUB_SECRET ;
3144const HAS_PROVIDER_MSFT = ! ! process . env . MSAL_CLIENT_ID && ! ! process . env . MSAL_CLIENT_SECRET ;
3245
@@ -60,11 +73,11 @@ export const auth = betterAuth({
6073 clientId : process . env . BETTER_AUTH_GITHUB_ID ! ,
6174 clientSecret : process . env . BETTER_AUTH_GITHUB_SECRET ! ,
6275 mapProfileToUser : ( profile ) => {
63- const [ firstName , lastName ] = profile . name . split ( ' ' ) ;
76+ const name = getNameFromGithubProfile ( profile ) ;
6477 return {
6578 ...profile ,
66- firstName : firstName || '' ,
67- lastName : lastName || ''
79+ firstName : name . firstName || '' ,
80+ lastName : name . lastName || ''
6881 } ;
6982 }
7083 }
0 commit comments