next.jsnext-auth

callbackUrl not working in auth.js signin


I have set up a next.js app with next-auth.js, authenticating with a custom OIDC identity provider. I'm currently using next-auth version 5.0.0-beta.19, which I believe is part of the proposed migration to auth.js.

Anyway the auth all works fine, with one exception. After signin I'm always redirected back to the homepage even when I specify a callbackUrl option argument to the signIn function. If I debug next-auth's redirect callback I can observe that the url and baseUrl arguments are always the same value (http://localhost:3000).

The component where signin is implemented looks like this:

import { auth, defaultProviderId, signIn, signOut } from "@/auth"

export default async function SignInButton() {
  const authSession = await auth();

    if (authSession?.user) {
        return (
          <form
            action={async () => {
              "use server"
              await signOut();
            }}
          >
          <button type="submit">Sign out {authSession.user.name}</button>
        </form>
        );
    }

    return (
      <form
        action={async () => {
          "use server"
          await signIn(defaultProviderId, {
            callbackUrl: "/this/doesnt/work"
          });
        }}
      >
        <button type="submit">Sign in</button>
      </form>
    )
}

I don't seem to be doing anything different to other examples. Is there any other code I can share to help identify the issue? Like I say, the authentication itself works fine.


Solution

  • From the docs: "Once authenticated, the user will be redirected back to the page they started the signin from. If you want the user to be redirected somewhere else after sign in (.i.e /dashboard), you can do so by passing the target URL as redirectTo in the sign-in options."

    https://authjs.dev/getting-started/session-management/login

    It seems that it is "redirectTo" instead of "callbackUrl" enter image description here