Skip to content

Commit fed34f7

Browse files
committed
fix: auth configuration
1 parent 2a910dd commit fed34f7

File tree

3 files changed

+26
-63
lines changed

3 files changed

+26
-63
lines changed

dev-auth/oidc-provider.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const configuration = {
2222
redirect_uris: [
2323
// Better Auth genericOAuth uses /oauth2/callback/:providerId
2424
"http://localhost:3000/api/auth/oauth2/callback/oidc",
25-
"http://localhost:3001/api/auth/oauth2/callback/oidc",
25+
"http://localhost:3000/api/auth/oauth2/callback/oidc",
2626
"http://localhost:3002/api/auth/oauth2/callback/oidc",
2727
"http://localhost:3003/api/auth/oauth2/callback/oidc",
2828
],

src/app/login/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
import { authClient } from "@/lib/auth-client";
44

5+
const OIDC_PROVIDER_ID = process.env.NEXT_PUBLIC_OIDC_PROVIDER_ID || "oidc";
6+
const OIDC_PROVIDER_NAME =
7+
process.env.NEXT_PUBLIC_OIDC_PROVIDER_NAME || "OIDC Provider";
8+
59
export default function LoginPage() {
610
const handleOIDCLogin = async () => {
711
try {
812
console.log("Initiating OIDC sign-in...");
913
const { data, error } = await authClient.signIn.oauth2({
10-
providerId: "oidc",
14+
providerId: OIDC_PROVIDER_ID,
1115
callbackURL: "/dashboard",
1216
});
1317

@@ -43,7 +47,7 @@ export default function LoginPage() {
4347
onClick={handleOIDCLogin}
4448
className="rounded-full bg-black px-8 py-3 text-white transition-colors hover:bg-zinc-800 dark:bg-white dark:text-black dark:hover:bg-zinc-200"
4549
>
46-
Sign In with OIDC
50+
Sign In with {OIDC_PROVIDER_NAME}
4751
</button>
4852
</main>
4953
</div>

src/lib/auth.ts

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,43 @@
11
import { betterAuth } from "better-auth";
22
import { genericOAuth } from "better-auth/plugins";
33

4-
// Read from environment variables to support any OIDC provider
5-
const OIDC_ISSUER = process.env.OIDC_ISSUER_URL || "";
6-
const OIDC_CLIENT_ID = process.env.OIDC_CLIENT_ID || "";
7-
const OIDC_CLIENT_SECRET = process.env.OIDC_CLIENT_SECRET || "";
8-
const BETTER_AUTH_SECRET =
9-
process.env.BETTER_AUTH_SECRET || "build-time-placeholder";
10-
const BETTER_AUTH_URL = process.env.BETTER_AUTH_URL || "http://localhost:3000";
4+
const OIDC_PROVIDER_ID = process.env.OIDC_PROVIDER_ID || "oidc";
5+
const OIDC_ISSUER = process.env.OIDC_ISSUER || "";
6+
const BASE_URL = process.env.BETTER_AUTH_URL || "http://localhost:3000";
117

12-
// Validate required environment variables (warnings only during build)
13-
const isBuild = process.env.NEXT_PHASE === "phase-production-build";
14-
15-
if (!process.env.BETTER_AUTH_SECRET) {
16-
const message =
17-
"[Better Auth] BETTER_AUTH_SECRET is required. Set it in .env.local to a strong, random value.";
18-
if (isBuild) {
19-
console.warn(message);
20-
} else {
21-
throw new Error(message);
22-
}
23-
}
24-
25-
if (!OIDC_ISSUER || !OIDC_CLIENT_ID || !OIDC_CLIENT_SECRET) {
26-
const message =
27-
"[Better Auth] OIDC configuration is incomplete. Set OIDC_ISSUER_URL, OIDC_CLIENT_ID, and OIDC_CLIENT_SECRET in .env.local";
28-
if (isBuild) {
29-
console.warn(message);
30-
} else {
31-
throw new Error(message);
32-
}
33-
}
34-
35-
console.log("[Better Auth] OIDC Configuration:", {
36-
issuer: OIDC_ISSUER,
37-
clientId: OIDC_CLIENT_ID,
38-
baseURL: BETTER_AUTH_URL,
39-
discoveryUrl: `${OIDC_ISSUER}/.well-known/openid-configuration`,
40-
callbackURL: `${BETTER_AUTH_URL}/api/auth/oauth2/callback/oidc`,
41-
});
42-
43-
// Configure trusted origins - defaults to localhost ports for development
44-
// Set TRUSTED_ORIGINS environment variable for production (comma-separated list)
458
const trustedOrigins = process.env.TRUSTED_ORIGINS
46-
? process.env.TRUSTED_ORIGINS.split(",").map((origin) => origin.trim())
47-
: [
48-
"http://localhost:3000",
49-
"http://localhost:3001",
50-
"http://localhost:3002",
51-
"http://localhost:3003",
52-
];
9+
? process.env.TRUSTED_ORIGINS.split(",").map((s) => s.trim())
10+
: [BASE_URL, "http://localhost:3002", "http://localhost:3003"];
5311

54-
// Always include BETTER_AUTH_URL if not already present
55-
if (BETTER_AUTH_URL && !trustedOrigins.includes(BETTER_AUTH_URL)) {
56-
trustedOrigins.push(BETTER_AUTH_URL);
12+
if (!trustedOrigins.includes(BASE_URL)) {
13+
trustedOrigins.push(BASE_URL);
5714
}
5815

5916
export const auth = betterAuth({
60-
secret: BETTER_AUTH_SECRET,
61-
baseURL: BETTER_AUTH_URL,
17+
secret: process.env.BETTER_AUTH_SECRET || "build-time-placeholder",
18+
baseURL: BASE_URL,
6219
trustedOrigins,
63-
// No database configuration - running in stateless mode
6420
session: {
65-
expiresIn: 60 * 60 * 24 * 7, // 7 days
21+
expiresIn: 60 * 60 * 24 * 7,
6622
cookieCache: {
6723
enabled: true,
68-
maxAge: 30 * 24 * 60 * 60, // 30 days cache duration
69-
strategy: "jwe", // Use encrypted tokens for better security
70-
refreshCache: true, // Enable stateless refresh
24+
maxAge: 5 * 60,
7125
},
7226
},
7327
plugins: [
7428
genericOAuth({
7529
config: [
7630
{
77-
providerId: "oidc",
31+
providerId: OIDC_PROVIDER_ID,
7832
discoveryUrl: `${OIDC_ISSUER}/.well-known/openid-configuration`,
79-
clientId: OIDC_CLIENT_ID,
80-
clientSecret: OIDC_CLIENT_SECRET,
33+
authorizationUrl: `${OIDC_ISSUER}/v1/authorize`,
34+
tokenUrl: `${OIDC_ISSUER}/v1/token`,
35+
userInfoUrl: `${OIDC_ISSUER}/v1/userinfo`,
36+
redirectURI: `${BASE_URL}/api/auth/oauth2/callback/${OIDC_PROVIDER_ID}`,
37+
clientId: process.env.OIDC_CLIENT_ID || "",
38+
clientSecret: process.env.OIDC_CLIENT_SECRET || "",
8139
scopes: ["openid", "email", "profile"],
40+
pkce: false,
8241
},
8342
],
8443
}),

0 commit comments

Comments
 (0)