Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v4.0.0-beta.7 - when /profile request returns 401 (Unauthhorized) should be handle somehow #1817

Closed
6 tasks done
sleitor opened this issue Nov 21, 2024 · 4 comments
Closed
6 tasks done

Comments

@sleitor
Copy link

sleitor commented Nov 21, 2024

Checklist

Description

Heed some hook, ability, or automatically catch 401 error on /profile (that request by use-user.js on 15th line) to force logout from the app.

How we can catch it and redirect to /login or /logout page?

Reproduction

  1. login on auth0
  2. in the developer tab remove the cookie
  3. focus off/on on the page or just wait couple time
  4. after get atomatically on middleware will get a 401 for profile

Additional context

No response

nextjs-auth0 version

v4.0.0-beta.7

Next.js version

15.0.3

Node.js version

v20.18.0

@guabu
Copy link

guabu commented Nov 22, 2024

Hey @sleitor 👋 When the call to /profile returns the 401 the user will no longer be defined. You should be able to check the existence of the user (it will be null if they're not authenticated) and act on that:

"use client";
 
import { useUser } from "@auth0/nextjs-auth0";
 
export default function Profile() {
  const { user, isLoading } = useUser();
  
  if (!user) {
    // handle the logic you want here
  }
 
  return (
    // ...
  );
}

@sleitor
Copy link
Author

sleitor commented Nov 22, 2024

Thank you @guabu! Appreciate for fast answer. that partially works :)

...
  const { user, ...rest } = useUser();
  console.log('rest parameters', rest);
...

There found another bug.
when we remove the __session from cookies then In the useUser hook not catched properly 401 responce from fetch

I expecting to see there error like 'Unauthorized' instead "error parsing json" :)
image

@guabu
Copy link

guabu commented Nov 24, 2024

Hey @sleitor 👋 Thanks for the follow-up!

We've got a fix that should address this in the next release as well!

@nandan-bhat
Copy link

Closing this ticket as this issue has been fixed 4.0.0-beta.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants