Skip to content

Commit

Permalink
🐛 fix: fix wrong email linking in next-auth db adapter (lobehub#4919)
Browse files Browse the repository at this point in the history
* 🐛 fix: wrong email linking

* 🧪 test: nextauth account linking
  • Loading branch information
cy948 authored Dec 9, 2024
1 parent d2d86a9 commit af45a28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/database/server/models/__tests__/nextauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ describe('LobeNextAuthDbAdapter', () => {
await serverDB.query.users.findMany({ where: eq(users.email, user.email) }),
).toHaveLength(1);
});

it('should create a user if id not exist and email is null', async () => {
// In previous version, it will link the account to the existing user if the email is null
// issue: https://github.com/lobehub/lobe-chat/issues/4918
expect(nextAuthAdapter).toBeDefined();
expect(nextAuthAdapter.createUser).toBeDefined();

const existUserId = 'user-db-1';
const existUserName = 'John Doe 1';
// @ts-expect-error: createUser is defined
await nextAuthAdapter.createUser({
...user,
id: existUserId,
name: existUserName,
email: '',
});

const anotherUserId = 'user-db-2';
const anotherUserName = 'John Doe 2';
// @ts-expect-error: createUser is defined
await nextAuthAdapter.createUser({
...user,
id: anotherUserId,
name: anotherUserName,
email: '',
});
// Should create a new user if id not exists and email is null
expect(
await serverDB.query.users.findMany({ where: eq(users.id, anotherUserId) }),
).toHaveLength(1);
});
});

describe('deleteUser', () => {
Expand Down
6 changes: 3 additions & 3 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,7 @@ 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 = email.trim() ? await UserModel.findByEmail(serverDB, email) : undefined;
// If the user is not found by email, try to find by providerAccountId
if (!existingUser && providerAccountId) {
existingUser = await UserModel.findById(serverDB, providerAccountId);
Expand Down Expand Up @@ -169,7 +169,7 @@ export function LobeNextAuthDbAdapter(serverDB: NeonDatabase<typeof schema>): Ad
},

async getUserByEmail(email): Promise<AdapterUser | null> {
const lobeUser = await UserModel.findByEmail(serverDB, email);
const lobeUser = email.trim() ? await UserModel.findByEmail(serverDB, email) : undefined;
return lobeUser ? mapLobeUserToAdapterUser(lobeUser) : null;
},

Expand Down

0 comments on commit af45a28

Please sign in to comment.