-
Notifications
You must be signed in to change notification settings - Fork 417
Expand file tree
/
Copy pathindex.ts
More file actions
37 lines (32 loc) · 1.24 KB
/
index.ts
File metadata and controls
37 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { action, query, redirect } from "@solidjs/router";
import { getSession, passwordSignIn } from "./server";
// Define which routes require authentication
const PROTECTED_ROUTES = ["/"];
const isProtectedRoute = (path: string) =>
PROTECTED_ROUTES.some(route =>
route.endsWith("/*")
? path.startsWith(route.slice(0, -2))
: path === route || path.startsWith(route + "/")
);
export const querySession = query(async (path: string) => {
"use server";
const { data } = await getSession();
if (path === "/login" && data.id) return redirect("/");
if (data.id) return data;
if (isProtectedRoute(path)) throw redirect(`/login?redirect=${path}`);
return null;
}, "session");
export const passwdSignIn = action(async (formData: FormData) => {
"use server";
const email = formData.get("email");
const password = formData.get("password");
if (typeof email !== "string" || typeof password !== "string")
return new Error("Email and password are required");
return await passwordSignIn(email.trim().toLowerCase(), password);
});
export const logout = action(async () => {
"use server";
const session = await getSession();
await session.update({ id: undefined });
throw redirect("/login", { revalidate: "session" });
});