feat(frontend): server-side Zitadel OIDC auth (part 2)

Add Authorization Code + PKCE login/logout against Zitadel, handled entirely
server-side with an httpOnly signed session cookie:
- $lib/server/auth: openid-client discovery (config cached), jose-signed
  session encode/decode, cookie/name constants
- routes: /login (builds the authorize URL with PKCE/state/nonce, ?mode=register
  -> prompt=create), /auth/callback (code exchange + session cookie),
  /logout (clears session + Zitadel end_session)
- hooks.server populates locals.user/accessToken from the session
- +layout.server exposes the user; Navbar shows the signed-in user + Salir,
  or Iniciar sesión / Registrarse
- switch to adapter-node; env-driven config (.env gitignored, .env.example
  committed) with issuer http://host.docker.internal:8080

Verified: svelte-check + build clean; dev smoke test shows the landing in the
logged-out state and /login issuing a 302 to Zitadel's authorize endpoint with
PKCE/state/nonce (discovery reachable). Real client id/secret still needed to
complete the token exchange.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-05 22:58:47 +02:00
co-authored by Claude Opus 4.8
parent 6bb31ad7c5
commit 0163da096e
14 changed files with 979 additions and 119 deletions
+20
View File
@@ -0,0 +1,20 @@
import { redirect } from '@sveltejs/kit';
import * as client from 'openid-client';
import type { RequestHandler } from './$types';
import { getOidcConfig, authConfig, decodeSession, SESSION_COOKIE } from '$lib/server/auth';
const handleLogout: RequestHandler = async ({ cookies }) => {
const session = await decodeSession(cookies.get(SESSION_COOKIE));
cookies.delete(SESSION_COOKIE, { path: '/' });
const config = await getOidcConfig();
const endSessionUrl = client.buildEndSessionUrl(config, {
post_logout_redirect_uri: authConfig.postLogoutUri,
...(session?.idToken ? { id_token_hint: session.idToken } : {})
});
redirect(302, endSessionUrl.href);
};
export const GET = handleLogout;
export const POST = handleLogout;