[英]Nodejs express JWT how to strore and send token from web/phone client (jsonwebtoken)
I'm using NodeJs with Express app to develop webs for browsers and mobile phones. 我正在使用带有Express应用程序的NodeJ来开发用于浏览器和手机的Web。 I'm using JWT because it seems to be the standard and I read that sessions doesn't work well in phones (without browser).
我正在使用JWT,因为它似乎是标准的,而且我读到会话在手机(没有浏览器)中不能很好地工作。 I have this code in the backend configured using "jsonwebtoken":
我在使用“ jsonwebtoken”配置的后端中有以下代码:
'use strict';
const jwt = require('jsonwebtoken');
const path = require('path');
const fs = require('fs');
const privateKEY = fs.readFileSync(path.join(__dirname, 'private.key'), 'utf8');
const publicKEY = fs.readFileSync(path.join(__dirname, 'public.key'), 'utf8');
module.exports = {
sign: (payload) => {
var signOptions = {
expiresIn: process.env.TOKEN_EXPIRE_TIME,
algorithm: "RS256"
};
return jwt.sign(payload, privateKEY, signOptions);
},
verify: (token) => {
var verifyOptions = {
expiresIn: process.env.TOKEN_EXPIRE_TIME,
algorithm: ["RS256"]
};
try {
return jwt.verify(token, publicKEY, verifyOptions);
} catch (err) {
return false;
}
},
decode: (token) => {
return jwt.decode(token, {
complete: true
});
}
};
But I don't know which is the way to implement the front end side. 但是我不知道哪种方法可以实现前端。 I need to know how to store the token for both devices (localStorage? sessionStorage? cookies? others?) and what could be a good way to make the links (href tag in html) sending the token from that storage method.
我需要知道如何为两种设备(localStorage,sessionStorage,Cookie或其他设备)存储令牌,以及使链接(HTML中的href标记)从该存储方法发送令牌的好方法是什么?
Type of storage for the JWT token at the front-end is totally dependent on your applications requirement and how do you want to handle user's login/logout session. 前端JWT令牌的存储类型完全取决于您的应用程序要求以及您要如何处理用户的登录/注销会话。
See this article on JWT uses, give a clap if you like
请参阅有关JWT用法的这篇文章 ,如果喜欢,请鼓掌
If you're not clear about this then you can usr localStorage as the data stored in it stays event after the browser is closed - if it doesn't bother your requirement. 如果不清楚这一点,则可以在关闭浏览器后usr localStorage,因为其中存储的数据将保持事件状态-如果不打扰您的需求。
For sending token from link - there is two things you can do - 要从链接发送令牌-您可以执行以下两项操作-
I hope that helps. 希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.