const login = action(async (formData: FormData) => {
("use server");
const username = formData.get("username");
const password = formData.get("password");
if (typeof username === "string" && typeof password === "string") {
const foundUser = await db.query.user.findFirst({
where: eq(user.username, username),
});
if (!foundUser || !(await verify(foundUser.password, password)))
return new Error("Wrong username or password");
const session = await lucia.createSession(foundUser.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);
setCookie(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
// this is happening before the cookie gets set
throw redirect("/");
}
}, "login");
const login = action(async (formData: FormData) => {
("use server");
const username = formData.get("username");
const password = formData.get("password");
if (typeof username === "string" && typeof password === "string") {
const foundUser = await db.query.user.findFirst({
where: eq(user.username, username),
});
if (!foundUser || !(await verify(foundUser.password, password)))
return new Error("Wrong username or password");
const session = await lucia.createSession(foundUser.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);
setCookie(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
// this is happening before the cookie gets set
throw redirect("/");
}
}, "login");