basePath
is constructed incorrectly, resulting in invalid URLs
#10234
Replies: 10 comments 10 replies
-
I can also confirm this issue with similiar setup. I have set The logs show something like this:
I tried following the conversations in #10094 but still don't understand this behaviour. Maybe @ndom91 can explain? |
Beta Was this translation helpful? Give feedback.
-
Hey folks, so the default basePath with next-auth is Does it help to remove that from your config? |
Beta Was this translation helpful? Give feedback.
-
The docs show that the default is However removing it from the config results in the same error: Being redirected to |
Beta Was this translation helpful? Give feedback.
-
I'm getting the same error
|
Beta Was this translation helpful? Give feedback.
-
Hey folks, so as far as i can tell, all of these come down to a bug in the way basePath and AUTH_URL were handled together A fix was recently merged, but isn't released yet (#10094 + sister PRs), I'll post again once a release is out. If anyone wants to try to install github |
Beta Was this translation helpful? Give feedback.
-
/** @type {import('next').NextConfig} */ export default nextConfig; |
Beta Was this translation helpful? Give feedback.
-
"And by omitting AUTH_URL completely I get redirected to the Docker internal hostname of 0.0.0.0 and the redirect url is invalid" We are having this issue, and I found this thread. Has the fix mentioned above been put into a release anywhere so we can see if it fixes our specific issue? If not, I'm not sure how I can try the fix but I'd like to. |
Beta Was this translation helpful? Give feedback.
-
Hi, I am still having issues. I am using Nextjs14 app router. I am using
//auth.config.ts
import type { NextAuthConfig } from 'next-auth';
export const authConfig = {
pages: {
signIn: '/login',
},
basePath: process.env.NGINX_SUBPATH_ROUTE || '',
callbacks: {
authorized({ auth, request: { nextUrl } }) {
// const basePath = process.env.NGINX_SUBPATH_ROUTE || '';
const isLoggedIn = !!auth?.user;
const isNotInLogIn = !nextUrl.pathname.startsWith('/login');
if (isNotInLogIn) {
if (isLoggedIn) return true;
return Response.redirect(new URL('/login', nextUrl)); // Redirect unauthenticated users to login page
} else if (isLoggedIn) {
return Response.redirect(new URL('/', nextUrl));
}
return true;
},
},
providers: [], // Add providers with an empty array for now
trustHost: true,
} satisfies NextAuthConfig; // 'auth.ts';
import NextAuth, { type DefaultSession } from 'next-auth';
import { authConfig } from './auth.config';
import Credentials from 'next-auth/providers/credentials';
import { LoginVariables, AuthenticatedUser } from './types/globals';
import backendFetchAPI from './app/utils/backend-fetch-api';
declare module 'next-auth' {
/**
* Returned by `auth`, `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user_details: {
user: any;
access_token: string;
token_type: string;
/**
* By default, TypeScript merges new interface properties and overwrites existing ones.
* In this case, the default session user properties will be overwritten,
* with the new ones defined above. To keep the default session user properties,
* you need to add them back into the newly declared interface.
*/
} & DefaultSession['user'];
}
}
async function loginUser(username: string, password: string): Promise<LoginVariables | AuthenticatedUser | undefined> {
try {
const user = await backendFetchAPI<AuthenticatedUser>('/auth/login', 'POST', {}, { username, password }, true);
return {
...user,
};
} catch (error) {
console.error('Error logging in:', error);
return undefined;
}
}
export const { auth, signIn, signOut } = NextAuth({
...authConfig,
providers: [
Credentials({
credentials: {
username: { label: 'Username', type: 'text', placeholder: 'Username' },
password: { label: 'Password', type: 'password' },
},
// @ts-ignore
authorize: async (credentials: any) => {
const user = await loginUser(credentials.username, credentials.password);
if (!user) return null;
return user;
},
}),
],
secret: process.env.AUTH_SECRET,
session: {
strategy: 'jwt',
maxAge: 180 * 24 * 60 * 60,
},
debug: true,
callbacks: {
async session({ session, token }) {
// @ts-ignore
session.user_details = token.user;
return session;
},
async jwt({ token, user }) {
if (user) {
token.user = user;
}
return token;
},
// async authorized({ auth, request: { nextUrl } }) {
// console.log(nextUrl.pathname)
// const basePath = process.env.NGINX_SUBPATH_ROUTE || '';
// const isLoggedIn = !!auth?.user;
// const isNotInLogIn = !nextUrl.pathname.startsWith('/login');
// if (isNotInLogIn) {
// if (isLoggedIn) return true;
// return Response.redirect(new URL('/login', nextUrl)); // Redirect unauthenticated users to login page
// } else if (isLoggedIn) {
// return Response.redirect(new URL('/', nextUrl));
// }
// return true;
// },
},
}); Nothing happens. It just does not route to the login page whatever I do. Even almost no error logs appear. Randomly I get the following error: Error handling upgrade request TypeError: Cannot read properties of undefined (reading 'bind')
at DevServer.handleRequestImpl... I have a dashboard app with a couple of screens, and users need to login to enter the app. Next-auth works if I don't add any basePath/subpath, if it runs at localhost:8045. But it doesn't work at http://localhost:8045/app3. I also tried:
What am I doing wrong here? |
Beta Was this translation helpful? Give feedback.
-
@ndom91 is there any way you are able to check on this PR? #10797 it seems like this fixes the issue. |
Beta Was this translation helpful? Give feedback.
-
Have a look here: #12160 |
Beta Was this translation helpful? Give feedback.
-
OAuth-based authentication providers such as Google are set up.
When a user with the same email address exists and performs a Google login, the
OAuthAccountNotLinked
error occurs.It is a correct behavior to occur. However, the redirected page is
/api/auth/auth/login?error=OAuthAccountNotLinked
.What I expect is
/auth/login?error=OAuthAccountNotLinked
.I would appreciate it if you could point out any errors in the configuration.
The environment variables AUTH_URL and NEXTAUTH_URL are not set.
NextAuthConfig looks like this
Beta Was this translation helpful? Give feedback.
All reactions