Next.js Discord

Discord Forum

No redirection after successful signOut()- NextAuth

Answered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I'm encountering an issue with redirecting users after signing out using NextAuth. Currently, after clicking the sign-out button:
<form
  action={async () => {
    "use server";
    await signOut({
      redirect: true,
      redirectTo: "http://localhost:3000/login-admin",
    });
  }}
>
  <button className="...">
    <PowerIcon className="w-6" />
    <div className="hidden md:block">Sign Out</div>
  </button>
</form>

The user logs out successfully but remains on the same page. My goal is to redirect them to the login page (http://localhost:3000/login-admin).

For added context this is my auth.config file:
import type { NextAuthConfig } from "next-auth";

export const authConfig = {
  pages: {
    signIn: "/login-admin",
  },
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
      const isLoggedIn = !!auth?.user;
      const isInAdmin = nextUrl.pathname.startsWith("/admin");
      if (isInAdmin) {
        if (isLoggedIn) return true;
        return false; // Redirect unauthenticated users to login page
      } else if (isLoggedIn) {
        return Response.redirect(new URL("/admin", nextUrl));
      }
      return true;
    },
  },

  providers: [], // Add providers with an empty array for now
} satisfies NextAuthConfig;

Any thoughts on how to achieve the desired redirection after sign-out?
Answered by Masai Lion
The issue was resolved, here is the comment on my issue that fixed it:

NextAuth V5 has some redirect issue with NextJs 14.2. The current solution is to downgrade NextJs in 14.1
View full answer

4 Replies

Spectacled bear
Can you provide a code sandbox example?
Masai LionOP
Sure thing. I'm trying to run the project in CodeSandbox but now the app won't let me log in and get to the admin page (where the issue is occuring)
Masai LionOP
@Spectacled bear, would you be cool downloading the repo and running it locally since CodeSandbox is breaking the login? Production Url => https://github.com/Gaurav-Narayan-Varma/dgcl
Masai LionOP
The issue was resolved, here is the comment on my issue that fixed it:

NextAuth V5 has some redirect issue with NextJs 14.2. The current solution is to downgrade NextJs in 14.1
Answer