Start the Next.js frontend: Google login, profile, birth data, admin
services/frontend (Next.js 16, App Router) runs on port 3001 in compose. The browser only talks to the frontend: /api/v1/* is proxied to the backend, so the session cookie lives on the frontend's host, and /api/geo/* exposes the country list and place search of the horoscope-api. - /belepes: Google Identity Services button, client id from /v1/config - /profil: account facts, display name and language, own birth data with place search, DST ambiguity prompt and the chart's positions - /admin: user search, role toggles, credit grants (admins only, 404 otherwise) Services now tolerate migrations newer than their binary, so they can be rebuilt one at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Backend calls from server components, with the visitor's session.
|
||||
import "server-only";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import type { Me } from "./types";
|
||||
|
||||
export const BACKEND_URL = (process.env.BACKEND_URL ?? "http://localhost:8081").replace(/\/$/, "");
|
||||
export const SESSION_COOKIE = "sf_session";
|
||||
|
||||
export class BackendError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
message: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
/** GET from the backend as the logged-in visitor; `null` on 401/404. */
|
||||
export async function backendGet<T>(path: string): Promise<T | null> {
|
||||
const token = (await cookies()).get(SESSION_COOKIE)?.value;
|
||||
const headers: HeadersInit = token ? { authorization: `Bearer ${token}` } : {};
|
||||
const res = await fetch(`${BACKEND_URL}${path}`, { headers, cache: "no-store" });
|
||||
if (res.status === 401 || res.status === 404) return null;
|
||||
if (!res.ok) throw new BackendError(res.status, `${path}: HTTP ${res.status}`);
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
|
||||
/** The logged-in user, or `null`. */
|
||||
export async function getMe(): Promise<Me | null> {
|
||||
const token = (await cookies()).get(SESSION_COOKIE)?.value;
|
||||
if (!token) return null;
|
||||
return backendGet<Me>("/v1/me");
|
||||
}
|
||||
|
||||
/** The logged-in user; sends visitors to the login page. */
|
||||
export async function requireMe(from: string): Promise<Me> {
|
||||
const me = await getMe();
|
||||
if (!me) redirect(`/belepes?vissza=${encodeURIComponent(from)}`);
|
||||
return me;
|
||||
}
|
||||
Reference in New Issue
Block a user