turn on SSO

This commit is contained in:
s871077
2026-09-21 18:42:32 +08:00
parent 137a4a2a39
commit 167eb0807d
12 changed files with 97 additions and 102 deletions
+18
View File
@@ -0,0 +1,18 @@
import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
// Public readiness endpoint (allow-listed in src/auth.config.ts). DB-backed on
// purpose: a pod should only take traffic once MySQL is actually reachable,
// which is what the old /api/years probe gave us before auth was restored.
// force-dynamic keeps Prisma from being invoked during `next build`, which runs
// without a DATABASE_URL in the Dockerfile builder stage.
export const dynamic = "force-dynamic";
export async function GET() {
try {
await prisma.$queryRaw`SELECT 1`;
return NextResponse.json({ status: "ok" });
} catch {
return NextResponse.json({ status: "unavailable" }, { status: 503 });
}
}
+13 -21
View File
@@ -2,9 +2,7 @@ import type { Metadata } from "next";
import "./globals.css";
import TopNav from "@/components/TopNav";
import Sidebar from "@/components/Sidebar";
// --- SSO DISABLED FOR CICD TEST ---
// import { auth } from "@/auth";
// --- END SSO DISABLED ---
import { auth } from "@/auth";
export const metadata: Metadata = {
title: "Global IT Cost Dashboard",
@@ -16,25 +14,19 @@ export default async function RootLayout({
}: {
children: React.ReactNode;
}) {
// --- SSO DISABLED FOR CICD TEST ---
// const session = await auth();
//
// // Unauthenticated (e.g. the /login page) -> render a bare shell with no
// // sidebar/topnav so the sign-in screen shows on its own.
// if (!session?.user) {
// return (
// <html lang="en">
// <body>{children}</body>
// </html>
// );
// }
//
// const isAdmin = session.user.role === "admin";
const session = await auth();
// Hardcoded so the sidebar/topnav still render — auth() would return null
// with SSO off and the whole UI chrome would vanish.
const isAdmin = true;
// --- END SSO DISABLED ---
// Unauthenticated (e.g. the /login page) -> render a bare shell with no
// sidebar/topnav so the sign-in screen shows on its own.
if (!session?.user) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
const isAdmin = session.user.role === "admin";
return (
<html lang="en">
+4 -8
View File
@@ -14,13 +14,11 @@ function errorMessage(code: string | undefined): string | null {
}
}
// --- SSO DISABLED FOR CICD TEST ---
// Server action: kick off the Microsoft SSO flow.
// async function microsoftLogin() {
// "use server";
// await signIn("microsoft-entra-id", { redirectTo: "/" });
// }
// --- END SSO DISABLED ---
async function microsoftLogin() {
"use server";
await signIn("microsoft-entra-id", { redirectTo: "/" });
}
// Server action: local username/password login.
async function credentialsLogin(formData: FormData) {
@@ -65,7 +63,6 @@ export default async function LoginPage({
</div>
)}
{/* --- SSO DISABLED FOR CICD TEST ---
<form action={microsoftLogin} className="mt-6">
<button
type="submit"
@@ -80,7 +77,6 @@ export default async function LoginPage({
OR
<span className="h-px flex-1 bg-[#E5E7EB]" />
</div>
--- END SSO DISABLED --- */}
<form action={credentialsLogin} className="mt-6 flex flex-col gap-3">
<label className="flex flex-col gap-1 text-sm text-[#374151]">
+2 -2
View File
@@ -35,8 +35,8 @@ export const authConfig = {
authorized({ auth, request }) {
const { pathname } = request.nextUrl;
// Always let the sign-in page through, even when unauthenticated.
if (pathname === "/login") return true;
// Public: the sign-in page and the k8s readiness probe.
if (pathname === "/login" || pathname === "/api/health") return true;
// Not signed in -> Auth.js redirects to the sign-in page.
if (!auth?.user) return false;
+24 -36
View File
@@ -1,56 +1,44 @@
// --- SSO DISABLED FOR CICD TEST ---
// import { auth, signIn, signOut } from "@/auth";
// --- END SSO DISABLED ---
import { auth, signIn, signOut } from "@/auth";
export default async function TopNav() {
// --- SSO DISABLED FOR CICD TEST ---
// const session = await auth();
// const user = session?.user;
const user = { name: "CI Test User" };
// --- END SSO DISABLED ---
const session = await auth();
const user = session?.user;
return (
<header className="flex h-14 items-center justify-between bg-[#111827] px-6 text-white">
<span className="text-lg font-semibold">Global IT Cost Dashboard</span>
{/* --- SSO DISABLED FOR CICD TEST ---
Original: `{user ? (<logged-in block/>) : (<login form/>)}`. Both forms
are server actions calling signOut/signIn, which would throw without
the Entra env vars, so only the name is rendered below.
<div className="flex items-center gap-3">
<span className="text-sm text-white/80">{user.name ?? user.email}</span>
<form
action={async () => {
"use server";
await signOut({ redirectTo: "/" });
}}
>
<button
type="submit"
className="rounded-md bg-white/10 px-4 py-1.5 text-sm font-medium hover:bg-white/20"
>
Logout
</button>
</form>
</div>
{user ? (
<div className="flex items-center gap-3">
<span className="text-sm text-white/80">{user.name ?? user.email}</span>
<form
action={async () => {
"use server";
await signIn("microsoft-entra-id");
await signOut({ redirectTo: "/" });
}}
>
<button
type="submit"
className="rounded-md bg-white/10 px-4 py-1.5 text-sm font-medium hover:bg-white/20"
>
Login
Logout
</button>
</form>
--- END SSO DISABLED --- */}
<div className="flex items-center gap-3">
<span className="text-sm text-white/80">{user.name}</span>
</div>
</div>
) : (
<form
action={async () => {
"use server";
await signIn("microsoft-entra-id");
}}
>
<button
type="submit"
className="rounded-md bg-white/10 px-4 py-1.5 text-sm font-medium hover:bg-white/20"
>
Login
</button>
</form>
)}
</header>
);
}
+6 -14
View File
@@ -1,18 +1,10 @@
// --- SSO DISABLED FOR CICD TEST ---
// Original: protect the whole app with role-aware auth in the edge runtime.
// import NextAuth from "next-auth";
// import { authConfig } from "@/auth.config";
//
// export const { auth: middleware } = NextAuth(authConfig);
import NextAuth from "next-auth";
import { authConfig } from "@/auth.config";
// No-op replacement. The EMPTY MATCHER below is the actual kill switch — this
// function is never invoked, and every route (including the three admin pages)
// is open.
export function middleware() {}
// Edge-runtime gate for the whole app. Uses the edge-safe config only —
// src/auth.ts pulls in node:fs via lookupRole and must never be imported here.
export const { auth: middleware } = NextAuth(authConfig);
export const config = {
// Original matcher (restore with the lines above):
// matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.).*)"],
matcher: [],
matcher: ["/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.).*)"],
};
// --- END SSO DISABLED ---