This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { redirect } from "@sveltejs/kit";
|
||||
const load = async ({ url, locals }) => {
|
||||
if (locals.user) {
|
||||
throw redirect(303, url.searchParams.get("redirectTo") ?? "/");
|
||||
}
|
||||
return {
|
||||
redirectTo: url.searchParams.get("redirectTo") ?? "",
|
||||
error: null
|
||||
};
|
||||
};
|
||||
const actions = {
|
||||
default: async ({ request, locals, url }) => {
|
||||
const formData = await request.formData();
|
||||
const email = String(formData.get("email") ?? "").trim();
|
||||
const password = String(formData.get("password") ?? "");
|
||||
const redirectTo = String(formData.get("redirectTo") ?? "").trim() || "/";
|
||||
if (!email || !password) {
|
||||
return {
|
||||
redirectTo,
|
||||
error: "Email and password are required."
|
||||
};
|
||||
}
|
||||
try {
|
||||
await locals.pb.collection("users").authWithPassword(email, password);
|
||||
} catch (err) {
|
||||
const message = err && typeof err === "object" && "message" in err ? String(err.message) : "Invalid email or password.";
|
||||
return {
|
||||
redirectTo,
|
||||
error: message
|
||||
};
|
||||
}
|
||||
throw redirect(303, redirectTo);
|
||||
}
|
||||
};
|
||||
export {
|
||||
actions,
|
||||
load
|
||||
};
|
||||
Reference in New Issue
Block a user