mega-plant/lib/auth-policy.ts
David 1ec52961ee Cash float card, Office role + Slips tab, job card PDF, manual close/delete
- Cash Floats: ledger card (opening balance, top-ups, slips), manual entries, slip photos
- Office access level; clock app Slips tab (photograph a slip, popup for supplier/cost
  code/comments/amount); only Office sees it; per-role route table in lib/clock-access
- Job cards: Machine Service PDF (with Oil block), JC numbers, manual Close job, Delete
- Hide Expenses > Vehicle Costs and Finance (Bastion) from the menu

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-22 15:09:57 +02:00

40 lines
2 KiB
TypeScript

export const STARTER_PASSWORD = "mega2026";
export const SESSION_COOKIE = "mega_session";
const validEmail = (email: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email) && email.length <= 254;
export function validateLogin(email: string, password: string): string | null {
if (!validEmail(email.trim()) || !password) return "Enter your email and password.";
return null;
}
export function validateNewPassword(password: string, confirmation: string): string | null {
if (password.length < 8) return "Choose a password with at least 8 characters.";
if (password === STARTER_PASSWORD) return "Choose a password different from the starter password.";
if (password !== confirmation) return "The passwords do not match.";
return null;
}
export function validateNewUser(name: string, email: string): string | null {
if (!name.trim()) return "Enter the person's name.";
if (!validEmail(email.trim())) return "Enter a valid email address.";
return null;
}
/**
* "full" = the main app and the whole clock app.
* "clock" = clock-in user: the Plant Clock app only (all its tabs).
* "mechanic" = the Plant Clock app's Fuel and Job cards tabs only — no clock in/out scanner.
* "office" = the Plant Clock app's Slips tab only (photograph a cash-card slip).
*/
export type UserAccess = "full" | "clock" | "mechanic" | "office";
export const USER_ACCESS: UserAccess[] = ["full", "clock", "mechanic", "office"];
export const ACCESS_LABEL: Record<UserAccess, string> = { full: "Full", clock: "Clock-in user", mechanic: "Mechanic", office: "Office" };
export const parseAccess = (value: unknown): UserAccess => (value === "clock" || value === "mechanic" || value === "office" ? value : "full");
export function authDestination(session: { passwordChanged: boolean; access?: UserAccess } | null): string {
if (!session) return "/login";
if (!session.passwordChanged) return "/change-password";
// A clock-only user has no home in the main app: send them to the clock.
return session.access && session.access !== "full" ? "/clock" : "/";
}