caelum0x 5cb0f48db7 feat: full-product round 2 — auth end-to-end + commerce engines
- apps/api: public /v1/auth routes (register/login/magic-link/session/logout) via
  @settlekit/auth, AuthService in context, signed sk_session cookie; existing
  api-key guard untouched (14 api tests intact).
- apps/dashboard + apps/portal: real login/signup UIs (password + magic-link),
  httpOnly session cookie via route handlers, server session helpers, sign-out.
- packages: fleshed refunds, dunning (retry schedules), disputes (state machine),
  payouts (settlement + available-balance) into real engines with stores+services.

Verified: full build 0 TS errors; 591 tests pass; live auth flow
(register->login->session->magic-link->bad-login 401) confirmed end-to-end.
2026-06-15 17:06:29 +03:00

28 lines
1015 B
TypeScript

// Server-side session helper.
//
// Reads the httpOnly "sk_session" cookie and resolves it to an account by
// calling GET /v1/auth/session. Returns null when there is no cookie or the
// token is no longer valid. This is intentionally NOT a redirect guard —
// pages may call it to personalize, but the dashboard does not force auth so
// existing pages keep rendering.
import { cookies } from "next/headers";
import { getSession, SESSION_COOKIE, type Account } from "./auth";
/** Read the opaque session token from the httpOnly cookie, if present. */
export function getSessionToken(): string | null {
return cookies().get(SESSION_COOKIE)?.value ?? null;
}
/**
* Resolve the current account from the session cookie, or null when there is
* no session / the token is invalid. Never throws.
*/
export async function getCurrentAccount(): Promise<Account | null> {
const token = getSessionToken();
if (!token) return null;
const { data } = await getSession(token);
return data?.account ?? null;
}