init
This commit is contained in:
28
frontend/lib/definitions.ts
Normal file
28
frontend/lib/definitions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const SignupFormSchema = z.object({
|
||||
name: z
|
||||
.string()
|
||||
.min(2, { message: "Name must be at least 2 characters long." })
|
||||
.trim(),
|
||||
password: z
|
||||
.string()
|
||||
.min(4, { message: "Be at least 8 characters long" })
|
||||
.trim(),
|
||||
});
|
||||
|
||||
export type FormState =
|
||||
| {
|
||||
errors?: {
|
||||
name?: string[];
|
||||
email?: string[];
|
||||
password?: string[];
|
||||
};
|
||||
message?: string;
|
||||
}
|
||||
| undefined;
|
||||
|
||||
export interface SessionPayload {
|
||||
userId: string;
|
||||
expiresAt: Date;
|
||||
}
|
||||
121
frontend/lib/menu-list.ts
Normal file
121
frontend/lib/menu-list.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { Tag, SquarePen, Barcode, LucideIcon } from "lucide-react";
|
||||
|
||||
type Submenu = {
|
||||
href: string;
|
||||
label: string;
|
||||
active?: boolean;
|
||||
};
|
||||
|
||||
type Menu = {
|
||||
href: string;
|
||||
label: string;
|
||||
active?: boolean;
|
||||
icon: LucideIcon;
|
||||
submenus?: Submenu[];
|
||||
};
|
||||
|
||||
type Group = {
|
||||
groupLabel: string;
|
||||
menus: Menu[];
|
||||
};
|
||||
|
||||
export function getMenuList(pathname: string): Group[] {
|
||||
return [
|
||||
/*
|
||||
{
|
||||
groupLabel: "",
|
||||
menus: [
|
||||
{
|
||||
href: "/dashboard",
|
||||
label: "Dashboard",
|
||||
icon: LayoutGrid,
|
||||
submenus: []
|
||||
}
|
||||
]
|
||||
},
|
||||
*/
|
||||
{
|
||||
groupLabel: "Flächendruck Service",
|
||||
menus: [
|
||||
{
|
||||
href: "/dashboard/generateFlaechendruck",
|
||||
label: "Generieren",
|
||||
icon: Tag,
|
||||
active: pathname === "/dashboard/generateFlaechendruck",
|
||||
},
|
||||
{
|
||||
href: "/dashboard/vorlagenFlaechendruck",
|
||||
label: "Vorlagen",
|
||||
icon: SquarePen,
|
||||
active: pathname === "/dashboard/vorlagenFlaechendruck",
|
||||
|
||||
/*submenus: [
|
||||
{
|
||||
href: "/posts",
|
||||
label: "All Posts"
|
||||
},
|
||||
{
|
||||
href: "/posts/new",
|
||||
label: "New Post"
|
||||
}
|
||||
]*/
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
groupLabel: "Rollendruck Service",
|
||||
menus: [
|
||||
{
|
||||
href: "/dashboard/generateRollendruck",
|
||||
label: "Generieren",
|
||||
icon: Tag,
|
||||
active: pathname === "/dashboard/generateRollendruck",
|
||||
},
|
||||
{
|
||||
href: "/dashboard/vorlagenRollendruck",
|
||||
label: "Vorlagen",
|
||||
icon: SquarePen,
|
||||
active: pathname === "/dashboard/vorlagenRollendruck",
|
||||
|
||||
/*submenus: [
|
||||
{
|
||||
href: "/posts",
|
||||
label: "All Posts"
|
||||
},
|
||||
{
|
||||
href: "/posts/new",
|
||||
label: "New Post"
|
||||
}
|
||||
]*/
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
groupLabel: "Billbee Service",
|
||||
menus: [
|
||||
{
|
||||
href: "/dashboard/updateOrders",
|
||||
label: "Update Sendungen",
|
||||
icon: Barcode,
|
||||
active: pathname === "/dashboard/updateOrders",
|
||||
},
|
||||
],
|
||||
},
|
||||
/*
|
||||
{
|
||||
groupLabel: "Settings",
|
||||
menus: [
|
||||
{
|
||||
href: "/users",
|
||||
label: "Users",
|
||||
icon: Users
|
||||
},
|
||||
{
|
||||
href: "/account",
|
||||
label: "Account",
|
||||
icon: Settings
|
||||
}
|
||||
]
|
||||
}*/
|
||||
];
|
||||
}
|
||||
56
frontend/lib/session.ts
Normal file
56
frontend/lib/session.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
"use server";
|
||||
|
||||
import { SignJWT, jwtVerify, JWTPayload } from "jose";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
const secretKey = process.env.SESSION_SECRET;
|
||||
const key = new TextEncoder().encode(secretKey);
|
||||
|
||||
type SessionPayload = JWTPayload & {
|
||||
user?: { name: string };
|
||||
expires?: Date;
|
||||
};
|
||||
|
||||
export async function encrypt(payload: SessionPayload) {
|
||||
return await new SignJWT(payload)
|
||||
.setProtectedHeader({ alg: "HS256" })
|
||||
.setIssuedAt()
|
||||
.setExpirationTime("7d")
|
||||
.sign(key);
|
||||
}
|
||||
|
||||
export async function decrypt(session: string | undefined = "") {
|
||||
try {
|
||||
const { payload } = await jwtVerify(session, key, {
|
||||
algorithms: ["HS256"],
|
||||
});
|
||||
return payload;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function login(name: string) {
|
||||
// Verify credentials && get the user
|
||||
|
||||
const user = { name: name };
|
||||
|
||||
// Create the session
|
||||
const expires = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
|
||||
const session = await encrypt({ user, expires });
|
||||
|
||||
// Save the session in a cookie
|
||||
(await cookies()).set("session", session, { expires, httpOnly: true });
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
// Destroy the session
|
||||
(await cookies()).set("session", "", { expires: new Date(0) });
|
||||
}
|
||||
|
||||
export async function getSession() {
|
||||
const session = (await cookies()).get("session")?.value;
|
||||
if (!session) return null;
|
||||
return await decrypt(session);
|
||||
}
|
||||
6
frontend/lib/utils.ts
Normal file
6
frontend/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user