-
Couldn't load subscription status.
- Fork 92
How to write a rewrite rule to remove the query parameters - Next.js app with proxy to another Next.js app #1931
-
Hi there 👋, I want to ask about this case for Netlify Proxies:
- I have my main app deployed at authjs-with-proxy.netlify.app/
- I have my Auth app deployed at https://next-auth-behind-cdn.vercel.app/authjs/ (with custom
basePathand trailing slash) - I proxy my /authjs in the main app to the Auth app in the
_redirectsfile:/authjs/* https://next-auth-behind-cdn.vercel.app/authjs/:splat 200! - I log in at authjs-with-proxy.netlify.app/authjs
- I got navigated back to authjs-with-proxy.netlify.app/authjs . On the address bar, the query params
codeandstateare still shown, while I expect it to not shown
image
This is not happening in a normal Auth app, where the query parameters got stripped down after the OAuth callback.
I'm currently removing the query params by using the Next.js router like this:
const router = useRouter(); if (router.query.code && router.query.state) router.replace('/', undefined, { shallow: true });
but I feel this can be achieved using either Netlify rewrites or Next.js rewrites. I've tried:
_redirects file
/authjs/?code=:code&state=:state /authjs/ 200
/authjs/?state=:state&code=:code /authjs/ 200
or in my Auth app's next.config.js:
const isProd = process.env.NODE_ENV === 'production' module.exports = { trailingSlash: true, assetPrefix: isProd ? process.env.NEXT_PUBLIC_ASSET_PREFIX : undefined, basePath: process.env.NEXT_PUBLIC_ASSET_PREFIX, async rewrites() { return [ { source: '/', has: [ { type: 'query', key: 'state', }, { type: 'query', key: 'code', }, ], destination: '/', }, ]; }, }
None of the above works for me, so I would really appreciate any help here 🙏 Thanks for reading!
Repos:
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 1 reply
-
Hey @ThangHuuVu.
I've spoken to the team working on our redirect engine and you can add a ? to the redirect target to suppress the query parameters.
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi, @MarcL thanks for answering me
https://github.com/nextauthjs/auth-with-proxy/blob/main/public/_redirects I updated the ? to the redirect targets but it doesn't seem to work 🤔 And the warning is still shown in the Netlify console.
https://app.netlify.com/sites/authjs-with-proxy/deploys/640685e5bbf7590008e70005
7:32:23 AM: Next.js cache saved.
7:32:23 AM: You have the following Netlify rewrites that might cause conflicts with the Next.js Runtime:
7:32:23 AM:
7:32:23 AM: - /authjs/?code=:code&state=:state /authjs/? 200
7:32:23 AM: - /authjs/?state=:state&code=:code /authjs/? 200
7:32:23 AM:
7:32:23 AM: For more information, see https://ntl.fyi/next-rewrites
Beta Was this translation helpful? Give feedback.