This commit is contained in:
simon.franken
2026-02-16 16:09:07 +01:00
parent d200254783
commit a9228d19c8
8 changed files with 226 additions and 105 deletions

View File

@@ -1,7 +1,13 @@
import { Router } from 'express';
import { initializeOIDC, createAuthSession, getAuthorizationUrl, handleCallback, getUserInfo } from '../auth/oidc';
import { syncUser } from '../middleware/auth';
import type { AuthenticatedRequest } from '../types';
import { Router } from "express";
import {
initializeOIDC,
createAuthSession,
getAuthorizationUrl,
handleCallback,
getUserInfo,
} from "../auth/oidc";
import { syncUser } from "../middleware/auth";
import type { AuthenticatedRequest } from "../types";
const router = Router();
@@ -16,71 +22,74 @@ async function ensureOIDC() {
}
// GET /auth/login - Initiate OIDC login flow
router.get('/login', async (req, res) => {
router.get("/login", async (req, res) => {
try {
await ensureOIDC();
const session = createAuthSession();
req.session.oidc = session;
const authorizationUrl = getAuthorizationUrl(session);
res.redirect(authorizationUrl);
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ error: 'Failed to initiate login' });
console.error("Login error:", error);
res.status(500).json({ error: "Failed to initiate login" });
}
});
// GET /auth/callback - OIDC callback handler
router.get('/callback', async (req, res) => {
router.get("/callback", async (req, res) => {
try {
await ensureOIDC();
const oidcSession = req.session.oidc;
if (!oidcSession) {
res.status(400).json({ error: 'Invalid session' });
res.status(400).json({ error: "Invalid session" });
return;
}
const tokenSet = await handleCallback(req.query as Record<string, string>, oidcSession);
const tokenSet = await handleCallback(
req.query as Record<string, string>,
oidcSession,
);
const user = await getUserInfo(tokenSet);
// Sync user with database
await syncUser(user);
// Store user in session
req.session.user = user;
delete req.session.oidc;
// Redirect to frontend
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:5173';
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:5173";
res.redirect(`${frontendUrl}/auth/callback?success=true`);
} catch (error) {
console.error('Callback error:', error);
const frontendUrl = process.env.FRONTEND_URL || 'http://localhost:5173';
console.error("Callback error:", error);
const frontendUrl = process.env.FRONTEND_URL || "http://localhost:5173";
res.redirect(`${frontendUrl}/auth/callback?error=authentication_failed`);
}
});
// POST /auth/logout - End session
router.post('/logout', (req: AuthenticatedRequest, res) => {
router.post("/logout", (req: AuthenticatedRequest, res) => {
req.session.destroy((err) => {
if (err) {
res.status(500).json({ error: 'Failed to logout' });
res.status(500).json({ error: "Failed to logout" });
return;
}
res.clearCookie('connect.sid');
res.json({ message: 'Logged out successfully' });
res.clearCookie("connect.sid");
res.json({ message: "Logged out successfully" });
});
});
// GET /auth/me - Get current user
router.get('/me', (req: AuthenticatedRequest, res) => {
router.get("/me", (req: AuthenticatedRequest, res) => {
if (!req.session?.user) {
res.status(401).json({ error: 'Not authenticated' });
res.status(401).json({ error: "Not authenticated" });
return;
}
res.json(req.session.user);
});
export default router;
export default router;