#SignOut when response.status is 401

1 messages · Page 1 of 1 (latest)

languid stag
#

I have a server action that with an api request. I'm using NextAuth for authentication. I would like to sign the user out or invalidate their session if this API response status returns a 401. This code is currently not working. How can I make it work? I'm using the app router.

export async function getUsers(page: number) {
    const session = await getServerSession(authOptions);

  try {
    const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/admin/users?skip=${page * 10}`, 
    {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            "api-key": process.env.API_KEY!,
            "Authorization": `Bearer ${session.jwt}`
        }
    });
    
    console.log(response.status)
    if (response.status === 401) {
        redirect('/api/auth/signout')
    }
    return await response.json();
  } catch (err) {
    console.log(err);
  }
}
spare flaxBOT
#

Hi @languid stag. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Hmm, I don't know enough to give you a confident answer yet.

However, based on the information provided in the extracts, there are a few points to consider when dealing with authentication and redirection in Next.js:

  1. The redirect (https://nextjs.org/docs/app/api-reference/functions/redirect) function is used to navigate to a new route after the completion of a Server Action. This function should be used within the context of a Server Action, not directly within an API call as you are attempting to do.

  2. When dealing with authentication, especially with NextAuth.js, you would typically handle sign-out by calling the sign-out endpoint provided by NextAuth.js. This is not directly shown in the extracts provided.

  3. The code you provided seems to be mixing client-side and server-side logic. The redirect function is intended for use on the server side, and it's not clear from the provided code where the getUsers function is being called from.

  4. If you are using the App Router, you might want to consider handling the sign-out process on the client side by redirecting the user to the sign-out route provided by NextAuth.js (/api/auth/signout).

Given these points, you may want to adjust your approach by handling the 401 status within the client-side logic that calls getUsers. If a 401 status is detected, you could then redirect the user to the sign-out page using the Next.js router or by setting window.location.

For more detailed guidance, you should refer to the NextAuth.js documentation, which provides information on how to set up sign-out functionality: NextAuth.js Documentation (https://next-auth.js.org/configuration/pages).

#

Since the exact code solution is not provided in the extracts, I recommend reviewing the NextAuth.js documentation and Next.js documentation for the correct usage of these features.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: