ehhhh
This commit is contained in:
parent
48878de844
commit
e53137abe4
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { redirect } from 'next/navigation';
|
||||||
import { Argon2id } from 'oslo/password';
|
import { Argon2id } from 'oslo/password';
|
||||||
import { lucia } from 'app/lib/lucia';
|
import { lucia } from 'app/lib/lucia';
|
||||||
import prisma from 'app/lib/db';
|
import prisma from 'app/lib/db';
|
||||||
|
import { toast } from '@/components/ui/use-toast';
|
||||||
|
|
||||||
export type User = {
|
export type User = {
|
||||||
id?: number,
|
id?: number,
|
||||||
|
@ -12,16 +13,24 @@ export type User = {
|
||||||
password: string,
|
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 {
|
try {
|
||||||
const user = await prisma.user.findFirst({
|
const user = await prisma.user.findFirst({
|
||||||
where: { email: clientUser.email },
|
where: { email: clientUser.email },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
console.log("NOT A USER")
|
||||||
// https://www.robinwieruch.de/next-forms/
|
// 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(
|
const validPassword = await new Argon2id().verify(
|
||||||
|
@ -29,8 +38,11 @@ const signIn = async (clientUser: User) => {
|
||||||
clientUser.password
|
clientUser.password
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.log(`VALID PASSWORD: ${validPassword}`)
|
||||||
|
|
||||||
if (!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');
|
throw new Error('Incorrect email or password');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +59,7 @@ const signIn = async (clientUser: User) => {
|
||||||
// https://www.robinwieruch.de/next-forms/
|
// https://www.robinwieruch.de/next-forms/
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect('/dashboard');
|
redirect('/submission');
|
||||||
};
|
};
|
||||||
|
|
||||||
export { signIn };
|
export { signIn };
|
||||||
|
|
|
@ -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)
|
|
|
@ -9,7 +9,7 @@ import { Button } from "@/components/ui/button";
|
||||||
import { signIn } from "app/api/auth/actions/sign-in";
|
import { signIn } from "app/api/auth/actions/sign-in";
|
||||||
|
|
||||||
const formSchema = z.object({
|
const formSchema = z.object({
|
||||||
email: z.string().email().min(4),
|
email: z.string().email(),
|
||||||
password: z.string().min(6)
|
password: z.string().min(6)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -17,13 +17,6 @@ export default function LoginForm() {
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
})
|
})
|
||||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
|
||||||
toast({
|
|
||||||
title: "You submitted:",
|
|
||||||
description: JSON.stringify(values)
|
|
||||||
})
|
|
||||||
signIn(values)
|
|
||||||
}
|
|
||||||
|
|
||||||
function onErrors(errors) {
|
function onErrors(errors) {
|
||||||
toast({
|
toast({
|
||||||
|
@ -34,7 +27,7 @@ export default function LoginForm() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)}>
|
<form action={signIn}>
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="email"
|
name="email"
|
||||||
|
|
Loading…
Reference in New Issue