Handling Authentication in React with Context and Hooks

Learn how to handle identity management in React by creating a global state for your authentication details with Context and update these details with Hooks.

By #GuestAuth0r @royderks :man_technologist:t2:

Read on :atom_symbol:

1 Like

Got any questions regarding the article? Feel free to reopen the topic!

Thanks for this article @royderks

How would you persist the logged in state between refreshes?

Let me ping @royderks directly through our Slack so as to get his response faster!

Hi @jredford, glad you enjoyed the article!

For persisting the authenticated state of the users, you can have a look at the “Keeping Users Signed In after a Refresh” section of this article https://auth0.com/blog/react-tutorial-building-and-securing-your-first-app/. It means you need to silently check if a user is logged in by using a Silent Authentication request

2 Likes

Thanks a lot Roy for the input!

Perfect, I was able to persist the auth state with the help of that article!

Thanks again @royderks and thanks @konrad.sopala

2 Likes

Glad we were able to help!

Nice tutorial. I’ve gone through to the end but I keep getting the following error in console:

Unhandled Promise Rejection: null

Clearly there is something wrong either in the .env file or in the settings of the Auth0 app. Could you provide more details on what settings you need to get this working in the Auth0 app. For example, does http://localhost:3000 need to be entered into the CORS setting?

As it stands now, I’ll get the error in the console as soon as I start the app, and when I press login, the browser loads https://dev-5-vlv7-y.auth0.com/ (my app domain) with a login form.

1 Like

Hello, Peter! Welcome to the Auth0 community. I see what you mean! I am getting the same message in the Dev Console. Part of that is that the Promise has no catch element and also that on page load the handleAuthentication method is not successful.

Let me revise the code. I think it will also be best to update this blog post to use our latest Auth0 SPA SDK. I’ll be working on this. As the weekend is soon approaching, I’ll give you an update by next week Wednesday. How does this sound?

Hello, Peter! I sincerely apologize that I forgot to give you an update. We are working on revamping the guidance for React Context and Hooks. I don’t have an ETA yet but the Research and Development is in progress :man_scientist: I’ll be updating this thread and the blog post once it’s ready :pray: