first commit
This commit is contained in:
47
auth/components/signin/action.ts
Normal file
47
auth/components/signin/action.ts
Normal file
@ -0,0 +1,47 @@
|
||||
"use server";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { Argon2id } from "oslo/password";
|
||||
import { lucia } from "@/lib/lucia";
|
||||
import { lachesis } from "@/lib/prisma";
|
||||
|
||||
const signIn = async (formData: FormData) => {
|
||||
const formDataRaw = {
|
||||
username: formData.get("username") as string,
|
||||
password: formData.get("password") as string,
|
||||
};
|
||||
|
||||
try {
|
||||
const user = await lachesis.user.findUnique({
|
||||
where: { username: formDataRaw.username },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return { error: "Incorrect username" };
|
||||
}
|
||||
|
||||
const validPassword = await new Argon2id().verify(
|
||||
user.hashedPassword,
|
||||
formDataRaw.password
|
||||
);
|
||||
|
||||
if (!validPassword) {
|
||||
return { error: "Incorrect password" };
|
||||
}
|
||||
|
||||
const session = await lucia.createSession(user.id, {});
|
||||
const sessionCookie = lucia.createSessionCookie(session.id);
|
||||
|
||||
cookies().set(
|
||||
sessionCookie.name,
|
||||
sessionCookie.value,
|
||||
sessionCookie.attributes
|
||||
);
|
||||
} catch (error: any) {
|
||||
return { error: "Sign-in failed: " + error.message };
|
||||
}
|
||||
redirect("/home");
|
||||
};
|
||||
|
||||
export { signIn };
|
Reference in New Issue
Block a user