update
This commit is contained in:
@@ -3,25 +3,14 @@
|
||||
const crypto = require('crypto');
|
||||
const Redis = require('ioredis');
|
||||
|
||||
function AuthToken(redisOpt) {
|
||||
if (!(this instanceof AuthToken)) {
|
||||
return new AuthToken(redisOpt);
|
||||
}
|
||||
|
||||
if (!redisOpt) {
|
||||
throw 'AuthToken: need redis config.';
|
||||
}
|
||||
|
||||
this.tokenDB = new Redis(redisOpt);
|
||||
this.tokenDB.on("connect",function(){
|
||||
console.log("AuthToken: ioredis connected: " + JSON.stringify(redisOpt));
|
||||
});
|
||||
function AuthToken(redisdb) {
|
||||
this.tokenDB = redisdb;
|
||||
}
|
||||
|
||||
AuthToken.prototype.genToken = async function(userData, expiresSeconds) {
|
||||
// 生成系统内部的user token
|
||||
let hash = crypto.createHash('md5');
|
||||
hash.update(JSON.stringify(userData));
|
||||
hash.update(JSON.stringify(userData) + Date());
|
||||
let userToken = hash.digest('hex');
|
||||
|
||||
// 缓存到redis
|
||||
@@ -109,4 +98,10 @@ AuthToken.prototype.getTokenData = async function (userToken) {
|
||||
return tokenData;
|
||||
};
|
||||
|
||||
module.exports = AuthToken;
|
||||
let tokenInstance = null;
|
||||
module.exports = function getTokenInstance(redisPort, redisHost, db) {
|
||||
if (!tokenInstance) {
|
||||
tokenInstance = new AuthToken(redisPort, redisHost, db);
|
||||
}
|
||||
return tokenInstance;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ehason/auth",
|
||||
"version": "1.0.2",
|
||||
"version": "1.2.2",
|
||||
"description": "User auth lib",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
Reference in New Issue
Block a user