diff --git a/README.md b/README.md index 3aa2a8c..6446900 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Compact, developer-friendly monorepo for a central identity service, a branded l - **P01 Foundation:** complete, initial commit `8f54ccb`. - **P02 ZITADEL local backend:** pinned official images, development Compose and secure local bootstrap helper source prepared. **Docker startup and Admin Console integration remain unverified**; all follow-up local testing is explicitly deferred to a future Gitea Actions scope. - **P03 Login V2 source:** official v4.20.4 source and matching client/proto workspace imported under `auth/ui/v2/`; no redesign or runtime build/test was performed. **CI validation remains pending.** -- **P04 Account Portal:** not started. +- **P04 Account Portal:** Next.js + ZITADEL OIDC Auth.js source, server-protected overview/profile/security pages and compact responsive foundation implemented under `users/ui/`. **No build/test/runtime validation yet; future Gitea Actions required.** - **Production / Coolify:** not configured or deployed. ## Responsibility map @@ -16,7 +16,7 @@ Compact, developer-friendly monorepo for a central identity service, a branded l | `core/backend/zitadel/` | Official ZITADEL backend integration and local helper | | `deployment/docker/compose.backend.dev.yaml` | Loopback-only developer stack | | `auth/ui/v2/` | Official Login V2 monorepo subset (Next.js source in `apps/login/`, matched client/proto and pnpm/Nx workspace) | -| `users/ui/` | Future account self-service portal (P04) | +| `users/ui/` | P04 account portal source: separate Next.js app, protected pages, OIDC, server-side user API (CI pending) | | `packages/` | Shared packages only when needed | | `tests/` | Focused contracts and future integration/UI tests | | `docs/` | Architecture, scopes and developer instructions | @@ -39,4 +39,4 @@ When the backend stack is actually running, the local Admin Console is expected - One Git repo, isolated containers; ZITADEL Go source remains upstream. ## Working agreements -Read [AGENTS.md](AGENTS.md), [architecture](docs/ARCHITECTURE.md), [P03 source import](docs/P03-SOURCE-IMPORT.md), [Login V2 workspace guide](auth/ui/v2/README.md) and [phase scopes](docs/PHASES.md). Use separate, expressly approved phases; no production secrets or user data in Git. +Read [AGENTS.md](AGENTS.md), [architecture](docs/ARCHITECTURE.md), [P03 source import](docs/P03-SOURCE-IMPORT.md), [Login V2 workspace guide](auth/ui/v2/README.md), [P04 account evidence](docs/P04-ACCOUNT-PORTAL.md), [Account Portal guide](users/ui/README.md) and [phase scopes](docs/PHASES.md). Use separate, expressly approved phases; no production secrets or user data in Git. diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c3aafc1..2b87936 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -5,7 +5,7 @@ ZitPlane is a central **identity and account-management platform**, not a billin - **ZITADEL** (official Go server): identities, authentication, OIDC, security policies, projects, admin console. - **Login V2** (customized upstream Next.js app): branded signup, sign-in, recovery and challenge screens. -- **Account portal** (custom Next.js app): user-scoped profile/security/linked identities. +- **Account portal** (P04 separate Next.js App Router source at `users/ui/`): standard ZITADEL OIDC + Auth.js, server-protected overview/read-only profile/security navigation, user-scoped API. Real user editing and security-device management are **not implemented yet**; CI and live integration remain pending. - **PostgreSQL**: persistent ZITADEL storage. Do not expose directly to the Internet. - **Coolify / Compose**: one Git deployment definition, isolated services and persistent volumes. diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md index e5d05cc..c6fc9fd 100644 --- a/docs/DEVELOPMENT.md +++ b/docs/DEVELOPMENT.md @@ -4,7 +4,7 @@ - P01 Foundation: committed. - P02 Local backend: configuration and helper implemented; **runtime verification blocked** because Docker is not installed on the connected Windows PC. - P03 Login V2: official v4.20.4 Next.js source and upstream pnpm/Nx/client/proto dependencies **imported** under `auth/ui/v2/`; builds/dependency installation and runtime verification **intentionally deferred to Gitea Actions**. -- P04 Account Portal: not started. +- P04 Account Portal: independent Next.js/App Router + ZITADEL OIDC/Auth.js source and compact profile/security UI implemented under `users/ui/`; **no dependency install, lockfile, build, tests or runtime validation performed**. See `users/ui/README.md` and `docs/P04-ACCOUNT-PORTAL.md`. ## Getting local ZITADEL running 1. Install and launch Docker Desktop with Linux containers. Verify `docker compose version`. @@ -20,7 +20,7 @@ The proxy is **loopback-only and HTTP-only**. It is for disposable local develop ## Where to change code (future phases) - Official Go server: **no core source edits**, see `core/backend/zitadel/`. - Auth UI changes: `auth/ui/v2/apps/login/src/` (upstream workspace kept intact). See `auth/ui/v2/README.md` for beginner-friendly locations. -- Account UI changes: `users/ui/` after P04. +- Account UI changes: `users/ui/src/app/`, `users/ui/src/components/`, and `users/ui/src/styles/`; protected identity/auth integration lives in `users/ui/src/auth.ts` and `users/ui/src/lib/` (no visual-only changes there). - Shared UI/components: `packages/` only if required by imported code. - Local Docker infrastructure: `deployment/docker/`. diff --git a/docs/P04-ACCOUNT-PORTAL.md b/docs/P04-ACCOUNT-PORTAL.md new file mode 100644 index 0000000..9184b87 --- /dev/null +++ b/docs/P04-ACCOUNT-PORTAL.md @@ -0,0 +1,43 @@ +# ZitPlane P04 — Account Portal Source Evidence + +Date: 2026-11-22 +Approved baseline: `4404849fc4bd30c7b4b7cabe7caed54cbd7cd37d` +Scope: `users/ui/**` + narrow `README.md` and `docs/**`; **P02/P03 source READ-ONLY**. + +## Implemented + +- Separate Next.js `16.4.3` / React `19.2.6` App Router source, Auth.js `next-auth@5.0.0-beta.32` for ZITADEL. +- OIDC Authorization Code + PKCE, state and nonce checks, dedicated confidential web client, app-specific JWT session (max 1h). +- Validated runtime-only issuer, app origin, client credentials and session secret; production requires HTTPS. +- No mock production user, browser-stored token, unsafe callback URL, reused privileged admin PAT or server master key. +- Server-side protected layout plus per-page and read-only API identity guards. **User ID is an immutable OIDC `sub`**, never email; token expiry cleared when absent/expired. +- Public `/signin`, protected overview/profile/security, and `GET /api/account/me` with user-only response / 401 and no-store. +- Local-only sign-out, explicitly **not a global SSO logout**. +- Responsive foundation UI, CSS media-query dark mode, no UI dependency on the unmodified Login V2 build. +- Read-only profile and links to existing ZITADEL self-service only. Editing profile/MFA, billing, subscription and cross-app admin remain out-of-scope. + +## Intentional omissions / acceptance gates + +The user explicitly excluded all **local builds, tests, dev servers and deployments**, and requested CI in a later phase. As a result, the following are **NOT VERIFIED**: +1. npm dependency compatibility and reproducible lockfile (no lockfile generated or installed in P04). +2. TypeScript typechecking, Next.js production build, React rendering and Auth.js provider behavior. +3. Real ZITADEL OIDC login, callback, session expiry, safe logout and user-scoped API 401/200 behavior. +4. HTTPS proxy/host forwarding, WebAuthn/SSO, account self-service permissions, Docker/Coolify integration. + +Auth.js v5 is still beta in the pinned version, so CI should validate compatibility and consider a stable alternative before production approval. The beta.32 version includes a patch for advisory GHSA-8fpg-xm3f-6cx3; user identity guards use `session.user.id` rather than merely `!!session`. + +**P04 source-only audit (2026-11-22):** 28 staged approved files (23 under `users/ui/**`, 5 approved README/docs paths), P02/P03 changes **0**, staged secret files **0**, `git diff --cached --check` **PASS**. Existing unrelated untracked `AI_INSTRUCTIONS.md` was left untouched. This audit verifies repository scope and basic patch hygiene, **not** authentication behavior or build correctness. + +**No Gitea Actions workflow is created or run in P04.** Source-only Git scope inspection is permitted and does not constitute tests/build. Do not declare runtime acceptance until focused disposable CI and staging checks pass. + +## Next steps + +- P05: approve integration scope and CI foundation, including lockfile generation, version/security review and disposable ZITADEL OIDC configuration; no live deployment. +- P06: compact branded UI redesign limited to page/components/styles while preserving authentication contract. +- P07: hardened deployment with configured public domains, TLS, persistent secrets and backups after acceptance. + +References: +- https://zitadel.com/docs/sdk-examples/nextjs +- https://authjs.dev/getting-started/installation?framework=Next.js +- https://authjs.dev/getting-started/session-management/protecting +- https://github.com/nextauthjs/next-auth/security/advisories/GHSA-8fpg-xm3f-6cx3 diff --git a/docs/PHASES.md b/docs/PHASES.md index eae4a67..8d46eb4 100644 --- a/docs/PHASES.md +++ b/docs/PHASES.md @@ -25,8 +25,13 @@ Implement one explicitly approved phase at a time. Keep out-of-scope files, exis - P02 prebuilt Login UI remains configured in the development Compose; do **not** replace it or call runtime acceptance complete. - Evidence and known gaps: `docs/P03-SOURCE-IMPORT.md`. CI workflow not part of this scope. -## P04 — Account portal foundation (proposed, NOT approved) -OIDC-protected basic account self-service skeleton; no fake production authentication. +## P04 — User Account Portal foundation (APPROVED 2026-11-22; SOURCE IMPLEMENTED) +- **Baseline:** `4404849fc4bd30c7b4b7cabe7caed54cbd7cd37d`. +- **Exact Write Scope:** `users/ui/**`, narrow project `README.md` and relevant `docs/**` (phase/evidence/architecture/developer guide). **P02/P03 source read-only.** +- Separate compact Next.js portal, ZITADEL OIDC/PKCE authentication, server-protected user routes, read-only profile and security settings structure. +- Real OIDC only; no fake production auth, browser admin credentials, billing or user-management mutation endpoints. Existing ZITADEL core/Login V2 untouched. +- Explicitly **no local builds, tests, dev server, Docker or deployment**. Source-level audit only, Git commit and approved Gitea main push. Gitea Actions testing deferred; no CI workflow in this scope. +- Evidence and pending runtime acceptance: `docs/P04-ACCOUNT-PORTAL.md`. ## P05 — Integration (proposed, NOT approved) Local development, shared UI tokens as needed, production-safe Docker topology, routing drafts, tests/docs. No live deployment. diff --git a/users/ui/.env.example b/users/ui/.env.example new file mode 100644 index 0000000..b6ab113 --- /dev/null +++ b/users/ui/.env.example @@ -0,0 +1,12 @@ +# ZitPlane Account Portal example ONLY (never store real values in Git). +# Create local users/ui/.env.local or inject secrets from a secure host/CI secret store. +# The issuer is the external, browser-reachable ZITADEL instance URL. +ZITADEL_ISSUER=http://localhost:8080 +ZITADEL_CLIENT_ID=REPLACE_WITH_ACCOUNT_PORTAL_OIDC_CLIENT_ID +# For Auth.js, use a confidential client and real secret. +# Do NOT put the ZITADEL server master key or Login V2 PAT here. +ZITADEL_CLIENT_SECRET=REPLACE_WITH_ACCOUNT_PORTAL_OIDC_CLIENT_SECRET +AUTH_SECRET=REPLACE_WITH_RANDOM_AT_LEAST_32_CHARACTERS +AUTH_URL=http://localhost:3001 +# Only enable when behind a trusted reverse proxy that sanitizes forwarded host headers. +AUTH_TRUST_HOST=false diff --git a/users/ui/.gitignore b/users/ui/.gitignore new file mode 100644 index 0000000..6ec9965 --- /dev/null +++ b/users/ui/.gitignore @@ -0,0 +1,9 @@ +node_modules/ +.next/ +out/ +coverage/ +*.tsbuildinfo +.env +.env.* +!.env.example +*.log diff --git a/users/ui/README.md b/users/ui/README.md index c14f05b..724cebd 100644 --- a/users/ui/README.md +++ b/users/ui/README.md @@ -1,3 +1,77 @@ -# Account portal frontend +# ZitPlane — Central Account Portal (P04) -Reserved for a separate responsive Next.js self-service UI. ZITADEL remains the source of authentication and identity. Phase 04 will introduce the application with secure user-scoped APIs; there is no runnable portal yet. +**Status:** Source foundation implemented, **not built, tested, started or deployed**. The UI is compact and responsive, but its visual redesign is reserved for P06. + +This is a separate Next.js 16 + React 19 App Router frontend using Auth.js / NextAuth.js 5.0.0-beta.32 and the built-in **ZITADEL OIDC provider**. No passwords, token-management UI, fake sessions or credentials are stored by this application. It does not replace the official ZITADEL Admin Console or the Login V2 frontend. + +## Simple folder guide + +| Folder | Change here for | +| --- | --- | +| `src/app/signin/` | Public sign-in entry (redirects to ZITADEL) | +| `src/app/(protected)/account/` | Overview, read-only profile and security landing pages | +| `src/components/` | Navigation shell, buttons and reusable display components | +| `src/styles/globals.css` | Mobile, light/dark styling and design tokens | +| `src/auth.ts` | **Protected** OIDC/auth configuration. Never edit for visual tasks | +| `src/lib/env.ts` | **Protected** runtime configuration validation | +| `src/lib/identity.ts` | **Protected** mandatory server-side identity checks | +| `src/app/api/` | Auth.js OIDC callback route and a protected, read-only `/api/account/me` | +| `src/types/` | Auth.js session type (immutable ZITADEL user ID) | + +## Routes and what works in source + +- `/` redirects to `/account`. +- `/signin` offers **real Auth.js OIDC sign-in**, no mock login. +- `/account` is server-side protected. +- `/account/profile` displays OIDC name/email/immutable subject **read-only**. +- `/account/security` explains credential ownership and links to ZITADEL self-service **if enabled for this user**. No local password/MFA editing is claimed. +- `/api/account/me` returns the authenticated user's ID/name/email only, or 401; no-store, no privileged ZITADEL API. +- `Sign out of portal` ends the local Auth.js session, **not necessarily global ZITADEL/other SaaS SSO**. +- The portal **never** manages app billing, subscriptions, license keys or product permissions. + +Access is checked in the protected layout **and individually in sensitive pages/API routes** using a nonempty verified session.user.id. Token expiry is tracked; if expiry is absent or past, the user ID is cleared (fail closed). Identity comes from the OIDC `sub`/providerAccountId, not mutable email. Refresh tokens, explicit global SSO logout, session revocation and account edit workflows are NOT implemented in P04. + +## Future development settings (DOCUMENTATION ONLY) + +Choose a dedicated **confidential ZITADEL Web/OIDC client**, authorization code + PKCE, with client secret held only on the account server. Do NOT reuse the official Login V2 service PAT, the ZITADEL master key, an arbitrary public client secret, or another SaaS app's client credentials. + +In development, register the exact callback: + +`http://localhost:3001/api/auth/callback/zitadel` + +For production, register the exact callback: + +`https://account.vib.tools/api/auth/callback/zitadel` + +Enable HTTP redirect dev mode **only on disposable local ZITADEL**. The production OIDC issuer and `AUTH_URL` must be HTTPS. + +Provide runtime variables (see `.env.example`): + +| Variable | Purpose | +| --- | --- | +| `ZITADEL_ISSUER` | External, browser-accessible ZITADEL issuer URL, e.g. https://auth.vib.tools | +| `ZITADEL_CLIENT_ID` | Dedicated Account Portal OIDC client ID | +| `ZITADEL_CLIENT_SECRET` | Dedicated server-side confidential client secret | +| `AUTH_SECRET` | Unique random secret, at least 32 characters | +| `AUTH_URL` | Exact public Account Portal origin (no trailing path) | +| `AUTH_TRUST_HOST` | `true` only behind a correctly configured, trusted reverse proxy | + +Neither P02 Docker nor a production domain is wired to this portal in P04. An isolated Gitea CI phase will first need to create a **reviewed deterministic lockfile** (this phase did not install dependencies), install dependencies, validate Auth.js beta/Next.js compatibility, typecheck/build, perform security checks and test OIDC callbacks against disposable ZITADEL. **Until those gates pass, do not deploy or claim production readiness.** + +## Source-only dependency notes + +Pinned versions are declared in `package.json` without package-manager invocation. Auth.js 5.0.0-beta.32 is the patched version for GHSA-8fpg-xm3f-6cx3; both route protection and API access additionally require `session.user.id`, not just a truthy session. Future CI must review new security advisories and create a locked dependency graph. `npm ci` cannot work until the lockfile is created and approved. + +## Security notes for AI UI editors + +- Allowed visual edits in later P06: `src/components/**`, `src/styles/**` and approved page JSX only. +- Do not touch `src/auth.ts`, credential validation, OIDC callbacks, token expiration, protected layout or API routes without separate approval. +- No `mockUser`, hardcoded JWT, localStorage token, browser-side master key or administrative API calls. +- ZITADEL user self-service link is an interim navigation aid; route access/feature availability is policy-dependent. +- Auth.js uses browser redirects and server actions, never direct browser calls to token endpoints. + +Sources: +- https://zitadel.com/docs/sdk-examples/nextjs +- https://authjs.dev/getting-started/installation?framework=Next.js +- https://authjs.dev/getting-started/session-management/protecting +- https://github.com/nextauthjs/next-auth/security/advisories/GHSA-8fpg-xm3f-6cx3 diff --git a/users/ui/next-env.d.ts b/users/ui/next-env.d.ts new file mode 100644 index 0000000..f4686ec --- /dev/null +++ b/users/ui/next-env.d.ts @@ -0,0 +1,4 @@ +/// +/// + +// This file is managed by Next.js; avoid editing manually. diff --git a/users/ui/next.config.ts b/users/ui/next.config.ts new file mode 100644 index 0000000..df6d268 --- /dev/null +++ b/users/ui/next.config.ts @@ -0,0 +1,22 @@ +import type { NextConfig } from "next"; + +const nextConfig: NextConfig = { + output: "standalone", + poweredByHeader: false, + reactStrictMode: true, + async headers() { + return [ + { + source: "/(.*)", + headers: [ + { key: "X-Content-Type-Options", value: "nosniff" }, + { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" }, + { key: "X-Frame-Options", value: "DENY" }, + { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" } + ] + } + ]; + } +}; + +export default nextConfig; diff --git a/users/ui/package.json b/users/ui/package.json new file mode 100644 index 0000000..c981239 --- /dev/null +++ b/users/ui/package.json @@ -0,0 +1,28 @@ +{ + "name": "@zitplane/account-portal", + "version": "0.1.0", + "private": true, + "description": "ZitPlane central account self-service portal", + "engines": { + "node": ">=22.0.0" + }, + "scripts": { + "dev": "next dev", + "build": "next build", + "start": "next start", + "typecheck": "tsc --noEmit" + }, + "dependencies": { + "next": "16.4.3", + "next-auth": "5.0.0-beta.32", + "react": "19.2.6", + "react-dom": "19.2.6", + "server-only": "0.0.1" + }, + "devDependencies": { + "@types/node": "25.7.0", + "@types/react": "19.2.14", + "@types/react-dom": "19.2.3", + "typescript": "5.9.3" + } +} diff --git a/users/ui/src/app/(protected)/account/page.tsx b/users/ui/src/app/(protected)/account/page.tsx new file mode 100644 index 0000000..617b389 --- /dev/null +++ b/users/ui/src/app/(protected)/account/page.tsx @@ -0,0 +1,46 @@ +import Link from "next/link"; + +import { AccountCard } from "@/components/account-card"; +import { requireIdentity } from "@/lib/identity"; + +export const metadata = { title: "Account overview" }; + +export default async function AccountPage() { + const user = await requireIdentity(); + const displayName = user.name?.trim() || user.email || "there"; + + return ( +
+
+

ACCOUNT OVERVIEW

+

Welcome, {displayName}

+

+ Your identity is managed centrally. Each connected SaaS product + manages its own subscriptions, billing and application data. +

+
+
+ + +
+
+

One account. Multiple applications.

+

+ Your ZitPlane identity can be used with multiple connected apps. + Billing and product-specific permissions remain inside each app. +

+ Review account details +
+
+ ); +} diff --git a/users/ui/src/app/(protected)/account/profile/page.tsx b/users/ui/src/app/(protected)/account/profile/page.tsx new file mode 100644 index 0000000..8899eda --- /dev/null +++ b/users/ui/src/app/(protected)/account/profile/page.tsx @@ -0,0 +1,47 @@ +import { DataRow } from "@/components/account-card"; +import { requireIdentity, zitadelSelfServiceUrl } from "@/lib/identity"; + +export const metadata = { title: "Personal information" }; + +export default async function ProfilePage() { + const user = await requireIdentity(); + + return ( +
+
+

PROFILE

+

Personal information

+

+ These details come from your authenticated ZITADEL session. + They are read-only in this foundation release. +

+
+
+
+

Account details

+ Central identity +
+
+ + + +
+

+ The immutable identity ID links your account across applications. + Email addresses can change; do not use them as an account database key. +

+
+
+

Need to update your information?

+

+ Use your ZITADEL self-service settings if your organization allows + account editing there. This portal does not currently modify identity data. +

+ + Open ZITADEL account settings + + +
+
+ ); +} diff --git a/users/ui/src/app/(protected)/account/security/page.tsx b/users/ui/src/app/(protected)/account/security/page.tsx new file mode 100644 index 0000000..3699a45 --- /dev/null +++ b/users/ui/src/app/(protected)/account/security/page.tsx @@ -0,0 +1,58 @@ +import { requireIdentity, zitadelSelfServiceUrl } from "@/lib/identity"; + +export const metadata = { title: "Security" }; + +const items = [ + { + title: "Password", + detail: "Password changes and recovery are handled by ZITADEL." + }, + { + title: "Multi-factor authentication", + detail: "Manage available authenticators and verification methods under your identity policies." + }, + { + title: "Passkeys and security keys", + detail: "Available methods depend on your organization's ZITADEL configuration." + } +]; + +export default async function SecurityPage() { + await requireIdentity(); + + return ( +
+
+

SECURITY

+

Security settings

+

+ ZitPlane delegates credentials and authentication to ZITADEL. + No password or MFA secret is stored in this portal. +

+
+
+

Account protection

+
+ {items.map((item) => ( +
+
+

{item.title}

+

{item.detail}

+
+ ZITADEL +
+ ))} +
+ + Open central security settings + + +
+

+ This link opens the existing ZITADEL self-service console. Access and + available controls depend on the user's permissions and active policies. + Portal-local sign-out does not terminate every application's SSO session. +

+
+ ); +} diff --git a/users/ui/src/app/(protected)/layout.tsx b/users/ui/src/app/(protected)/layout.tsx new file mode 100644 index 0000000..700488a --- /dev/null +++ b/users/ui/src/app/(protected)/layout.tsx @@ -0,0 +1,15 @@ +import type { ReactNode } from "react"; + +import { AccountShell } from "@/components/account-shell"; +import { requireIdentity } from "@/lib/identity"; + +export const dynamic = "force-dynamic"; + +export default async function ProtectedLayout({ + children +}: { + children: ReactNode; +}) { + const user = await requireIdentity(); + return {children}; +} diff --git a/users/ui/src/app/api/account/me/route.ts b/users/ui/src/app/api/account/me/route.ts new file mode 100644 index 0000000..0b270f5 --- /dev/null +++ b/users/ui/src/app/api/account/me/route.ts @@ -0,0 +1,26 @@ +import { NextResponse } from "next/server"; + +import { auth } from "@/auth"; + +export const dynamic = "force-dynamic"; + +export async function GET() { + const session = await auth(); + if (!session?.user?.id) { + return NextResponse.json( + { error: "Unauthorized" }, + { status: 401, headers: { "Cache-Control": "private, no-store" } } + ); + } + + return NextResponse.json( + { + user: { + id: session.user.id, + name: session.user.name ?? null, + email: session.user.email ?? null + } + }, + { headers: { "Cache-Control": "private, no-store" } } + ); +} diff --git a/users/ui/src/app/api/auth/[...nextauth]/route.ts b/users/ui/src/app/api/auth/[...nextauth]/route.ts new file mode 100644 index 0000000..86c9f3d --- /dev/null +++ b/users/ui/src/app/api/auth/[...nextauth]/route.ts @@ -0,0 +1,3 @@ +import { handlers } from "@/auth"; + +export const { GET, POST } = handlers; diff --git a/users/ui/src/app/layout.tsx b/users/ui/src/app/layout.tsx new file mode 100644 index 0000000..e1a7158 --- /dev/null +++ b/users/ui/src/app/layout.tsx @@ -0,0 +1,21 @@ +import type { Metadata } from "next"; +import type { ReactNode } from "react"; + +import "@/styles/globals.css"; + +export const metadata: Metadata = { + title: { + default: "ZitPlane Account", + template: "%s | ZitPlane" + }, + description: "Centralized account and security settings for your ZitPlane identity.", + robots: { index: false, follow: false } +}; + +export default function RootLayout({ children }: { children: ReactNode }) { + return ( + + {children} + + ); +} diff --git a/users/ui/src/app/page.tsx b/users/ui/src/app/page.tsx new file mode 100644 index 0000000..104a681 --- /dev/null +++ b/users/ui/src/app/page.tsx @@ -0,0 +1,5 @@ +import { redirect } from "next/navigation"; + +export default function Home() { + redirect("/account"); +} diff --git a/users/ui/src/app/signin/page.tsx b/users/ui/src/app/signin/page.tsx new file mode 100644 index 0000000..0e1a3d5 --- /dev/null +++ b/users/ui/src/app/signin/page.tsx @@ -0,0 +1,42 @@ +import Link from "next/link"; +import { redirect } from "next/navigation"; + +import { auth, signIn } from "@/auth"; + +export const dynamic = "force-dynamic"; + +export default async function SignInPage() { + const session = await auth(); + if (session?.user?.id) { + redirect("/account"); + } + + async function startLogin() { + "use server"; + await signIn("zitadel", { redirectTo: "/account" }); + } + + return ( +
+
+ +

ZITPLANE IDENTITY

+

Your account, one secure place.

+

+ Sign in with your central ZITADEL identity to manage your account. + Passwords, passkeys, and MFA stay with your identity provider. +

+
+ +
+

+ New to ZitPlane? Registration is handled by the central sign-in service, + according to its account policies. +

+ Back to account home +
+
+ ); +} diff --git a/users/ui/src/auth.ts b/users/ui/src/auth.ts new file mode 100644 index 0000000..a44e221 --- /dev/null +++ b/users/ui/src/auth.ts @@ -0,0 +1,69 @@ +import NextAuth from "next-auth"; +import Zitadel from "next-auth/providers/zitadel"; + +import { oidcSettings } from "@/lib/env"; + +/** + * The ZITADEL account portal is a standard OIDC relying party. + * No ZITADEL master key, service PAT or user password is used here. + * Auth.js manages PKCE, state, callbacks and encrypted HTTP-only session cookies. + * + * The local app session expires no later than the initial OIDC access-token lifetime. + * Automatic refresh and federated logout are deliberately future-scoped work. + */ +export const { handlers, auth, signIn, signOut } = NextAuth(() => { + const { issuer, clientId, clientSecret, secret, trustHost } = oidcSettings(); + + return { + secret, + trustHost, + providers: [ + Zitadel({ + issuer, + clientId, + clientSecret, + checks: ["pkce", "state", "nonce"], + authorization: { params: { scope: "openid profile email" } } + }) + ], + session: { strategy: "jwt", maxAge: 60 * 60 }, + callbacks: { + async signIn({ account, profile }) { + return ( + account?.provider === "zitadel" && + typeof profile?.sub === "string" && + profile.sub.length > 0 + ); + }, + async jwt({ token, account }) { + if (account?.provider === "zitadel") { + token.zitadelSubject = + typeof account.providerAccountId === "string" + ? account.providerAccountId + : undefined; + // Fail closed when the provider does not return an expiration. + token.zitadelTokenExpiresAt = + typeof account.expires_at === "number" ? account.expires_at : 0; + } + return token; + }, + async session({ session, token }) { + const valid = + typeof token.zitadelSubject === "string" && + token.zitadelSubject.length > 0 && + typeof token.zitadelTokenExpiresAt === "number" && + token.zitadelTokenExpiresAt * 1000 > Date.now(); + + // Never expose an authenticated identity when an OIDC token has expired. + // Every page and API checks nonempty user.id; do not gate on !!session alone. + return { + ...session, + user: { + ...session.user, + id: valid ? token.zitadelSubject! : "" + } + }; + } + } + }; +}); diff --git a/users/ui/src/components/account-card.tsx b/users/ui/src/components/account-card.tsx new file mode 100644 index 0000000..e0bcb6b --- /dev/null +++ b/users/ui/src/components/account-card.tsx @@ -0,0 +1,37 @@ +import Link from "next/link"; +import type { ReactNode } from "react"; + +export function AccountCard({ + title, + description, + href, + action +}: { + title: string; + description: ReactNode; + href: string; + action: string; +}) { + return ( +
+

{title}

+

{description}

+ {action} +
+ ); +} + +export function DataRow({ + label, + value +}: { + label: string; + value?: string | null; +}) { + return ( +
+
{label}
+
{value || "Not provided"}
+
+ ); +} diff --git a/users/ui/src/components/account-shell.tsx b/users/ui/src/components/account-shell.tsx new file mode 100644 index 0000000..c178d6f --- /dev/null +++ b/users/ui/src/components/account-shell.tsx @@ -0,0 +1,80 @@ +import Link from "next/link"; +import type { ReactNode } from "react"; + +import { signOut } from "@/auth"; + +type AccountUser = { + id: string; + name?: string | null; + email?: string | null; + image?: string | null; +}; + +const navigation = [ + { href: "/account", label: "Overview", description: "Account home" }, + { href: "/account/profile", label: "Profile", description: "Personal information" }, + { href: "/account/security", label: "Security", description: "Credentials and access" } +] as const; + +export function AccountShell({ + user, + children +}: { + user: AccountUser; + children: ReactNode; +}) { + async function portalLogout() { + "use server"; + await signOut({ redirectTo: "/signin" }); + } + + return ( +
+ +
+
+ Identity & security + +
+
+ {children} +
+ +
+
+ ); +} diff --git a/users/ui/src/lib/env.ts b/users/ui/src/lib/env.ts new file mode 100644 index 0000000..c105965 --- /dev/null +++ b/users/ui/src/lib/env.ts @@ -0,0 +1,46 @@ +import "server-only"; + +function required(name: string): string { + const value = process.env[name]?.trim(); + if (!value || value.startsWith("REPLACE_")) { + throw new Error(`Missing required account portal setting: ${name}`); + } + return value; +} + +function validatedUrl(name: string): string { + const raw = required(name); + const url = new URL(raw); + const loopback = url.hostname === "localhost" || url.hostname === "127.0.0.1"; + if ( + !["http:", "https:"].includes(url.protocol) || + (url.protocol !== "https:" && (process.env.NODE_ENV === "production" || !loopback)) || + url.username || + url.password || + url.search || + url.hash || + url.pathname !== "/" + ) { + throw new Error(`${name} must be an HTTPS origin (HTTP localhost only in development)`); + } + return url.origin; +} + +export function oidcSettings() { + const secret = required("AUTH_SECRET"); + if (secret.length < 32) { + throw new Error("AUTH_SECRET must be at least 32 characters"); + } + const issuer = validatedUrl("ZITADEL_ISSUER"); + validatedUrl("AUTH_URL"); + const clientId = required("ZITADEL_CLIENT_ID"); + const clientSecret = required("ZITADEL_CLIENT_SECRET"); + + return { + issuer, + clientId, + clientSecret, + secret, + trustHost: process.env.AUTH_TRUST_HOST === "true" + }; +} diff --git a/users/ui/src/lib/identity.ts b/users/ui/src/lib/identity.ts new file mode 100644 index 0000000..8b0a8e9 --- /dev/null +++ b/users/ui/src/lib/identity.ts @@ -0,0 +1,24 @@ +import "server-only"; + +import { redirect } from "next/navigation"; + +import { auth } from "@/auth"; + +export async function requireIdentity() { + const session = await auth(); + if (!session?.user?.id) { + redirect("/signin"); + } + return session.user; +} + +/** Browser-facing ZITADEL settings link, not an administrative API endpoint. */ +export function zitadelSelfServiceUrl(): string { + const issuer = process.env.ZITADEL_ISSUER; + if (!issuer) { + throw new Error("Missing ZITADEL_ISSUER"); + } + // Validate via a well-defined URL; never accept browser-supplied redirect targets. + const origin = new URL(issuer).origin; + return new URL("/ui/console/users/me", origin).toString(); +} diff --git a/users/ui/src/styles/globals.css b/users/ui/src/styles/globals.css new file mode 100644 index 0000000..cda2b3c --- /dev/null +++ b/users/ui/src/styles/globals.css @@ -0,0 +1,135 @@ +:root { + color-scheme: light dark; + font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; + --bg: #f6f7f9; + --panel: #ffffff; + --panel-subtle: #f9fafb; + --ink: #1c2633; + --muted: #627187; + --line: #e4e9f0; + --accent: #235bdb; + --accent-hover: #194ab5; + --accent-soft: #eaf0ff; + --success: #078060; + --shadow: 0 5px 24px rgba(21, 36, 58, 0.04); +} + +* { box-sizing: border-box; } +html { scroll-behavior: smooth; } +body { margin: 0; background: var(--bg); color: var(--ink); font-size: 14px; line-height: 1.55; } +a { color: inherit; text-decoration: none; } +button { font: inherit; cursor: pointer; } +a:focus-visible, button:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; } +h1, h2, h3, p { margin-top: 0; } +h1 { font-size: clamp(1.65rem, 4vw, 2.05rem); line-height: 1.22; letter-spacing: -.035em; } +h2 { font-size: 1rem; line-height: 1.3; } +h3 { font-size: .9rem; } +p { margin-bottom: 1rem; } +.muted { color: var(--muted); } +.eyebrow { font-size: .68rem; letter-spacing: .14em; color: var(--accent); font-weight: 750; margin-bottom: .8rem; } + +.app-layout { min-height: 100dvh; display: grid; grid-template-columns: 244px minmax(0, 1fr); } +.sidebar { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 15px; padding: 25px 18px 20px; background: var(--panel); border-right: 1px solid var(--line); } +.brand { display: flex; align-items: center; gap: 12px; padding: 0 8px; } +.brand-symbol { display: grid; place-items: center; background: var(--accent); color: #fff; width: 39px; height: 39px; border-radius: 12px; font-weight: 800; font-size: 1.2rem; letter-spacing: -.06em; flex: none; } +.brand-label { display: flex; flex-direction: column; line-height: 1.25; } +.brand-label strong { font-size: 1rem; letter-spacing: -.02em; } +.brand-label small { margin-top: 4px; color: var(--muted); font-size: .73rem; } +.nav-heading { font-size: .66rem; letter-spacing: .12em; color: var(--muted); font-weight: 700; margin: 24px 10px 0; } +.nav-list { display: grid; gap: 5px; } +.nav-item { padding: 10px 12px; border-radius: 9px; display: flex; flex-direction: column; gap: 1px; font-weight: 650; } +.nav-item small { font-size: .73rem; font-weight: 400; color: var(--muted); } +.nav-item:hover, .nav-item:focus-visible { background: var(--accent-soft); } +.sidebar-spacer { flex: 1; } +.identity-tile { display: flex; align-items: center; gap: 9px; padding: 10px; background: var(--panel-subtle); border: 1px solid var(--line); border-radius: 11px; } +.avatar { width: 33px; height: 33px; display: grid; place-items: center; border-radius: 50%; color: var(--accent); background: var(--accent-soft); font-weight: 750; flex: none; } +.identity-text { min-width: 0; display: flex; flex-direction: column; line-height: 1.4; } +.identity-text strong, .identity-text small { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } +.identity-text strong { font-size: .78rem; } +.identity-text small { font-size: .7rem; color: var(--muted); } + +.main-column { min-width: 0; display: flex; flex-direction: column; } +.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 63px; padding: 0 clamp(22px, 4vw, 52px); border-bottom: 1px solid var(--line); background: var(--panel); } +.topbar-label { color: var(--muted); font-size: .8rem; font-weight: 600; } +.status-dot { display: inline-flex; align-items: center; gap: 8px; font-size: .74rem; color: var(--muted); } +.status-dot > span { width: 7px; height: 7px; border-radius: 50%; background: var(--success); } +.main-content { width: 100%; max-width: 1130px; margin: 0 auto; padding: 38px clamp(22px, 4vw, 52px) 70px; flex: 1; } +.content-stack { display: grid; gap: 24px; } +.page-heading { max-width: 690px; } +.page-heading h1 { margin-bottom: 12px; } +.page-heading .muted { margin-bottom: 0; max-width: 630px; } +.overview-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; } +.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 25px; box-shadow: var(--shadow); min-width: 0; } +.card h2 { margin-bottom: 9px; } +.card .muted { font-size: .85rem; } +.card-header { display: flex; justify-content: space-between; align-items: center; gap: 15px; margin-bottom: 10px; } +.action-card { display: flex; flex-direction: column; align-items: flex-start; min-height: 182px; } +.action-card .muted { flex: 1; } +.note-card { background: var(--panel-subtle); border: 1px solid var(--line); border-radius: 14px; padding: 23px 25px; } +.note-card p { color: var(--muted); font-size: .86rem; max-width: 680px; } +.note-card h2 { margin-bottom: 8px; } +.pill { border-radius: 99px; background: var(--accent-soft); padding: 5px 10px; color: var(--accent); white-space: nowrap; font-size: .67rem; font-weight: 650; } +.button { display: inline-flex; gap: 8px; justify-content: center; align-items: center; text-align: center; min-height: 37px; padding: 8px 14px; border-radius: 9px; border: 1px solid transparent; font-weight: 650; font-size: .81rem; transition: background .15s ease, border-color .15s ease; } +.button-primary { color: #fff; background: var(--accent); } +.button-primary:hover { background: var(--accent-hover); } +.button-outline { background: var(--panel); border-color: var(--line); } +.button-outline:hover { background: var(--panel-subtle); border-color: var(--accent); } +.button-subtle { color: var(--ink); background: transparent; border-color: var(--line); } +.button-subtle:hover { background: var(--panel-subtle); } +.button-full { width: 100%; } +.text-link { color: var(--accent); font-weight: 650; font-size: .83rem; } +.text-link:hover { text-decoration: underline; } +.footer { text-align: center; color: var(--muted); font-size: .72rem; padding: 18px; border-top: 1px solid var(--line); } +.data-list { margin: 0; } +.data-row { display: grid; grid-template-columns: minmax(140px, 28%) 1fr; gap: 20px; padding: 16px 0; border-bottom: 1px solid var(--line); } +.data-row:last-child { border-bottom: none; } +.data-row dt { color: var(--muted); } +.data-row dd { margin: 0; overflow-wrap: anywhere; font-weight: 570; } +.form-note, .fine-print { font-size: .76rem; color: var(--muted); line-height: 1.7; } +.setting-list { margin-bottom: 24px; } +.setting-row { display: flex; gap: 15px; justify-content: space-between; align-items: flex-start; border-bottom: 1px solid var(--line); padding: 15px 0; } +.setting-row:last-child { border-bottom: none; } +.setting-row h3 { margin-bottom: 4px; } +.setting-row p { margin: 0; max-width: 520px; } +.sign-in-screen { min-height: 100dvh; display: flex; justify-content: center; align-items: center; padding: 28px; } +.sign-in-card { width: 100%; max-width: 430px; border: 1px solid var(--line); border-radius: 18px; background: var(--panel); padding: 35px; box-shadow: var(--shadow); } +.sign-in-card .brand-symbol { margin-bottom: 26px; } +.sign-in-card h1 { font-size: 1.8rem; margin-bottom: 16px; } +.sign-in-card .muted { margin-bottom: 28px; } +.sign-in-card .fine-print { margin: 16px 0 20px; } + +@media (max-width: 760px) { + .app-layout { display: flex; flex-direction: column; } + .sidebar { position: static; height: auto; gap: 12px; padding: 14px 18px; border-right: none; border-bottom: 1px solid var(--line); } + .brand { padding: 0; } + .nav-heading, .sidebar-spacer, .identity-tile { display: none; } + .nav-list { display: flex; gap: 5px; overflow-x: auto; } + .nav-item { white-space: nowrap; padding: 7px 10px; } + .nav-item small { display: none; } + .sidebar form { align-self: flex-start; } + .topbar { min-height: 48px; padding: 0 18px; } + .main-content { padding: 26px 18px 48px; } + .overview-grid { grid-template-columns: 1fr; } + .card, .note-card { padding: 19px; } + .data-row { grid-template-columns: 1fr; gap: 3px; } + .sign-in-card { padding: 27px; } +} + +@media (prefers-color-scheme: dark) { + :root { + --bg: #10151d; + --panel: #171f2a; + --panel-subtle: #1c2633; + --ink: #eef3fa; + --muted: #a7b5c8; + --line: #2e3a4a; + --accent: #78a6ff; + --accent-hover: #5b8bea; + --accent-soft: #263951; + --success: #33b999; + --shadow: 0 4px 24px rgba(0, 0, 0, .08); + } + .button-primary { background: #346aca; color: #fff; } + .button-primary:hover { background: #2756b2; } + .brand-symbol { background: #346aca; } +} diff --git a/users/ui/src/types/next-auth.d.ts b/users/ui/src/types/next-auth.d.ts new file mode 100644 index 0000000..51536db --- /dev/null +++ b/users/ui/src/types/next-auth.d.ts @@ -0,0 +1,16 @@ +import type { DefaultSession } from "next-auth"; + +declare module "next-auth" { + interface Session { + user: DefaultSession["user"] & { + id: string; + }; + } +} + +declare module "next-auth/jwt" { + interface JWT { + zitadelSubject?: string; + zitadelTokenExpiresAt?: number; + } +} diff --git a/users/ui/tsconfig.json b/users/ui/tsconfig.json new file mode 100644 index 0000000..6de6bbb --- /dev/null +++ b/users/ui/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "target": "ES2017", + "lib": ["dom", "dom.iterable", "esnext"], + "allowJs": false, + "skipLibCheck": true, + "strict": true, + "noEmit": true, + "esModuleInterop": true, + "module": "esnext", + "moduleResolution": "bundler", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "react-jsx", + "incremental": true, + "plugins": [{ "name": "next" }], + "paths": { "@/*": ["./src/*"] } + }, + "include": ["next-env.d.ts", ".next/types/**/*.ts", ".next/dev/types/**/*.ts", "**/*.ts", "**/*.tsx"], + "exclude": ["node_modules"] +}