basic authentication function

This commit is contained in:
andrzej 2024-09-11 13:06:31 +02:00
parent 3611609474
commit 0096057ec9
1 changed files with 13 additions and 4 deletions

View File

@ -2,14 +2,23 @@ import prisma from "app/lib/db";
import bcrypt from 'bcrypt'; import bcrypt from 'bcrypt';
export type User = { export type User = {
id: number, id?: number,
email: string, email: string,
password: string, password: string,
} }
export default async function authenticate(clientUser: User) { export default async function authenticate(clientUser: User) {
const dbUser: User = await prisma.user.findFirst({ where: { email: clientUser.email } }) const dbUser: User = await prisma.user.findFirst({ where: { email: clientUser.email } })
if (!dbUser) return null if (!dbUser) return "user doesn't exist"
const passwordMatches = await
bcrypt.compare(clientUser.password, dbUser.password)
if (!passwordMatches) return "password doesn't match"
return "password matches!"
} }
let res = await authenticate({ email: "nobody", password: "nothing" })
console.log("nonexistent user: " + res)
res = await authenticate({ email: "demo@demo.demo", password: "nothing" })
console.log("existent user, bad password: " + res)
res = await authenticate({ email: "demo@demo.demo", password: "password" })
console.log("existent user, good password: " + res)