Closed nickzelei closed 9 months ago
Hi @nickzelei - thanks for raising this
Originally I thought this was an issue with Next.js trying to statically render an API route when it used a Dynamic function.
But I see the SDK is trying to get config before Next.js can bail out of static rendering the route. I don't think this is going to get fixed on the Next.js end, so I'll leave this open to investigate a solution.
In the meantime, you can use your workaround of providing dummy values to the build.
+1 running into this. My builders are spread across different environments so ideally I'd like to avoid remembering to update the environments everywhere. Working around this right now by just having some default environment variables everywhere. Specifically impacting APIs using the App Router and withApiAuthRequired
.
nextjs-auth0 version - 3.0.1 Next.js version - 13.4.12 Node.js version - 18.7.1
Perhaps this bug is related to the following pull request (https://github.com/vercel/next.js/pull/54203), which was fixed in version v13.4.19.
I fixed this by calling handleAuth
inside the route handler as such:
export const GET = async (req, ctx) => {
const response = await handleAuth()(req, ctx);
return response;
};
You can avoid calling it each time by lazy storing it in scope:
import { handleAuth, type AppRouteHandlerFn } from "@auth0/nextjs-auth0";
let _handleAuth: AppRouteHandlerFn | undefined;
export const GET: AppRouteHandlerFn = async (req, ctx) => {
if (typeof _handleAuth === "undefined") {
_handleAuth = handleAuth() as AppRouteHandlerFn;
}
const response = await _handleAuth(req, ctx);
return response;
};
It makes sense that lazy loading it will fix it as the environment variables won't be access until they need to be (which is how it should be imo 😄 ) - however, I'm not sure putting the handleAuth
inside of a GET
function is the way to go here. handleAuth
is intended to be used as a spread and offers more than just GET methods. Most of the auth methods are POST I believe.
While it might compile, I have doubts that it would work as intended.
These two are mutual:
export const GET = x();
export const GET = (...args) => x()(...args);
Except, of course, that the x()
is called in runtime. At the end of the day handleAuth
returns the same NextJS route handler (see here). So it's all GET requests. The spread is used to catch multiple routes (/login
,/logout
, /callback
, etc.) but not the HTTP method.
Yeah I'm running into this when pushing to Azure Static Web Apps as well... This seems ridiculous to check for a secret during CI...
Implementing the fix from @g12i worked, Thanks!!
still encounter the error
My app/api/auth/[auth0]/route.js
import {
handleAuth,
handleLogin,
handleLogout,
handleProfile
} from "@auth0/nextjs-auth0";
let _handleAuth;
export const GET = async (req, ctx) => {
if (!_handleAuth) {
_handleAuth = handleAuth;
}
const response = await _handleAuth({
login: handleLogin({
authorizationParams: {
audience: process.env.AUTH0_AUDIENCE,
// If you want to check the authorization server for if you have a
// session, you need to visit it with a login (you can use `prompt=none``
// to fail silently if the user doesn't have a session).
prompt: "login",
scope: "email offline_access openid profile readwrite:any"
},
returnTo: "/incidents"
}),
logout: handleLogout(() => {
return {
returnTo: "/"
};
}),
profile: handleProfile({ refetch: true }),
onError(req, error) {
console.error("An Auth0 error occured:", error);
}
})(req, ctx);
return response;
};
@g12i's fix does fix the TypeError: "secret" is required
error originating from api/auth/[auth0]/route.ts
but I'm still seeing the error for pre-rendered pages:
Error occurred prerendering page "/dashboard". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: "secret" is required
Hi all 👋 - I've published an experimental tag that defers reading the configuration of the SDK until request time, which allows the SDK to instruct Next.js to bail out of static rendering before the config is read - full pr is https://github.com/auth0/nextjs-auth0/pull/1541 (interesting bit is https://github.com/auth0/nextjs-auth0/commit/bec7d6c5998ba06ae87cb906235d7568bab2418f#diff-c3095d5010e65c52737a98a5d618ea24049ebe90c8470752426081d70ed6e012)
@nickzelei - I've tested it on your repo and confirm that the project builds successfully without the .env.local
I'd be grateful if a few other people who have requested this tried it out too and let me know if they experience any issues.
You can test the experimental tag by installing the SDK like so npm i @auth0/nextjs-auth0@experimental-lazy-conf
hey @adamjmcgrath your changes seem to allow a build now.
I reproduced by:
git clone git@github.com:nickzelei/auth0-bug.git && cd auth0-bug
npm install
npm run build
<-- failnpm i @auth0/nextjs-auth0@experimental-lazy-conf
npm run build
<-- successAwesome!
@adamjmcgrath, thank you for resolving the previous issue.
Retracting my comment as this thread is unrelated to my concern.
Checklist
Description
I'm running
next build
against a fresh repo generated with the create next app and am running into build errors due to missing Auth0 environment variables.It seems that this is because I don't have a
.env.local
with the environment variables specified there. Why do I need this? These will be provided at runtime, so am confused as to why they are failing build steps.I followed these two issues which seem to have fixed this, but I am not seeing that.
Reproduction
/app/api/auth/[auth0]/route.ts
file with the recommended setup.npm run build
You can skip the first two steps by cloning my repo here: https://github.com/nickzelei/auth0-bug Running
npm run build
results in an error due to missing environment variables.This can be fixed by adding a basic
.env.local
fileAdditional context
No response
nextjs-auth0 version
3.1.0
Next.js version
13.4.13
Node.js version
18.16.0