Next.js Discord

Discord Forum

Next auth v5 signOut function doesn't do anything in server action

Unanswered
Pink salmon posted this in #help-forum
Open in Discord
Pink salmonOP
I am using app router and nextjs 14. Here is my server action, I have verified via console logs that I am indeed calling the signOut function.
export async function foo() {
  return fetch('...', {...})
    .then(async (res) => {
      const json = await res.json();
      if (!res.ok) {
        if (res.status === 401) {
          await signOut();
          return [];
        }
        throw new Error('Request failed');
      }
      return json
    })
    .catch(() => []);
}


I expect the signOut function to redirect my user back to the login page when it runs. Instead nothing appears to happen. I am on the /dashboard page when this is occurring. Here is my middleware.
import NextAuth from 'next-auth';
import { authConfig } from './auth.config';

export default NextAuth(authConfig).auth;

export const config = {
  // https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
  matcher: ['/', '/login', '/dashboard'],
};


Here is my auth.ts
import NextAuth, { User } from 'next-auth';
import Credentials from 'next-auth/providers/credentials';
import { authConfig } from '../src/auth.config';
import { z } from 'zod';
import { KAHE_URL } from './lib/constants';

export const { auth, signIn, signOut } = NextAuth({
  ...authConfig,
  providers: [
    Credentials({
      async authorize(credentials) {
        const parsedCredentials = z
          .object({ email: z.string().email(), password: z.string() })
          .safeParse(credentials);

        if (parsedCredentials.success) {
          const { email, password } = parsedCredentials.data;
          const user = await attemptSignIn(email, password);
          if (user) return user;
        }

        return null;
      },
    }),
  ],
});

function attemptSignIn(email: string, password: string): Promise<User | null> {...}


And here is my auth config
import type { NextAuthConfig } from 'next-auth';

export const authConfig = {
  pages: {
    signIn: '/login',
  },
  session: {
    strategy: 'jwt',
    maxAge: 60 * 60 * 12, // 12 hours
  },
  callbacks: {
    authorized({ auth, request: { nextUrl } }) {
      const isLoggedIn = !!auth?.user;
      if (!isLoggedIn) return false; // Redirects to login page

      const isOnLoginPage = nextUrl.pathname.startsWith('/login');
      if (isOnLoginPage) {
        return Response.redirect(new URL('/', nextUrl));
      }

      return true;
    },
    jwt: async ({ token, user }) => {
      if (user) {
        token = { accessToken: user.jwt };
      }
      return token;
    },
    session: async ({ session, token }) => {
      if (session.user) {
        session.accessToken = token.accessToken as string;
      }
      return session;
    },
  },
  providers: [], // Handled in auth.ts
  trustHost: true,
} satisfies NextAuthConfig;

4 Replies

Catla
@Pink salmonDid you solve this?
When I call signout from my nav client component in v5, it clears session.user for my nav component only. For the page itself it treats session.user like it still exists. Not sure what on earth is going on, but I know for nav I call signOut via next-auth/react, and on my page I get the session.user from @auth
@Catla <@402578266008846337>Did you solve this?
Pink salmonOP
I did not, in fact I couldn't figure it out insomuch that my boss said we had to stop using Next.js and go back to Vite. 😀
Catla
Goodness