next.config.js Kinde Env Vars
I'm trying to set some of my KINDE environment variables in my next.config.js file in order to support preview deployments. Here's a minimal example:
I do not have any of the 3 env variables set elsewhere
When I visit my application, I get the following error:
This works if I set the variables through a .env file but my understanding from https://kinde.com/docs/developer-tools/nextjs-sdk/#working-with-preview-urls is that it should work without previously having set the values.
Kinde Docs
NextJS App Router SDK v2 - Developer tools - Help center
Our developer tools provide everything you need to get started with Kinde.
13 Replies
In your next.config.js file, you've hardcoded the values for these environment variables. However, the Kinde documentation suggests a slightly different approach for handling preview URLs, especially when deploying on platforms like Vercel that generate dynamic URLs for preview deployments. The documentation recommends using conditional logic to set these environment variables based on whether the VERCEL_URL environment variable is present, which would indicate a Vercel preview deployment.
Here's an example based on the Kinde documentation:
const nextConfig = {
env: {
KINDE_SITE_URL: process.env.KINDE_SITE_URL ??
https://${process.env.VERCEL_URL}
,
KINDE_POST_LOGOUT_REDIRECT_URL: process.env.KINDE_POST_LOGOUT_REDIRECT_URL ?? https://${process.env.VERCEL_URL}
,
KINDE_POST_LOGIN_REDIRECT_URL: process.env.KINDE_POST_LOGIN_REDIRECT_URL ?? https://${process.env.VERCEL_URL}/dashboard
,
},
};
This configuration uses the ?? operator to fall back to the VERCEL_URL environment variable if the specific Kinde environment variables are not set. This approach ensures that your application can dynamically adjust the URLs based on the deployment environment, which is particularly useful for handling Vercel's preview URLs.
If you're not deploying on Vercel or if VERCEL_URL is not set for some reason, you'll need to ensure that the environment variables are set through some other means, such as a .env.local file or directly in your deployment environment's configuration.
Given that setting the
variables through a .env file works for you, it's possible that the environment variables are not being correctly read from next.config.js in your deployment environment or during local development. Double-check that your deployment environment supports reading from next.config.js for environment variables and that there are no typos or errors in your configuration file.
https://kinde.com/docs/developer-tools/nextjs-sdk/#working-with-preview-urlsKinde Docs
NextJS App Router SDK v2 - Developer tools - Help center
Our developer tools provide everything you need to get started with Kinde.
Let me know if this works for you
Thanks. That's similar to the logic I am trying (not using vercel, deploying to AWS amplify so it's a bit more complicated), but to get down to the root of the problem, i wanted a minimal illustration, which is what i posted.
Thanks for clarifying @jessebs I will get a more experienced team member to help out here for AWS Amplify
Hi @jessebs apologies for the trouble, I believe a url may be missing from the config (will get it added to the docs) - would you be able to try this:
Hi Viv,
No - i still get the same error. I previously had KINDE_ISSUER_URL set through an environment variable, which is why it wasn't part of the example.
@kinde-oss/[email protected] [email protected] (pages router)
@kinde-oss/[email protected] [email protected] (pages router)
Gotcha, can I check if your
api/auth/[...kindeAuth]/route.ts
page looks like this?
thanksWe're using the pages router so it looks a bit different. it's at
api/auth/[...kindeAuth].ts
Hey @jessebs,
Are you still experiencing issues?
Yes but we've worked around it in our solution
Hey @jessebs,
Would you mind sharing how you worked around this solution?
We’ve basically moved the logic of determining urls into our build pipeline
Okay noted.
Great to hear you have managed to build a workaround, my team are looking into this issue so other customers won't experience this issue in the NextJS Pages Router SDK