refactor: Enhance cookie handling and error management in authentication
Some checks failed
CI / skip-ci-check (pull_request) Successful in 1m23s
CI / lint-and-type-check (pull_request) Failing after 1m44s
CI / test (pull_request) Successful in 1m52s
CI / build (pull_request) Successful in 1m52s
CI / secret-scanning (pull_request) Successful in 1m24s
CI / dependency-scan (pull_request) Successful in 1m29s
CI / sast-scan (pull_request) Successful in 2m29s
CI / workflow-summary (pull_request) Successful in 1m22s
Some checks failed
CI / skip-ci-check (pull_request) Successful in 1m23s
CI / lint-and-type-check (pull_request) Failing after 1m44s
CI / test (pull_request) Successful in 1m52s
CI / build (pull_request) Successful in 1m52s
CI / secret-scanning (pull_request) Successful in 1m24s
CI / dependency-scan (pull_request) Successful in 1m29s
CI / sast-scan (pull_request) Successful in 2m29s
CI / workflow-summary (pull_request) Successful in 1m22s
- Updated `proxy.ts` to explicitly define cookie names based on the request protocol, improving clarity in cookie management. - Refactored `auth.ts` to always throw an error for missing `NEXTAUTH_SECRET` at runtime, ensuring critical configuration is validated consistently.
This commit is contained in:
parent
9c4db74fd1
commit
19d5b7ef99
@ -9,14 +9,9 @@ import { logger } from "./logger"
|
|||||||
function getNextAuthSecret(): string {
|
function getNextAuthSecret(): string {
|
||||||
const secret = process.env.NEXTAUTH_SECRET
|
const secret = process.env.NEXTAUTH_SECRET
|
||||||
if (!secret) {
|
if (!secret) {
|
||||||
// Only throw in non-build contexts (runtime)
|
// Always throw at runtime - this is a critical configuration error
|
||||||
// During build, Next.js might not have env vars available
|
|
||||||
if (process.env.NEXT_PHASE !== "phase-production-build") {
|
|
||||||
throw new Error("NEXTAUTH_SECRET is not set. Define it to enable authentication.")
|
throw new Error("NEXTAUTH_SECRET is not set. Define it to enable authentication.")
|
||||||
}
|
}
|
||||||
// Return a placeholder during build - will fail at runtime if not set
|
|
||||||
return "build-time-placeholder"
|
|
||||||
}
|
|
||||||
return secret
|
return secret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
proxy.ts
8
proxy.ts
@ -13,12 +13,14 @@ export async function proxy(request: NextRequest) {
|
|||||||
|
|
||||||
// Get token (works in Edge runtime)
|
// Get token (works in Edge runtime)
|
||||||
// For HTTPS, NextAuth adds __Secure- prefix automatically
|
// For HTTPS, NextAuth adds __Secure- prefix automatically
|
||||||
// Don't specify cookieName - let getToken auto-detect the correct cookie name
|
// getToken should handle the prefix, but we specify the base name
|
||||||
// It will automatically look for both prefixed and non-prefixed versions
|
const isHttps = request.url.startsWith("https://")
|
||||||
|
const cookieName = isHttps ? `__Secure-authjs.session-token` : `authjs.session-token`
|
||||||
|
|
||||||
const token = await getToken({
|
const token = await getToken({
|
||||||
req: request,
|
req: request,
|
||||||
secret: process.env.NEXTAUTH_SECRET,
|
secret: process.env.NEXTAUTH_SECRET,
|
||||||
// Don't specify cookieName - getToken will auto-detect __Secure- prefix for HTTPS
|
cookieName: cookieName,
|
||||||
})
|
})
|
||||||
|
|
||||||
// User activity logging - track all page visits and API calls
|
// User activity logging - track all page visits and API calls
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user