Skip to content

Commit

Permalink
🐛 fix: wrong email linking
Browse files Browse the repository at this point in the history
  • Loading branch information
cy948 committed Dec 7, 2024
1 parent 757a28e commit 6a39759
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/libs/next-auth/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { and, eq } from 'drizzle-orm';
import type { NeonDatabase } from 'drizzle-orm/neon-serverless';
import { Adapter, AdapterAccount } from 'next-auth/adapters';

import { UserModel } from '@/database/server/models/user';
import * as schema from '@/database/schemas';
import { UserModel } from '@/database/server/models/user';
import { merge } from '@/utils/merge';

import {
Expand Down Expand Up @@ -53,7 +53,13 @@ export function LobeNextAuthDbAdapter(serverDB: NeonDatabase<typeof schema>): Ad
async createUser(user): Promise<AdapterUser> {
const { id, name, email, emailVerified, image, providerAccountId } = user;
// return the user if it already exists
let existingUser = await UserModel.findByEmail(serverDB, email);
let existingUser;
// Only find by email if email is provided
// fix for: https://github.com/lobehub/lobe-chat/issues/4918
// side impact: no email restrictions, aka anonymous login,
// discuss at https://github.com/lobehub/lobe-chat/discussions/4844
if (email.trim().length > 0)
existingUser = await UserModel.findByEmail(serverDB, email.trim());
// If the user is not found by email, try to find by providerAccountId
if (!existingUser && providerAccountId) {
existingUser = await UserModel.findById(serverDB, providerAccountId);
Expand Down

0 comments on commit 6a39759

Please sign in to comment.