Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

node.js - How to store authentication token with node and express

I have two nodejs app for client and server. Client side doesn't rely on any libraries for building interfaces. It uses pure html and express routes to the requested page.

router.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

Client sends an auth request to the server via AJAX call and receive the auth token back. I need to store it somewhere and make sure express checks if token is available or not to reroute user to login page if unauthenticated (token is not available)

I was thinking to use localStorage but it is not available/accessible under express.

$.ajax({
  url: 'http://server:8080/login',
  type: "POST",
  data: {username, password},
  dataType: "JSON",
  contentType: 'application/json',
  success: function (data) {
    localStorage.setItem("jwtToken", data.jwtToken);
  },
});
question from:https://stackoverflow.com/questions/65891966/how-to-store-authentication-token-with-node-and-express

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe your should consider store it like a cookie and all the new requests to your server will contain the necessary information to verify the auth of the client.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...