This commit is contained in:
andrzej 2024-09-12 17:45:18 +02:00
parent 48878de844
commit e53137abe4
4 changed files with 37 additions and 38 deletions

View File

@ -0,0 +1,18 @@
import prisma from "app/lib/db";
import bcrypt from 'bcrypt';
export type User = {
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 false
const passwordMatches = await
bcrypt.compare(clientUser.password, dbUser.password)
if (!passwordMatches) return false
return true
}

View File

@ -5,6 +5,7 @@ import { redirect } from 'next/navigation';
import { Argon2id } from 'oslo/password';
import { lucia } from 'app/lib/lucia';
import prisma from 'app/lib/db';
import { toast } from '@/components/ui/use-toast';
export type User = {
id?: number,
@ -12,16 +13,24 @@ export type User = {
password: string,
}
const signIn = async (clientUser: User) => {
const signIn = async (formData: FormData) => {
console.log("signIn called!")
const clientUser: User = {
email: formData.get("email") as string,
password: formData.get("password") as string
}
try {
const user = await prisma.user.findFirst({
where: { email: clientUser.email },
});
if (!user) {
console.log("NOT A USER")
// https://www.robinwieruch.de/next-forms/
throw new Error('Incorrect email or password');
toast({
title: "Whoops!",
description: "Username not recognised!"
})
}
const validPassword = await new Argon2id().verify(
@ -29,8 +38,11 @@ const signIn = async (clientUser: User) => {
clientUser.password
);
console.log(`VALID PASSWORD: ${validPassword}`)
if (!validPassword) {
// https://www.robinwieruch.de/next-forms/
// https://www.robinwieruch.de/next-forms
console.log("NOT A VALID PASSWORD!")
throw new Error('Incorrect email or password');
}
@ -47,7 +59,7 @@ const signIn = async (clientUser: User) => {
// https://www.robinwieruch.de/next-forms/
}
redirect('/dashboard');
redirect('/submission');
};
export { signIn };

View File

@ -1,24 +0,0 @@
import prisma from "app/lib/db";
import bcrypt from 'bcrypt';
export type User = {
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 "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)

View File

@ -9,7 +9,7 @@ import { Button } from "@/components/ui/button";
import { signIn } from "app/api/auth/actions/sign-in";
const formSchema = z.object({
email: z.string().email().min(4),
email: z.string().email(),
password: z.string().min(6)
})
@ -17,13 +17,6 @@ export default function LoginForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
})
function onSubmit(values: z.infer<typeof formSchema>) {
toast({
title: "You submitted:",
description: JSON.stringify(values)
})
signIn(values)
}
function onErrors(errors) {
toast({
@ -34,7 +27,7 @@ export default function LoginForm() {
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit, onErrors)}>
<form action={signIn}>
<FormField
control={form.control}
name="email"