Skip to content

Commit

Permalink
🐛 fix: nullptr errors in NextAuth adapter (#4960)
Browse files Browse the repository at this point in the history
* 🐛 fix: nullptr errors in adapter

* ✅ test: pass tests
  • Loading branch information
cy948 authored Dec 10, 2024
1 parent 059810b commit d242ee0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/database/server/models/__tests__/nextauth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,39 @@ describe('LobeNextAuthDbAdapter', () => {
await serverDB.query.users.findMany({ where: eq(users.id, anotherUserId) }),
).toHaveLength(1);
});

it('should create a user if id not exist even email is invalid type', 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: Object({}), // assign a non-string value
});

const anotherUserId = 'user-db-2';
const anotherUserName = 'John Doe 2';
// @ts-expect-error: createUser is defined
await nextAuthAdapter.createUser({
...user,
id: anotherUserId,
name: anotherUserName,
// @ts-expect-error: try to assign undefined value
email: undefined,
});

// 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
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 @@ -53,7 +53,10 @@ 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 = email.trim() ? await UserModel.findByEmail(serverDB, email) : undefined;
let existingUser =
email && typeof email === 'string' && 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 +172,10 @@ export function LobeNextAuthDbAdapter(serverDB: NeonDatabase<typeof schema>): Ad
},

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

Expand Down

0 comments on commit d242ee0

Please sign in to comment.