Add PocketBase login, first-sign-in password changes, account settings, and user activation, reactivation and deletion with self-removal guards. Protect application pages and APIs and keep private pages out of PWA caches. Resolve plant locations from site assignments and improve map controls, profit chart views, navigation and mobile table layouts. Validation: production build and TypeScript passed; 28 unit tests passed. Backend access integration tests and mobile verification are documented.
19 lines
692 B
TypeScript
19 lines
692 B
TypeScript
import "server-only";
|
|
import { cache } from "react";
|
|
import { cookies } from "next/headers";
|
|
import { redirect } from "next/navigation";
|
|
import { sessionFromToken } from "./auth-service";
|
|
import { SESSION_COOKIE, authDestination } from "./auth-policy";
|
|
|
|
// React cache scopes this result to one render, never across users or requests.
|
|
export const getSession = cache(async () => {
|
|
const token = (await cookies()).get(SESSION_COOKIE)?.value;
|
|
return token ? sessionFromToken(token) : null;
|
|
});
|
|
|
|
export async function requireUser() {
|
|
const session = await getSession();
|
|
const destination = authDestination(session);
|
|
if (destination !== "/") redirect(destination);
|
|
return session!;
|
|
}
|