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,45 @@
|
||||
// Backend calls from the browser, through the frontend's /api/v1 proxy.
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
public status: number,
|
||||
public code: string,
|
||||
message: string,
|
||||
public details?: unknown,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export async function api<T = unknown>(path: string, init: { method?: string; body?: unknown } = {}): Promise<T> {
|
||||
const res = await fetch(`/api/v1${path}`, {
|
||||
method: init.method ?? (init.body === undefined ? "GET" : "POST"),
|
||||
headers: init.body === undefined ? undefined : { "content-type": "application/json" },
|
||||
body: init.body === undefined ? undefined : JSON.stringify(init.body),
|
||||
credentials: "same-origin",
|
||||
});
|
||||
const text = await res.text();
|
||||
const data = text ? JSON.parse(text) : null;
|
||||
if (!res.ok) {
|
||||
const err = data?.error ?? {};
|
||||
throw new ApiError(res.status, err.code ?? "error", err.message ?? `HTTP ${res.status}`, err);
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
|
||||
/** A readable Hungarian message for an API error. */
|
||||
export function errorText(e: unknown): string {
|
||||
if (e instanceof ApiError) {
|
||||
switch (e.code) {
|
||||
case "unauthorized":
|
||||
return "Lejárt a munkamenet, lépj be újra.";
|
||||
case "forbidden":
|
||||
return "Ehhez nincs jogosultságod.";
|
||||
case "insufficient_credits":
|
||||
return "Nincs elég kredited.";
|
||||
default:
|
||||
return e.message;
|
||||
}
|
||||
}
|
||||
return "Nem sikerült kapcsolódni a szerverhez. Próbáld újra.";
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Hungarian names and glyphs for the chart tables.
|
||||
|
||||
export const SIGNS: Record<string, { hu: string; glyph: string }> = {
|
||||
aries: { hu: "Kos", glyph: "♈" },
|
||||
taurus: { hu: "Bika", glyph: "♉" },
|
||||
gemini: { hu: "Ikrek", glyph: "♊" },
|
||||
cancer: { hu: "Rák", glyph: "♋" },
|
||||
leo: { hu: "Oroszlán", glyph: "♌" },
|
||||
virgo: { hu: "Szűz", glyph: "♍" },
|
||||
libra: { hu: "Mérleg", glyph: "♎" },
|
||||
scorpio: { hu: "Skorpió", glyph: "♏" },
|
||||
sagittarius: { hu: "Nyilas", glyph: "♐" },
|
||||
capricorn: { hu: "Bak", glyph: "♑" },
|
||||
aquarius: { hu: "Vízöntő", glyph: "♒" },
|
||||
pisces: { hu: "Halak", glyph: "♓" },
|
||||
};
|
||||
|
||||
export const POINTS: Record<string, { hu: string; glyph: string }> = {
|
||||
sun: { hu: "Nap", glyph: "☉" },
|
||||
moon: { hu: "Hold", glyph: "☽" },
|
||||
mercury: { hu: "Merkúr", glyph: "☿" },
|
||||
venus: { hu: "Vénusz", glyph: "♀" },
|
||||
mars: { hu: "Mars", glyph: "♂" },
|
||||
jupiter: { hu: "Jupiter", glyph: "♃" },
|
||||
saturn: { hu: "Szaturnusz", glyph: "♄" },
|
||||
uranus: { hu: "Uránusz", glyph: "♅" },
|
||||
neptune: { hu: "Neptunusz", glyph: "♆" },
|
||||
pluto: { hu: "Plútó", glyph: "♇" },
|
||||
north_node: { hu: "Észak-Holdcsomópont", glyph: "☊" },
|
||||
south_node: { hu: "Dél-Holdcsomópont", glyph: "☋" },
|
||||
lilith: { hu: "Lilith", glyph: "⚸" },
|
||||
chiron: { hu: "Kiron", glyph: "⚷" },
|
||||
ceres: { hu: "Ceres", glyph: "⚳" },
|
||||
pallas: { hu: "Pallas", glyph: "⚴" },
|
||||
juno: { hu: "Juno", glyph: "⚵" },
|
||||
vesta: { hu: "Vesta", glyph: "⚶" },
|
||||
};
|
||||
|
||||
export const ANGLES: [string, string][] = [
|
||||
["ascendant", "Aszcendens"],
|
||||
["midheaven", "MC"],
|
||||
["descendant", "Descendens"],
|
||||
["imum_coeli", "IC"],
|
||||
];
|
||||
|
||||
export function signName(sign: string | null | undefined): string {
|
||||
return sign ? (SIGNS[sign]?.hu ?? sign) : "–";
|
||||
}
|
||||
|
||||
/** `12°20'50"` → `12°20′50″` (typographic prime marks). */
|
||||
export function dms(text: string): string {
|
||||
return text.replace(/'/g, "′").replace(/"/g, "″");
|
||||
}
|
||||
|
||||
export const ROLE_LABELS: Record<string, string> = {
|
||||
user: "Felhasználó",
|
||||
premium: "Prémium",
|
||||
vip: "VIP",
|
||||
creator: "Szerző",
|
||||
admin: "Admin",
|
||||
};
|
||||
|
||||
const regionNames = new Intl.DisplayNames(["hu"], { type: "region" });
|
||||
|
||||
/** Hungarian country name from an ISO code (falls back to the given name). */
|
||||
export function countryName(iso2: string, fallback?: string): string {
|
||||
try {
|
||||
return regionNames.of(iso2) ?? fallback ?? iso2;
|
||||
} catch {
|
||||
return fallback ?? iso2;
|
||||
}
|
||||
}
|
||||
|
||||
export function formatDate(iso: string | null | undefined, withTime = false): string {
|
||||
if (!iso) return "–";
|
||||
const d = new Date(iso);
|
||||
return new Intl.DateTimeFormat("hu-HU", {
|
||||
year: "numeric",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
...(withTime ? { hour: "2-digit", minute: "2-digit" } : {}),
|
||||
timeZone: "Europe/Budapest",
|
||||
}).format(d);
|
||||
}
|
||||
|
||||
/** `1990-03-15` → `1990. március 15.` without time zone shifts. */
|
||||
export function formatBirthDate(date: string): string {
|
||||
const [y, m, d] = date.split("-").map(Number);
|
||||
return new Intl.DateTimeFormat("hu-HU", { year: "numeric", month: "long", day: "numeric", timeZone: "UTC" }).format(
|
||||
new Date(Date.UTC(y, m - 1, d)),
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// Shapes of the backend responses the frontend uses.
|
||||
|
||||
export type Role = "admin" | "creator" | "premium" | "vip" | "user";
|
||||
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
email_verified: boolean;
|
||||
display_name: string | null;
|
||||
locale: string;
|
||||
roles: Role[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Me {
|
||||
user: User;
|
||||
providers: string[];
|
||||
primary_chart_id: string | null;
|
||||
credits: number;
|
||||
}
|
||||
|
||||
export interface PublicConfig {
|
||||
google_login: { enabled: boolean; client_id: string | null };
|
||||
locales: string[];
|
||||
llm_models: { id: string; credits: number; role: Role | null }[];
|
||||
default_model: string;
|
||||
monthly_free_credits: number;
|
||||
password_min_length: number;
|
||||
}
|
||||
|
||||
export interface Position {
|
||||
sign: string;
|
||||
degree_in_sign_dms: string;
|
||||
house?: number | null;
|
||||
retrograde?: boolean;
|
||||
}
|
||||
|
||||
export interface ChartPoint extends Position {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export interface ChartResult {
|
||||
points: ChartPoint[];
|
||||
angles: Record<string, Position | number>;
|
||||
houses?: { cusps: (Position & { house: number })[] } | null;
|
||||
lots?: Record<string, Position> | null;
|
||||
time: { time_known: boolean; local: string; zone: string; utc_offset: string; dst: boolean };
|
||||
location: { place?: { name: string; region?: string | null; country_code: string } | null; latitude: number; longitude: number };
|
||||
}
|
||||
|
||||
export interface BirthData {
|
||||
date: string;
|
||||
time?: string | null;
|
||||
country?: string | null;
|
||||
place?: string | null;
|
||||
geonames_id?: number | null;
|
||||
}
|
||||
|
||||
export interface Chart {
|
||||
id: string;
|
||||
label: string;
|
||||
is_primary: boolean;
|
||||
birth_data: BirthData;
|
||||
highlights: { sun: string | null; moon: string | null; ascendant: string | null; time_known: boolean };
|
||||
chart: ChartResult;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface AdminUser {
|
||||
id: string;
|
||||
email: string;
|
||||
display_name: string | null;
|
||||
roles: Role[];
|
||||
email_verified: boolean;
|
||||
created_at: string;
|
||||
last_login_at: string | null;
|
||||
charts: number;
|
||||
credits: number;
|
||||
}
|
||||
|
||||
export interface Place {
|
||||
geonames_id: number;
|
||||
name: string;
|
||||
region: string | null;
|
||||
population: number | null;
|
||||
country_code: string;
|
||||
}
|
||||
|
||||
export interface Country {
|
||||
iso2: string;
|
||||
name: string;
|
||||
}
|
||||
Reference in New Issue
Block a user