You may also like...

42 Responses

  1. Vova says:

    Hi Clue,
    Thanks for the article!!!
    I wonder how to create not “static user details” … for instance json file or something like that. I want create more admins

    • Clue Mediator says:

      Hello Vova,
      I’m glad you like the article.

      Below code will help you to read/write json file instead of static users.


      var fs = require('fs');
      ...
      ...
      var users = [];
      ...
      fs.readFile('jsonfile.json', 'utf8', function readFile(err, data){
      if (err){
      console.log(err);
      return false;
      }
      const obj = JSON.parse(data); // parse data from string
      obj.push({id: 2, square:3}); // add some data
      const json = JSON.stringify(obj); // convert it back to JSON format
      fs.writeFile('jsonfile.json', json, 'utf8', callback); // write it back
      }});
      ...
      ...

      Like & Share with your friends. Happy Coding..!!

      • Vova says:

        Thanks Clue!
        This part of code should have separate file? or I can add it to server.js file?

        • Vova says:

          I made something like this:
          const users = [
          {
          userId: “789789”,
          password: “123456”,
          name: “Mike X”,
          username: “mike”,
          isAdmin: true
          },
          {
          userId: “7333333”,
          password: “123456”,
          name: “Joe”,
          username: “joe”,
          isAdmin: true
          },
          ]

          let userData = users.find(obj =>{
          return obj.username === “joe”
          })

          How can I check “username” as variable instead of write “joe” ?

        • Clue Mediator says:

          Hello Vova,
          I would recommend you to take separate file instead of variable in server.js file. You can also use database instead of it.

          – Thanks

  2. Edwin says:

    Hi clue, I’m getting a ‘token not defined’ on utils.js file

    • Clue Mediator says:

      Hi Edwin,
      Sorry, We made a mistake in “generateToken” function. Can you please check it and let me know if you are still facing an issue.

      Thank you for pointing our attention there.

      Follow us on Facebook & Twitter to get latest update.
      – Clue Mediator

  3. Abhijit says:

    i’m not able to perform multi-user auth for above example …can you guys help with sample code or stuff?

    • Clue Mediator says:

      Hi Abhijit,
      What do you mean by multi-user auth? Can you please explain more?

      We’ll try our best to help you.

  4. Abhijit says:

    yeah like above example is for static single user sign-in ,
    i’m trying to implement sign-ins for more than one user…like have 4 users, 2 are admins and like that
    i’m noobie in node backend, thanks for help

    • Clue Mediator says:

      Hi Abhijit,
      You have to implement the role based application to manage the multi role user login.

      Let me add this topic in my list. We will write separate article on it.

      You can subscribe us for weekly email update of our news & articles.

      Like us on Facebook & Share it. Happy Coding..!!!

      – Clue Mediator

  5. Abhijit says:

    Ok….looking forward to it

  6. Tobias says:

    Hello,

    very nice article to read and to understand how that all works. I have one question: If I have other backend routes too, that should be protected by the token how do I verify that the user is loggedin ?

    • Clue Mediator says:

      Hello Tobias,
      We glad you like it.

      Let’s talk about your queries. Are you talking about the other backend technologies or other private routes?

      If you are talking about the private routes then you should have separate middleware where you can verify the token for each request and based on that you can manage the logged-in status.

      In this article, we have covered the general straight forward flow of the authentication. Check out the below article where you will find the more secure way to implement authentication including refresh token and CSRF protection.

      Login App with CSRF protection – Understanding authentication using JWT access token and refresh token

      We are still working on it to serve two more articles related to it.

      Hope you like it.

      Subscribe us for weekly updates or like and follow us for regular updates.

      – Clue Mediator

  7. Tobias Dethleffsen says:

    thanks Clue. The linked article sounds like what I am searching for. I am looking forward to read it if it is finished.

    • Clue Mediator says:

      Great, You will get a bunch of articles by next week.

      Keep in touch and share it with your friends.

      Happy Coding..!!

  8. gurudeep shrotriya says:

    Hey Clue! While posting signin request over Postman, I’m getting following error:
    {
    “error”: true,
    “message”: “Username or Password required.”
    }
    I checked with your code..everything is same still I’m getting this error

    • Clue Mediator says:

      Hello Gurudeep,

      Are you passing the username and password via users/signin post request? You can also clone the Github repository and use the postman collection that we have provided at the bottom of the post because I have tested it and it’s working with the existing demo.

      Let me know if you are still facing any issues.

      Subscribe us for weekly updates or like and follow us for regular updates.

      Happy Coding..!!

  9. Mor Mauda says:

    Hi,

    Thanks for this!

    Do you familiar with the problem of :

    You need to enable JavaScript to run this app.

    when trying to run GET request from postman for “http://localhost:3000” ?

    • Clue Mediator says:

      I would guess your GET request URL is wrong. Please try to access it from the browser so you can get more idea about the error.

  10. kamal says:

    Hi,

    Thanks for this!

    Can you please give register form example with same flow

  11. saif says:

    can you please let me know where to add the below code to add multiple users instead of static users?

    var fs = require(‘fs’);


    var users = [];

    fs.readFile(‘jsonfile.json’, ‘utf8’, function readFile(err, data){
    if (err){
    console.log(err);
    return false;
    }
    const obj = JSON.parse(data); // parse data from string
    obj.push({id: 2, square:3}); // add some data
    const json = JSON.stringify(obj); // convert it back to JSON format
    fs.writeFile(‘jsonfile.json’, json, ‘utf8’, callback); // write it back
    }});

    • Clue Mediator says:

      Hello Saif,
      In the current article, we are only reading the data to verify them. So I suggest you create a new promise based function and write code to get the list of the user from the JSON file. Call this newly created method from signin API.

      Let us know if you still have any doubts.

  12. Will Shope says:

    Where is the /users/signin coming from? I get an error 404 whenever I try to POST to it.

  13. Oryz says:

    Hi Clue Mediator, thank for this article
    i want to ask, how to read user data from API JSON Response?

    • Clue Mediator says:

      Hi Oryz,
      Please check out the following article to read the data from the API JSON response and display it on the page.
      API calls with React Hooks

      • Oryz says:

        no, that’s not what I mean, i want to get userData in this article from API response.
        in this article userData get from static data, but i want to get userData from other API like a “https://jsonplaceholder.typicode.com/users”, how to fetch/axios from that’s API source and check username password from my form?how to add fetch or axios in server.js? is it possible to fetch from other API in server.js?

        • Oryz says:

          i have a backend website and develop with symfony, i create API with it
          i develop frontend with reactjs, and want to login with userData from API source in symfony

        • Clue Mediator says:

          Yes, you can use the node-fetch npm package to manage the API call.

          • Oryz says:

            I’ve tried to do that, with fetch or axios in server.js before compare username password but no response and no error, i tried put axios in login.js and work, but why didn’t work in server.js?

          • Clue Mediator says:

            Can you please check the Console or Network tabs? Is there any CORS error?

  14. Josh says:

    Hi Clue… I really appreciate your work. It’s quite impressive. With that being said, I am a novice at coding, however, I’m having issues myself logging in as I’m coming up empty with your code. If you could please assist, it would me appreciated. Thank you so much!

    • Clue Mediator says:

      Hi Josh,
      At the end of the article, you will find the working demo GitHub source code. You can use the postman collection for the testing.

  15. ing hort says:

    hi Clue mediator very thank for your tutorial i try do follow you but when i try test api it have problem with function post. when post it message cannot post users/singin.. please help explain me. thanks.

    • Clue Mediator says:

      Hi Hort, Can you please clone the git repository and test again? If you are still facing any issues then share your code through contact email.
      Thanks

  16. john says:

    20 |Unhandled Rejection (TypeError): Cannot read property ‘status’ of undefined

    setLoading(false);
    21 |
    22 |
    > 23 | if (error.response.status === 401) setError(error.response.data.message);
    | ^ 24 | else setError(“Something went wrong. Please try again later.”);
    25 |
    26 | });

    Resolve the error

  17. Arjun says:

    Hi Clue,

    I am new to this, when i am using postman

    For: http://localhost:4000/user/signin getting empty response or can not get /user/signin 400 Bad request

    Thanks !!

Leave a Reply

Your email address will not be published. Required fields are marked *