Skip to content

Commit b39965d

Browse files
committed
fix gh login without name attr
1 parent 4281f39 commit b39965d

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,11 @@ dokku letsencrypt:enable dev-teaching-api
300300
```bash
301301
# inside shell of VSCode DevContainer (with configured dokku git remote)
302302
dokku postgres:export dev-teaching-api > tdev-backup.dump
303+
psql -U postgres -h localhost -c 'drop database if exists teaching_api;'
304+
psql -U postgres -h localhost -c 'create database teaching_api;'
303305
pg_restore -h localhost --verbose --clean --no-owner --no-privileges -U postgres -d teaching_api < tdev-backup.dump
306+
yarn run prisma migrate dev
307+
304308
# when ai-pr was once merged/deployed to the db, run `delete from _prisma_migrations where migration_name ilike '%_ai_%';`
305309
```
306310

src/auth.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import prisma from './prisma';
44
import { admin, createAuthMiddleware, oneTimeToken } from 'better-auth/plugins';
55
import { CORS_ORIGIN_STRINGIFIED } from './utils/originConfig';
66
import { getNameFromEmail } from './helpers/email';
7-
import type { MicrosoftEntraIDProfile } from 'better-auth/social-providers';
7+
import type { GithubProfile, MicrosoftEntraIDProfile } from 'better-auth/social-providers';
88
import Logger from './utils/logger';
99
import { getIo, notify } from './socketIoServer';
1010
import 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+
3043
const HAS_PROVIDER_GH = !!process.env.BETTER_AUTH_GITHUB_ID && !!process.env.BETTER_AUTH_GITHUB_SECRET;
3144
const 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

Comments
 (0)