J
J
jtag2019-06-16 15:56:15
JSON Web Token
jtag, 2019-06-16 15:56:15

What is the correct way to follow a secure link using Json Web Token?

A token is sent from the server after verifying the login and password.

req.login(user, {session: false}, (err)=>{
        if(err){
          res.send(err);
        }
        if(res) {
          let payload = {id: user.username};
          const token = jwt.sign(payload, secretKey);
          return res.json({token: token});
        }

On the client, the received token is stored in localStorage:
$.ajax({
            url: post_url,
            type: request_method,
            data: form_data
        }).done(function(response){
            localStorage.setItem("token", response.token);
        });

1. After saving the token, nothing happens further, it turns out that you need to manually immediately follow the new link?
2. How to write code correctly so that the client goes through a secure route with the following header: < "Authorization", 'Bearer ' + localStorage.token) >

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-06-16
@Robur

After saving the token, nothing happens further, it turns out that you need to manually immediately follow the new link

You have code that gets the token, saves it, and does nothing else, so nothing else happens to you.
The next step is to use the token.
Use headers when calling $.ajax
here are the details:
https://api.jquery.com/jQuery.ajax/
If you expect it to work "somehow by itself" like with cookies, it won't. It is necessary to use this token "by hand", adding it to requests to the server.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question