100 thoughts to “React JWT Authentication (without Redux) example”

  1. Hi, Excelent tutorial.
    This is helping me a lot :D.
    I just need some help

    When a user log in the user is still able to return to the /login page by typing it in the url. So my question is how to redirect the url to other page if the user has already logged in?

    Sorry for my english.

  2. Thanks for this. There are not many resources on this topic if you search on Google for something like “react user role authentication tutorial”. The one that ranks 1st, more than a tutorial, is a bunch of complicated code without explanation. Yours is easy to follow, with just the minimal requirements to make it work. Great for us just starting with React! I just wish I had found it sooner.

    1. Hi, if you use one of backend servers that I mentioned in the tutorial, you should notify the .env file where we configure port for api requests.

      The port should be 8000 instead of 3000.

  3. I have been in search of such a tutorial and this basically does it for me. This will save me a lot of headache and heartache, so thank you so much for this. At lease now I have a template to use with a python and Go backend.

  4. Hello,
    I tried to add some other routes and when i added [authJwt.verifyToken, authJwt.isModerator] to the routes in backend, but everyone even without log in could go to those routes. Do I need to do something extra to protect the routes?

      1. Hi
        I’ve just tried to do that with middleware, But It’s not working
        Can I have from you special help

  5. Do you have same example with Functional component + React hook
    I try to convert but keep getting errors.

  6. Friends, I need to make the following changes to this code. If I log in as an admin I enable access to all pages in the navigation bar. If I log in as a moderator I enable access to public and moderator pages. If I log in as a user, I can only access public pages. And where do I modify the code to load my pages instead of messages admin content, moderator content and user content?
    Thank you for your help!

  7. Hi Bezkoder,

    Thank you for this nice explanation of how this works ! I felt that implementing Redux was a bit too messy and happily found out your implementation to help me out.
    My bit of feedback would be to leave the validation part out of this tutorial and make a separate one for it as it makes it for much more code and is not at the essence of what you are trying to demonstrate (or maybe I am wrong) when reading the title.

  8. Hello. I implemented this in my project (I used your tutorial on doing spring boot backend with PostgreSQL), but when I launch my localhost, I get “Unauthorized error: Full authentication is required to access this resource”. What I could’ve done wrong? Thank you.

    1. Hi, you should check if http header is correct or not (Bearer for Spring Server and x-access-token for Node.js server)

  9. Are the routes actually protected? The links are protected but what about the url path? Directly accessing the route would load the component? Or I am wrong?

    1. Hi, you can protect the routes by checking the token every time user accessed the url. In the tutorial, I show you the way to protect on backend side. The component is still loaded, but protected data was not returned.

      1. would you check the expiration of the token after decoding with `exp` and compare against current time to protect private component loading this way seems to avoid calling the server for checking if the token is invalid. Or would you check the token itself with the server for this ensure the signature is also valid on top of this? Maybe restricting component load based on exp is enough because you check the token on every API request anyways

  10. Can you explain how can we set `Authorization` header for every request, if it exists in the `localStorage`.
    Adding it to every request is feel like not a good practise.

  11. Hi,

    Nice tutorial, I got a question. How do you manage react pages with different layouts with private routes that can redirect?

  12. Great Tutorial! But I do have a stupid question: Where do I have to put the validation-code part into? I am speaking of the code after: npm install react-validation validator.

    Thank you so much in advance!

    Greetz,
    H

  13. Hey,Thankyou for this amazing tutorial.
    How can i add a image logo in the navbar.I am a beginner and cannot figure it out.Any help will be appreciated.

  14. Nice tutorial! If you don’t mind please provide tutorial something like this in nextjs too =) Thankss

  15. Hi, I just wanted to say thank you for this tutorial. It is so easy to follow and works like a charm. This has been very beneficial for me learning react authentication, thanks again.

  16. Hi,

    I like your tutorial style, and this is the second one I’m trying to rebuild. The first one didn’t get anything on screen (different browsers), but seem to work on the database-level, so I tried this one.
    Unfortunattely still no luck. I get an error message “Module not found: Can’t resolve ‘react-validation/build/button’. I tried to solve this by manually installing ‘react-validation’ {npm install react-validation}.
    But still no luck. Any suggestion ?

    Thanks in advance

  17. Hi,

    Great tutorial, just like all the tutorials you post. I just have one question that might be a bug. If you log out and then use the back button of your browser, it will take you to the previous page and it doesn’t check for authentication. So it shows you the page. If you then refresh that page it checks for authentication and will divert to the login screen.
    Am i missing something or did I do something wrong? You shouldnt be able to use the back button when logged out to see the previous page.

    Thanks
    Eddie

  18. Hey,

    Very nice tutorial!

    I have a question regarding the logout process. How to implement a redirect/re-render here? So when the user is removed from the storage we should redirect to / or /login and rerender the page.

    Many thanks,
    TZ

  19. hi, first thank you for this amazing work, i have runed both the back and front and but i don’t know how to link between them ??

    1. Hi, you only need to run both with instruction in the tutorials. πŸ™‚
      – Backend: port 8080
      – Frontend: port 8081

      Then open browser with url: http://localhost:8081/

  20. Hi,
    for me sign in is successful but when i Login again with same user name and password it says

    “profile.component.js:20 Uncaught TypeError: Cannot read property ‘username’ of null”

    and also it is not reading the below code:
    {this.state.content}in home.component.js
    so i changed the above code to
    const {content} = this.props;
    return (

    {content}

    now no error but home screen is blank without any content.

    Please help me in the above 2 problems.

    1. Hello,
      I struggled with the same problem at the same line of code, I couldn’t figure it out. I realized that the accessToken was something different (token) for me. I would check the results you get from running the POST request in Postman and making sure the
      if(response.data.(yourtokenhere))
      in auth.service
      and the
      if(user&&user.(yourtokenhere))
      in auth-header
      have the same name of the token in it that you got in Postman. I hope this helps

    1. Hi Suraj, I had the same problem and I found one solution :

      In profile component , try to declare currentUser in your state like this :
      this.state = {
      currentUser: [AuthService.getCurrentUser()]
      };

      Because is an array of information about user.

      In render , you can call this array like this, example for get username :
      {this.state.currentUser[0].username}

    2. Hi, if you get the error “TypeError: Cannot read property β€˜username’ of undefined”, your version of express is different. This error means that you are not able to extract the body of the request.
      So, you have to use bodyParser ( for pulling information from HTML POST (express4)).
      Also if you are on the localhost, you have to use cors, in order to avoid browser cors error.

  21. Hi,

    First, amazing tutorials! Thank you so much!

    I followed both the backend node js + mongodb JWT tutorial and the React one. Everything works perfectly except one thing. When i try to tunnel through NGROK, i get network error on the home page. Any idea why? Or how i can fix this?

    Below is what is in the console log.

    from origin ‘http://ba3f2c631ae9.ngrok.io’ has been blocked by CORS policy: The ‘Access-Control-Allow-Origin’ header has a value ‘http://localhost:8081’ that is not equal to the supplied origin.

  22. Thank you very much for this, you’ve done an amazing work. It’s very understandable, and works perfectly.
    Is it possible to use this as a starting point for my projects ?

  23. Hi, thank you for your tutorials, it’s really helpful to me. Could you please tell me how to connect this react framework to your node.js backend? BTW, when I npm start this app, it returns Network Error. How this happens?

    1. Hi, first you should look at how we set the HTTP request header in auth-header.js file.
      Then, just run Node.js backend with the instruction in the tutorial.

      There are some things you need to notice: CORS configuration for backend, and setting port in .env for frontend.

  24. Hi there,
    just a question, how do you set role on signup? I’m askin’ about it cuz I’m trying to define my role during the signUp but i can’t find when you send the role.. it seems always be a req with no role.. the code exit with a default role [1] = user

    1. Hi, in this tutorial, the payload in signup request doesn’t have role and the backend processes 'user' as default role. If you wanna define role during the signup, you can add role[] inside the payload πŸ™‚

  25. Hi again,

    I have another question. It seems that the session expiration is only managed on server side but it should be on the front end side as well. Am I wrong? What can be done to do this?

      1. I don’t think this is the solution according to this:
        https://stackoverflow.com/questions/5523140/html5-local-storage-vs-session-storage

        the session storage doesn’t manage automatically expiration. sessionStorage only saves by Tab instead of the whole browser so the benefit is minimal. I would prefer to keep logged in if I access from another tab.

        The solution I am going to implement saves the timestamp together with the token in AuthService.login. AuthService.logout removes both the info. Whenever the application asks for the user token a check on expiration is done (the elapsed can be hardcoded but a better approach is to have it configurable). If the logged user is expired (passed the 1 day of validity) the Auth. getCurrentUser remove the user and timestamp from localStorage and returns null and the application understands the user must log out (go to the login page and refresh the window).

        At this point, I have a question: if a token on server-side expires is it a good decision to log out a user and ask to login again? Wouldn’t a refresh token be better? I am new to SpringBoot and I do not know what are the best practices for Access Token vs Refresh Token.

        Finally, there is then another typical scenario that Login scenarios must cover, log out a user after X minutes of inactivity. Any suggestions on how this can be done?

        Thank you in advance for the help.

        1. Ah yeah, now I understand your idea. You can research more details about Sliding-sessions.

          I will try to implement and write the tutorial when having time πŸ™‚

  26. How do u store the id ,name , role and jwt token in the same time , my controller return just the token how can i do ?

    1. Hi, you can see the way my controller returns HTTP response in the backend tutorial (Spring Boot/ Node.js Express). It contains all of id, name, role and jwt token.

        1. Hi, you need to use HTTP client to send signup request to backend server πŸ™‚

  27. Hi,

    First of all, let me thank you for your great tutorial they saved me lot of time.
    I just implemented the SpringBoot tutorial and it worked perfectly for my needs.
    https://bezkoder.com/spring-boot-jwt-authentication/
    Now I need to add a ReactJS front end and I am trying to add this tutorial. My problem is that when I submit login form I got:

    Unhandled Rejection (TypeError): Cannot read property ‘push’ of undefined

    at this line:
    this.props.history.push(“/profile”);

    consider that your code works and I am trying to import in my projects only parts I am interested in. The problem is that in this porting I don’t know what is causing the issue. Consider also that I am using the same libraries.

    1. I solved adding this in the App.js:

      “`

      “`

      then in the SignIn.js file where I have defined my SignIn component passed the history:

      “`

      “`
      in your case, you don’t have a separate component for the form and this confused me.

      1. Hi, I don’t know why your code didn’t display. You can post your code in raw format, I will modify it for better view πŸ™‚

      2. Hi,
        Try again to put code. I think the problem are < and >. I have three files: App.js, SigniIn.js and SignInForm.js, basically a separate component for the form.

        I solved adding this in the App.js:

        <Route exact path="/signin" component={SignIn}/>

        before I had:

        <Route exact path="/signin"><SignIn></Route>

        and in SignIn.js:

        <SignInForm history={this.props.history}/>

        finally in SignIn for I could use:

        this.props.history.push(β€œ/profile”);

        Hope this time the code will be print

        1. I did a better fix using withRouter from react-route-dom, basically, you only need to mark the components that need to use history. In my case, I avoid passing history from SignIn.js page to SignInForm and simply declare at the end of SignInForm.js this:

          export default withRouter(SignInForm);
          

          the problem is that in my app I have a sidebar and a status bar to update depending on whether the user is logged in or not. On both these components, I need to pass this.props.history including some intermediary components. The approach above simplifies everything.

          In your case is not required but if people have some intermediary components not referenced directly from Route this should be the good approach to solve the problem.

  28. Hello – your node, express, jwt, mongo, and react tutorials work great locally. However, when I push the app to Heroku, I get a network error when I try to login or create an account. Any suggestions?

    1. Hi, please take a look at your CORS configuration, you need to change the origin πŸ™‚

      1. The problem is I am not sure what to change it to. Heroku dynamically sets the port. So does that mean I need to put the heroku app url in the cors setup? Also do I keep the auth-service and user-service ports for server?

  29. Excellent tutorial, best one I’ve seen in fact! I cloned it from your site but when it comes up on localhost:8081, it shows “Network Error” in the content area uner the navbar. The NavBar looks correct and I if I click Sign Up link, it shows up. But if I enter a Username, Email, and Password, it also shows “Network Error” when I click the form’s blue Sign Up button. Suggestions?

    1. Hi, this is just a front-end project. You should implement one of the backend servers that I provided in this tutorial for fullstack development πŸ™‚

    2. hey man you made same mistake ,you just have to download another source code for api of your choice and the run that api and the react npm start.

      1. Hi, this React project is just a client, you need backend server for providing REST APIs. Please look at the tutorials I mention above.

  30. Nice post. I just don’t like that `window.location.reload()` though πŸ™‚

  31. This is a great tutorial. Intuitive and well explained. Do you have any examples of how to convert the localStoarge to an httpOnly cookie for the JWT token (read that Local Storage is easier to attack)

  32. Thank you so Much for this tutorial.
    it s very helpfull.

    Can i Modify your tutorial so that it can could use the new born “redux-toolkit”.
    and How can i send it to .

    let me know. Thank again for this tutorial

  33. Hello, great tutorial and very well explained.
    1 question, do you recommend redux for this kind of projects (with many types of users and much more data from database) ?

  34. thanks for share this
    by the way, in your code, my intelliJ also vscode have error for spread operator, any workaround to fix this.

  35. Hi Mr Bezkoder,
    Thank you for this great tutorial!
    You saved my day.
    Could we have a Front-end version with React Redux and JWT Passport?
    In any case thank you!!!

  36. Sir, to me you are God sent , you have taught me what I have been trying and searching the whole web to learn I appreciate you a lot keep the good work.

  37. Hi,

    when I fetch GET method, everything is working, i.e.:

    componentDidMount() {
            this.setState({isLoading: true});
            const currentUser = AuthService.getCurrentUser();
            const id = currentUser.id;
            fetch('api/cases/user/' + id, {headers: authHeader()})
                .then(response => response.json())
                .then(data => this.setState({cases: data, isLoading: false}));
        }
    

    but for POST method, i.e.:

    async handleSubmit(event) {
            event.preventDefault();
            const {aCase} = this.state;
    
            await fetch('/api/cases', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json',
                    'Authorization': authHeader()
                },
                body: JSON.stringify(aCase)
            });
            this.props.history.goBack();
        }
    

    the result is: Access denied.

    At first glance, do you see any error in this method?

    1. Hi bezkoder, problem solved πŸ™‚
      For people interested in it:

      async handleSubmit(event) {
              event.preventDefault();
              const {aCase} = this.state;
              const header = new Headers(authHeader());
              header.set('Accept', 'application/json');
              header.set('Content-Type', 'application/json');
              await fetch('/api/cases', {
                  method: 'POST',
                  headers: header,
                  body: JSON.stringify(aCase)
              });
              this.props.history.goBack();
          }
      
  38. Hi bezkoder,

    I’m writing my application and your tutorial is very helpful for me. Everything is working well, including JWT etc. I can get all my data and post something in Postman using token.

    But when I try to use POST method in fetch function, I receive “Access denied” message.

    Could you show, please, how to write example fetch (or axios) method according to this tutorial? (with token use)

  39. Hi, thank you very much, you are the best!
    Could you please so kind to share source code?

    1. Hi, you can follow the steps in the tutorial and try to make your project work.
      I’ve tested the source code and it worked definitely. But I think sharing the source code early will not help people because they need to try their best first.

      Yeah, I will upload my code to Github. πŸ™‚
      You can return to this page and get it in 2 or 3 weeks.

  40. you are great man!! people who tried this code will always thank you for a great start.

  41. Excellent post, it helped me a lot. Thank you!.
    Could you give me an idea of ​​how to do this with Hooks (useState, useEffect)?

        1. How can I choose the roles is either user or admin or moderator . It’s makes me confuse . Please help me out

      1. Excellent work!!! But my question is, what if I don’t want to use BoardAdmin, BoardModerator, BoardUser in the navbar link. I have my links already just to create a login & register Page. Please I need your help on this working on my bootcamp project

Comments are closed to reduce spam. If you have any question, please send me an email.