chore: move server functions to db/utils (#951)

This commit is contained in:
Jeremy 2025-04-22 22:40:01 -07:00 committed by GitHub
parent a159b77fcf
commit 24cb2ce19b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 22 additions and 105 deletions

View file

@ -1,6 +1,5 @@
import 'server-only';
import { genSaltSync, hashSync } from 'bcrypt-ts';
import {
and,
asc,
@ -28,6 +27,7 @@ import {
type Chat,
} from './schema';
import type { ArtifactKind } from '@/components/artifact';
import { generateHashedPassword } from './utils';
// Optionally, if not using email/pass login, you can
// use the Drizzle adapter for Auth.js / NextAuth
@ -47,11 +47,10 @@ export async function getUser(email: string): Promise<Array<User>> {
}
export async function createUser(email: string, password: string) {
const salt = genSaltSync(10);
const hash = hashSync(password, salt);
const hashedPassword = generateHashedPassword(password);
try {
return await db.insert(user).values({ email, password: hash });
return await db.insert(user).values({ email, password: hashedPassword });
} catch (error) {
console.error('Failed to create user in database');
throw error;

16
lib/db/utils.ts Normal file
View file

@ -0,0 +1,16 @@
import { generateId } from 'ai';
import { genSaltSync, hashSync } from 'bcrypt-ts';
export function generateHashedPassword(password: string) {
const salt = genSaltSync(10);
const hash = hashSync(password, salt);
return hash;
}
export function generateDummyPassword() {
const password = generateId(12);
const hashedPassword = generateHashedPassword(password);
return hashedPassword;
}