mega-plant/lib/auth-policy.ts
David 29a035a4f2 Clock app: vehicle-QR diesel book-out with meter photo; user access levels
- Scanning a vehicle QR (its register code, e.g. TLB04) books diesel from the main
  tank; litres, optional odometer and a required meter photo (stored protected,
  viewable from the Booked out table)
- Settings: users are Full or Clock-app-only; clock-only users are confined to /clock
- Clock app: tapped tab highlights at once plus a loading screen; remove the
  "scanned on this device" list

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

34 lines
1.6 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 clock app); "clock" = the Plant Clock app only. */
export type UserAccess = "full" | "clock";
export const USER_ACCESS: UserAccess[] = ["full", "clock"];
export const parseAccess = (value: unknown): UserAccess => (value === "clock" ? "clock" : "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 === "clock" ? "/clock" : "/";
}