Infinite redirect with middleware and basePath
See original GitHub issueQuestion 💬
I have a NextJS app setup to run under the /admin
basepath. Whenever I use the middleware, I am having problems with it sending me to the wrong url for the login page. If I try passing a custom sign in page to the options, it redirects me infinitely. Can anyone point me in the right direction? Is this even possible?
How to reproduce ☕️
I am running next-auth@4.3.1
, next@12.1.0
_app.js
import { SessionProvider } from 'next-auth/react';
import '../styles/globals.css';
function App({ Component, pageProps: { session, ...pageProps } }) {
return (
<SessionProvider
session={session}
basePath="/admin/api/auth"
>
<Component {...pageProps} session={session} />
</SessionProvider>
);
}
export default App;
_middleware.js
Note that without the custom signin page, the app redirects me to
/api/auth/signin
which does not exists (I can see the page if I manually go to/admin/api/auth/signin
) and with the custom signin page I get an infinite redirect error.
import withAuth from 'next-auth/middleware';
export default withAuth({
pages: {
signIn: '/admin/signin',
},
});
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
env: {
COGNITO_CLIENT_ID: '<removed>',
COGNITO_CLIENT_SECRET: '<removed>',
COGNITO_ISSUER: '<removed>',
COGNITO_DOMAIN: '<removed>',
NEXTAUTH_URL: 'http://localhost:3000/admin/api/auth',
NEXTAUTH_SECRET: '<removed>',
},
basePath: '/admin',
};
module.exports = nextConfig;
pages/signin.js
import { Button } from '@mantine/core';
import { signIn } from 'next-auth/react';
export default function SignIn({ providers }) {
return (
<Button onClick={() => signIn('cognito')}>
Sign in with Cognito
</Button>
);
}
pages/api/auth
import NextAuth from 'next-auth/next';
import CognitoProvider from 'next-auth/providers/cognito';
export default NextAuth({
providers: [
CognitoProvider({
clientId: process.env.COGNITO_CLIENT_ID,
clientSecret: process.env.COGNITO_CLIENT_SECRET,
issuer: process.env.COGNITO_ISSUER,
domain: process.env.COGNITO_DOMAIN,
}),
],
secret: process.env.NEXTAUTH_SECRET,
debug: true,
});
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Infinite redirection using middleware - Stack Overflow
The infinite loop is from trying to redirect to the login route, and always hitting this code: else if (to.meta.middleware == "guest") {...
Read more >URL Rewriting Middleware in ASP.NET Core - Microsoft Learn
NET Core's URL Rewriting Middleware is capable of meeting the need for ... It's easy to accidentally create a loop of infinite redirects....
Read more >next.config.js: Redirects
Redirects allow you to redirect an incoming request path to a different destination path. To use Redirects you can use the redirects key...
Read more >Customizing Server configuration | LoopBack Documentation
Configure the API Explorer. Disable redirect to API Explorer · Use a self-hosted API Explorer · Customize CORS · Express settings · Configure...
Read more >express-deep-link - npm Package Health Analysis - Snyk
Provides deep linking capabilities via express middleware. ... Why Does deep link Guard Against Infinite Redirects?
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I have the same issue by just using
export { default } from "next-auth/middleware"
in my middleware.@nicks6853 thanks for reaching out. So this should theoretically be possible. I was successfully able to recreate and reproduce your infinite refresh / forwarding problem.
I think I’ve boiled it down to
packages/next-auth/src/next/middleware.ts:L65-L68
- this check isn’t being hit and so its not early returning. Therefore its continuing on until a few lines further down it appends another copy of thecallbackUrl
query param and redirect’s to thesignInUrl
over and over again…Unfortunately I haven’t been abel to get any further tonight. I’ll take another look at this tomorrow 👍