Part 15 / Topic 3 of 5
The DAL: getCurrentUser (optional) and requireUser (the real gate)
// harness/app/account/lib/auth.ts
async function resolveCurrentUser(): Promise<StoredUser | null> {
"use cache: private";
const session = await getSession();
if (!session.userId) return null;
return findUserById(session.userId);
}
export async function getCurrentUser() { return resolveCurrentUser(); }
export async function requireUser(): Promise<StoredUser> {
const user = await resolveCurrentUser();
if (!user) redirect("/account/login");
return user;
}
"use cache: private" exists for exactly this function's shape — it reads cookies() (a runtime API a plain "use cache" cannot touch at all, confirmed directly against the installed use-cache-private.md reference's own table: cookies()/headers()/searchParams are allowed inside "use cache: private", not inside plain "use cache"), and the result "never [gets] store[d] in a server cache across requests" — it's per-request, per-user, by design. The installed docs' own Data Access Layer section names the pattern directly: "We recommend creating a DAL to centralize your data requests and authorization logic... the function should check if the session is valid, then redirect or return the user information needed." Splitting that into two functions (getCurrentUser, no redirect; requireUser, redirects) rather than the installed docs' single verifySession is deliberate here — AccountNav (section 4) wants to render differently for a logged-out visitor in some contexts and block in others, and one function can't honestly do both.
Done here? The practice set for Part 15 — including the solutions — is on the part page.