mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 15:31:16 -07:00
Tweak: read auth providers in-process on the sign-in page, set env earlier (#7023)
This commit is contained in:
@@ -3,7 +3,7 @@ import { createHash, timingSafeEqual } from "node:crypto";
|
||||
import NextAuth from "next-auth";
|
||||
import CredentialsProvider from "next-auth/providers/credentials";
|
||||
|
||||
import { isAuthEnabled } from "utils/env";
|
||||
import { applyNextAuthEnv, isAuthEnabled } from "utils/env";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const MIN_AUTH_SECRET_LENGTH = 32;
|
||||
@@ -12,20 +12,13 @@ const authEnabled = isAuthEnabled();
|
||||
const issuer = process.env.HOMEPAGE_OIDC_ISSUER;
|
||||
const clientId = process.env.HOMEPAGE_OIDC_CLIENT_ID;
|
||||
const clientSecret = process.env.HOMEPAGE_OIDC_CLIENT_SECRET;
|
||||
const homepageAuthSecret = process.env.HOMEPAGE_AUTH_SECRET;
|
||||
const homepageExternalUrl = process.env.HOMEPAGE_EXTERNAL_URL;
|
||||
const homepageAuthPassword = process.env.HOMEPAGE_AUTH_PASSWORD;
|
||||
const homepageAuthPasswordDigest = homepageAuthPassword
|
||||
? createHash("sha256").update(homepageAuthPassword, "utf8").digest()
|
||||
: null;
|
||||
|
||||
// Map HOMEPAGE_* envs to what NextAuth expects
|
||||
if (!process.env.NEXTAUTH_SECRET && homepageAuthSecret) {
|
||||
process.env.NEXTAUTH_SECRET = homepageAuthSecret;
|
||||
}
|
||||
if (!process.env.NEXTAUTH_URL && homepageExternalUrl) {
|
||||
process.env.NEXTAUTH_URL = homepageExternalUrl;
|
||||
}
|
||||
// Also done in instrumentation.js
|
||||
applyNextAuthEnv();
|
||||
|
||||
const defaultScope = process.env.HOMEPAGE_OIDC_SCOPE || "openid email profile";
|
||||
const cleanedIssuer = issuer ? issuer.replace(/\/+$/, "") : issuer;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import classNames from "classnames";
|
||||
import { getProviders, signIn } from "next-auth/react";
|
||||
import { signIn } from "next-auth/react";
|
||||
import { useRouter } from "next/router";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { BiShieldQuarter } from "react-icons/bi";
|
||||
@@ -94,10 +94,8 @@ export default function SignIn({ providers, settings }) {
|
||||
<div className="mx-auto flex h-12 w-12 items-center justify-center rounded-2xl bg-theme-500/15 text-theme-600 dark:text-theme-300">
|
||||
<BiShieldQuarter className="h-6 w-6" />
|
||||
</div>
|
||||
<h1 className="mt-6 text-2xl font-semibold text-gray-900 dark:text-slate-100">
|
||||
Authentication not configured
|
||||
</h1>
|
||||
<p className="mt-3 text-sm text-gray-600 dark:text-slate-400">OIDC is disabled or misconfigured.</p>
|
||||
<h1 className="mt-6 text-2xl font-semibold text-gray-900 dark:text-slate-100">Authentication error</h1>
|
||||
<p className="mt-3 text-sm text-gray-600 dark:text-slate-400">Auth is disabled or misconfigured.</p>
|
||||
</div>
|
||||
</main>
|
||||
</>
|
||||
@@ -202,7 +200,16 @@ export default function SignIn({ providers, settings }) {
|
||||
}
|
||||
|
||||
export async function getServerSideProps(context) {
|
||||
const providers = await getProviders();
|
||||
// Avoid getProviders() fetch
|
||||
let providers = {};
|
||||
try {
|
||||
// Dynamic so a bad config throws in here rather than at page load
|
||||
const { authOptions } = await import("pages/api/auth/[...nextauth]");
|
||||
providers = Object.fromEntries(authOptions.providers.map(({ id, name, type }) => [id, { id, name, type }]));
|
||||
} catch (e) {
|
||||
console.error("Unable to load auth providers: %s", e.message);
|
||||
}
|
||||
|
||||
const homepageSettings = getSettings();
|
||||
const settings = Object.fromEntries(
|
||||
PUBLIC_SIGN_IN_SETTINGS.filter((key) => Object.prototype.hasOwnProperty.call(homepageSettings, key)).map((key) => [
|
||||
|
||||
Reference in New Issue
Block a user