Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to write a rewrite rule to remove the query parameters - Next.js app with proxy to another Next.js app #1931

Unanswered
ThangHuuVu asked this question in Q&A
Discussion options

Hi there 👋, I want to ask about this case for Netlify Proxies:

  1. I have my main app deployed at authjs-with-proxy.netlify.app/
  2. I have my Auth app deployed at https://next-auth-behind-cdn.vercel.app/authjs/ (with custom basePath and trailing slash)
  3. I proxy my /authjs in the main app to the Auth app in the _redirects file: /authjs/* https://next-auth-behind-cdn.vercel.app/authjs/:splat 200!
  4. I log in at authjs-with-proxy.netlify.app/authjs
  5. I got navigated back to authjs-with-proxy.netlify.app/authjs . On the address bar, the query params code and state are 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:

You must be logged in to vote

Replies: 1 comment 1 reply

Comment options

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.

You must be logged in to vote
1 reply
Comment options

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /