How can I store JWT and send them with every request using reaction

So happy that I know, because I got my basic registration / authentication system.

so basically i got this:

app.post('/login', function(req,res) {
 Users.findOne({
email: req.body.email
}, function(err, user) {
if(err) throw err;

if(!user) {
  res.send({success: false, message: 'Authentication Failed, User not found.'});
} else {
  //Check passwords
  checkingPassword(req.body.password, user.password, function(err, isMatch) {
    if(isMatch && !err) {
      //Create token
      var token = jwt.sign(user,db.secret, {
        expiresIn: 1008000
      });
      res.json({success: true, jwtToken: "JWT "+token});
    } else {
      res.json({success: false, message: 'Authentication failed, wrong password buddy'});

       }
     });
    }
 });
});

Then I protect my / admin and POSTMAN routes, when I send a receive request with jwt in the header, everything works fine.

Now here is the tricky part, mostly when I am going to log in, if this sucess then redirects me to the admin page, and every time I try to access admin / * routes, I want to send jwToken to the server, but the problem is how do i achieve this? I do not use reduction / flow, just using a react / react router.

I do not know how a mechanic works.

Thanks guys,

+4
1
+5

Source: https://habr.com/ru/post/1652664/


All Articles