|
| 1 | +import { drizzle } from 'drizzle-orm/postgres-js'; |
| 2 | +import { pgTable, serial, varchar } from 'drizzle-orm/pg-core'; |
| 3 | +import { eq } from 'drizzle-orm'; |
| 4 | +import postgres from 'postgres'; |
| 5 | +import { genSaltSync, hashSync } from 'bcrypt-ts'; |
| 6 | + |
| 7 | +// Optionally, if not using email/pass login, you can |
| 8 | +// use the Drizzle adapter for Auth.js / NextAuth |
| 9 | +// https://authjs.dev/reference/adapter/drizzle |
| 10 | +let client = postgres(`${process.env.POSTGRES_URL!}?sslmode=require`); |
| 11 | +let db = drizzle(client); |
| 12 | + |
| 13 | +let users = pgTable('User', { |
| 14 | + id: serial('id').primaryKey(), |
| 15 | + email: varchar('email', { length: 64 }), |
| 16 | + password: varchar('password', { length: 64 }), |
| 17 | +}); |
| 18 | + |
| 19 | +export async function getUser(email: string) { |
| 20 | + return await db.select().from(users).where(eq(users.email, email)); |
| 21 | +} |
| 22 | + |
| 23 | +export async function createUser(email: string, password: string) { |
| 24 | + let salt = genSaltSync(10); |
| 25 | + let hash = hashSync(password, salt); |
| 26 | + |
| 27 | + return await db.insert(users).values({ email, password: hash }); |
| 28 | +} |
0 commit comments