basic authentication function
This commit is contained in:
parent
3611609474
commit
0096057ec9
|
@ -2,14 +2,23 @@ import prisma from "app/lib/db";
|
|||
import bcrypt from 'bcrypt';
|
||||
|
||||
export type User = {
|
||||
id: number,
|
||||
id?: number,
|
||||
email: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
export default async function authenticate(clientUser: User) {
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue