-
Notifications
You must be signed in to change notification settings - Fork 143
Fix Unknown User #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Fix Unknown User #122
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import { serverEnv } from '@/env/server' | ||
| import { prisma } from '@/lib/prisma' | ||
| import { getUserDisplayName } from '@/lib/user' | ||
| import { PrismaAdapter } from '@next-auth/prisma-adapter' | ||
| import { Role } from '@prisma/client' | ||
| import type { NextAuthOptions } from 'next-auth' | ||
|
|
@@ -81,6 +82,26 @@ export const authOptions: NextAuthOptions = { | |
| return false | ||
| } | ||
|
|
||
| if (!user.name?.trim()) { | ||
| const profileWithName = profile as | ||
| | { name?: string; login?: string; preferred_username?: string } | ||
| | undefined | ||
| const fallbackName = getUserDisplayName({ | ||
| name: | ||
| profileWithName?.name ?? | ||
| profileWithName?.preferred_username ?? | ||
| profileWithName?.login, | ||
| email: user.email, | ||
| }) | ||
|
|
||
| if (user.id) { | ||
| await prisma.user.update({ | ||
| where: { id: user.id }, | ||
| data: { name: fallbackName }, | ||
| }) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unhandled DB error in signIn blocks authenticationMedium Severity The Reviewed by Cursor Bugbot for commit 5f48759. Configure here. |
||
| } | ||
|
|
||
| return true | ||
| }, | ||
| async session({ session, user }) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| type UserDisplayNameInput = { | ||
| name?: string | null | ||
| email?: string | null | ||
| } | ||
|
|
||
| export function getUserDisplayName({ | ||
| name, | ||
| email, | ||
| }: UserDisplayNameInput): string { | ||
| const trimmedName = name?.trim() | ||
|
|
||
| if (trimmedName) { | ||
| return trimmedName | ||
| } | ||
|
|
||
| const emailPrefix = email?.split('@')[0]?.trim() | ||
|
|
||
| if (emailPrefix) { | ||
| return emailPrefix | ||
| } | ||
|
|
||
| return 'Beam user' | ||
| } |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nullish coalescing skips fallback for empty string names
Medium Severity
The
??(nullish coalescing) operator only falls through onnull/undefined, not on empty strings. IfprofileWithName?.nameis""(which some OAuth providers return instead ofnullfor unset names), thepreferred_usernameandloginfallbacks are skipped entirely. The||operator would correctly fall through for empty strings, preserving the intended fallback chain of name → preferred_username → login.Reviewed by Cursor Bugbot for commit 5f48759. Configure here.