Add OIDC dashboard auth (#1020)

This commit is contained in:
Walter Cheng
2026-05-12 09:43:42 +07:00
committed by GitHub
parent a48fa4eb21
commit c3d91b019b
14 changed files with 1016 additions and 67 deletions
+75 -27
View File
@@ -9,6 +9,9 @@ export default function LoginPage() {
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [hasPassword, setHasPassword] = useState(null);
const [authMode, setAuthMode] = useState("password");
const [oidcConfigured, setOidcConfigured] = useState(false);
const [oidcLoginLabel, setOidcLoginLabel] = useState("Sign in with OIDC");
const router = useRouter();
useEffect(() => {
@@ -18,7 +21,7 @@ export default function LoginPage() {
const baseUrl = typeof window !== "undefined" ? window.location.origin : "";
try {
const res = await fetch(`${baseUrl}/api/settings`, {
const res = await fetch(`${baseUrl}/api/auth/status`, {
signal: controller.signal,
});
clearTimeout(timeoutId);
@@ -31,6 +34,9 @@ export default function LoginPage() {
return;
}
setHasPassword(!!data.hasPassword);
setAuthMode(data.authMode || "password");
setOidcConfigured(data.oidcConfigured === true);
setOidcLoginLabel(data.oidcLoginLabel || "Sign in with OIDC");
} else {
// Safe fallback on non-OK response to avoid infinite loading state.
setHasPassword(true);
@@ -69,6 +75,13 @@ export default function LoginPage() {
}
};
const handleOidcLogin = () => {
window.location.href = "/api/auth/oidc/start";
};
const oidcAvailable = oidcConfigured && ["oidc", "both"].includes(authMode);
const passwordAvailable = authMode !== "oidc" || !oidcConfigured;
// Show loading state while checking password
if (hasPassword === null) {
return (
@@ -88,37 +101,72 @@ export default function LoginPage() {
<div className="relative z-10 w-full max-w-md">
<div className="text-center mb-8">
<h1 className="text-3xl font-bold text-primary mb-2">9Router</h1>
<p className="text-text-muted">Enter your password to access the dashboard</p>
<p className="text-text-muted">
{authMode === "oidc" && oidcConfigured
? "Sign in with your OIDC provider to access the dashboard"
: "Enter your password to access the dashboard"}
</p>
</div>
<Card>
<form onSubmit={handleLogin} className="flex flex-col gap-4">
<div className="flex flex-col gap-2">
<label className="text-sm font-medium">Password</label>
<Input
type="password"
placeholder="Enter password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
autoFocus
/>
{error && <p className="text-xs text-red-500">{error}</p>}
</div>
<div className="flex flex-col gap-4">
{oidcAvailable && (
<Button type="button" variant="primary" className="w-full" onClick={handleOidcLogin}>
{oidcLoginLabel}
</Button>
)}
<Button
type="submit"
variant="primary"
className="w-full"
loading={loading}
>
Login
</Button>
{oidcAvailable && passwordAvailable && <div className="h-px bg-border/60" />}
<p className="text-xs text-center text-text-muted mt-2">
Default password is <code className="bg-sidebar px-1 rounded">123456</code>
</p>
</form>
{passwordAvailable ? (
<form onSubmit={handleLogin} className="flex flex-col gap-4">
{((authMode === "oidc" && !oidcConfigured) || (authMode === "both" && !oidcConfigured)) && (
<p className="text-xs text-amber-600 dark:text-amber-400 text-center">
OIDC login is enabled, but the issuer/client fields are not configured yet. Password login is still available for recovery.
</p>
)}
{authMode === "both" && oidcConfigured && (
<p className="text-xs text-text-muted text-center">
Password and OIDC login are both enabled.
</p>
)}
<div className="flex flex-col gap-2">
<label className="text-sm font-medium">Password</label>
<Input
type="password"
placeholder="Enter password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
autoFocus={!oidcAvailable}
/>
{error && <p className="text-xs text-red-500">{error}</p>}
</div>
<Button
type="submit"
variant="primary"
className="w-full"
loading={loading}
>
Login
</Button>
<p className="text-xs text-center text-text-muted mt-2">
Default password is <code className="bg-sidebar px-1 rounded">123456</code>
</p>
{hasPassword === false && (
<p className="text-xs text-center text-text-muted">
No custom password is set yet. The default password above will work until you change it.
</p>
)}
</form>
) : (
error && <p className="text-xs text-red-500">{error}</p>
)}
</div>
</Card>
</div>
</div>