-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.ts
39 lines (30 loc) · 973 Bytes
/
actions.ts
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
38
39
"use server";
import { getIronSession } from "iron-session";
import { sessionOptions, SessionData, defaultSession } from "./lib";
import { cookies } from "next/headers";
import { revalidatePath } from "next/cache";
//data
let password = process.env.ACCESS_CODE!;
export const getSession = async () => {
const session = await getIronSession<SessionData>(cookies(), sessionOptions);
if (!session.isLoggiedIn) {
session.isLoggiedIn = defaultSession.isLoggiedIn;
}
return session;
};
export const login = async (
prevState: { error: undefined | string },
formData: FormData
) => {
const session = await getSession();
const formName = formData.get("name") as string;
const formPassword = formData.get("password") as string;
if (formPassword !== password) {
return { error: "Wrong access code." };
}
session.name = formName;
session.isLoggiedIn = true;
await session.save();
revalidatePath;
};
export const logout = async () => {};