iotsensor fixed but session valid broken
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
const { hash, compareHash } = require("./bcrypt.js");
|
||||
const { apikeyModel } = require("../database/model/apiKeyModel");
|
||||
const { tokenModel } = require("../database/model/tokenModel.js");
|
||||
const { generateUUID } = require("./generateUUID.js");
|
||||
|
||||
/*
|
||||
@ -11,37 +11,38 @@ const { generateUUID } = require("./generateUUID.js");
|
||||
6) store in database
|
||||
*/
|
||||
//can be used for api key or token. Both are the same logic
|
||||
async function addAPIKey(userId, permission) {
|
||||
let hashtoken = await generateUUID();
|
||||
let apikey = await hash(hashtoken);
|
||||
async function addToken(userId, permission , expiry) {
|
||||
let uuid = await generateUUID();
|
||||
let hashtoken = await hash(uuid);
|
||||
|
||||
let token = await apikeyModel.create({
|
||||
let token = await tokenModel.create({
|
||||
userid: userId,
|
||||
apikey: apikey,
|
||||
token: hashtoken,
|
||||
permission: permission,
|
||||
expiration: expiry,
|
||||
});
|
||||
|
||||
//user token with - tokenid is table id
|
||||
return token.id + "-" + hashtoken;
|
||||
return token.id + "-" + uuid;
|
||||
}
|
||||
|
||||
async function checkAPikey(SuppliedKey, rowid) {
|
||||
async function checkToken(Supplied, rowid) {
|
||||
try {
|
||||
const retrivedKey = await apikeyModel.findOne({
|
||||
const retrivedToken = await tokenModel.findOne({
|
||||
raw: true,
|
||||
attributes: ["apikey", "permission"],
|
||||
attributes: ["token", "permission"],
|
||||
where: {
|
||||
id: rowid,
|
||||
},
|
||||
});
|
||||
//console.log(retrivedKey.apikey);
|
||||
if (compareHash(SuppliedKey, retrivedKey.apikey)) {
|
||||
if (compareHash(Supplied, retrivedToken.token)) {
|
||||
//return true;
|
||||
return retrivedKey.permission;
|
||||
return retrivedToken.permission;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { addAPIKey , checkAPikey };
|
||||
module.exports = { addToken , checkToken };
|
Reference in New Issue
Block a user