Custom Base Path not working #9274
-
Hi, I have the following problem, I need to change the custom base path for next auth to work. My application already has an endpoint api/auth for backend server, so I started looking for a solution to this problem. Following the documentation - I changed the value of NEXTAUTH_URL (site/next/api/auth) and basePath in (next/api/auth) and added a custom folder in the path to the nextauth config (app/next/api/auth/[...nextauth]/route.ts). This helped but only for the local version of the site, if you load it on a real domain then incorrectly flies request to providers (for example Google expects redirect url - site/api/auth). Tried to solve this problem in app and pages routing and got the same result. The problem seems to have gone away in the latest beta version (5.0.0-beta.4), but I would like to use a more stable version if possible, since without proper documentation it is hard to migrate all my methods from [...nextauth].ts or [...nextauth]/route.ts of the previous current version. Below is a simple sample code for implementation import type { AuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
export const authConfig: AuthOptions = {
providers: [
GoogleProvider({
clientId: process.env.AUTH_GOOGLE_ID!,
clientSecret: process.env.AUTH_GOOGLE_SECRET!,
}),
],
secret: process.env.NEXTAUTH_SECRET!,
pages: {
signIn: "/signin",
},
}; import { authConfig } from "@/configs/auth";
import NextAuth from "next-auth/next";
const handler = NextAuth(authConfig);
export { handler as GET, handler as POST }; "use client";
import { SessionProvider } from "next-auth/react";
export const Providers = ({ children }: { children: React.ReactNode }) => {
return (
<SessionProvider basePath="/next/api/auth">{children}</SessionProvider>
);
};
I would appreciate any help and advice to solve this problem. |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 7 replies
-
Did you set your basePath on the next.config ? |
Beta Was this translation helpful? Give feedback.
-
I will share my solution to this problem, one way or another you have to use app routing to create a custom path route for nextauth. The path will look like this src/app/next/api/auth/[...nextauth]/route.ts Then create a route for NextAuth (app routing) - https://next-auth.js.org/configuration/initialization#route-handlers-app. ` const auth = async (req: any, ctx: any) => { const authOptions: AuthOptions = { return await NextAuth(req, ctx, authOptions); export { auth as GET, auth as POST }; |
Beta Was this translation helpful? Give feedback.
-
Added basePath in auth.js and SessionProvider |
Beta Was this translation helpful? Give feedback.
-
Hi friends, my nextauth config (app/visa/api/auth/[...nextauth]/route.ts).
`import NextAuth from 'next-auth'; export const { handlers, signIn, signOut, auth } = NextAuth({ add a base path by default it is /api/auth so change to this /custom_name/api/auth
`'use client'; import { signInOAuth } from '@/lib/actions/sign-in.action'; export const Social = () => { <Button className="w-full" size="lg" variant="outline" onClick={() => { onClick('google'); }} > ); }; ` after that we will use signIn from auth.ts i.e creating a server action for sign in.. `'use server'; import { type OAuthProviderType } from 'next-auth/providers'; /**
redirect(redirectUrl); path for google developer console. |
Beta Was this translation helpful? Give feedback.
-
I was not having success with the above solutions, for us the fix was adding the so if you had import { SessionProvider } from "next-auth/react";
// baseUrl === 'https://example.com` basePath === `/custom-base`
<SessionProvider baseUrl={baseUrl} basePath={`${basePath}/api/auth`}> not defining the |
Beta Was this translation helpful? Give feedback.
-
For v4, define const config = {
// ...
env: {
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
},
}; Read more about this bug here: For v5, see this pull request (contains 2 workarounds): And another proposed workaround: |
Beta Was this translation helpful? Give feedback.
-
for v5 working workaround |
Beta Was this translation helpful? Give feedback.
I will share my solution to this problem, one way or another you have to use app routing to create a custom path route for nextauth. The path will look like this src/app/next/api/auth/[...nextauth]/route.ts
Then create a route for NextAuth (app routing) - https://next-auth.js.org/configuration/initialization#route-handlers-app.
`
declare module 'next-auth' {
interface Session extends DefaultSession {
token: string;
user: DefaultSession['user'] & {
email: string;
name: string;
picture: string;
};
}
}
const auth = async (req: any, ctx: any) => {
const authOptions: AuthOptions = {
providers: [
GoogleProvider({...
credentialProviderLogin,
credentialProviderSignup,
],
secret:
session: {
},
pag…