is name field is required in schema as i want to use first_name and last_name instead of name field but it throw error. #12221
Unanswered
mussaddiqmahmood7
asked this question in
Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working with Drizzle ORM in a Next.js project, where I'm setting up authentication using NextAuth with custom fields for users. I want to use first_name and last_name fields instead of a single name field in the users table schema, but I’m encountering issues where it seems that the name field is still expected, causing errors.
Here's a simplified version of my code setup for reference:
import { db } from '
/server/db';/server/db/schema';import { users, accounts, sessions } from '
import { type UserType } from '~/types/user';
import bcrypt from 'bcrypt';
import { eq } from 'drizzle-orm';
import { type NextAuthOptions } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import Google, { type GoogleProfile } from 'next-auth/providers/google';
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import { type Adapter } from 'next-auth/adapters';
export const authOptions: NextAuthOptions = {
adapter: DrizzleAdapter(db, { usersTable: users, accountsTable: accounts, sessionsTable: sessions }) as Adapter,
providers: [
Credentials({
id: 'credentials',
name: 'credentials',
credentials: { email: { label: 'Email', type: 'email' }, password: { label: 'Password', type: 'password' }},
async authorize(_credentials): Promise<UserType | null> {
const user = await db.query.users.findFirst({
where: eq(users.email, _credentials.email)
});
if (user && await bcrypt.compare(_credentials.password, user.password)) {
return user;
}
throw new Error('Invalid credentials');
}
}),
Google({
clientId: process.env.AUTH_GOOGLE_ID ?? '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET ?? '',
profile: (profile: GoogleProfile) => ({
id: profile.sub,
first_name: profile.given_name,
last_name: profile.family_name,
email: profile.email,
image: profile.picture,
auth_type: 'oauth',
})
})
],
session: { strategy: 'jwt' },
secret: process.env.NEXTAUTH_SECRET
};
The users schema in Drizzle ORM is defined as follows, where I’ve intentionally excluded name:
export const users = createTable('user', {
id: varchar('id', { length: 255 }).primaryKey(),
first_name: varchar('first_name', { length: 255 }),
last_name: varchar('last_name', { length: 255 }),
password: varchar('password', { length: 255 }),
email: varchar('email', { length: 255 }),
role: varchar('role', { length: 255 }).default('user').notNull(),
auth_type: varchar('auth_type', { length: 20 }),
image: varchar('image', { length: 255 }),
created_at: timestamp('created_at').defaultNow().notNull(),
updated_at: timestamp('updated_at').defaultNow().notNull(),
});
Has anyone else worked with Drizzle ORM and NextAuth in a similar way, without the name field? If so, how did you handle it to prevent these errors? Any guidance on configuring NextAuth to avoid requiring name would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions