diff --git a/db/access_tokens.js b/db/access_tokens.js index dacc2c4..91f6fa2 100644 --- a/db/access_tokens.js +++ b/db/access_tokens.js @@ -1,6 +1,6 @@ 'use strict'; -const {logger, authl} = global; +const {logger} = global; const loki = require('lokijs'); global.dbl = new loki('./loki.json', { @@ -8,15 +8,15 @@ global.dbl = new loki('./loki.json', { autosave: true, autosaveInterval: 5000, autoloadCallback() { - authl = global.dbl.getCollection('tokens'); - if (authl === null) { - authl = global.dbl.addCollection('tokens'); + global.authl = global.dbl.getCollection('tokens'); + if (global.authl === null) { + global.authl = global.dbl.addCollection('tokens'); } } }); module.exports.find = (key, done) => { - const ltoken = authl.findOne({'token': key}); + const ltoken = global.authl.findOne({'token': key}); if (ltoken){ const {userId, clientId} = ltoken; return done(null, {userId, clientId}) @@ -27,7 +27,7 @@ module.exports.find = (key, done) => { }; module.exports.findByUserIdAndClientId = (userId, clientId, done) => { - const ltoken = authl.findOne({'userId': userId}); + const ltoken = global.authl.findOne({'userId': userId}); if (ltoken){ logger.log('info', {message: `Load token by userId (${userId}): User found`}); const {token, userId: uid, clientId: cid} = ltoken; @@ -45,13 +45,13 @@ module.exports.findByUserIdAndClientId = (userId, clientId, done) => { module.exports.save = (token, userId, clientId, done) => { logger.log('info', {message: `Start saving token`}); - const ltoken = authl.findOne({'userId': userId}); + const ltoken = global.authl.findOne({'userId': userId}); if (ltoken){ logger.log('info', {message: `User Updated`}); - authl.update(Object.assign({}, ltoken, {token, userId, clientId})); + global.authl.update(Object.assign({}, ltoken, {token, userId, clientId})); } else { logger.log('info', {message: `User not Found. Create new...`}); - authl.insert({'type': 'token', token, userId, clientId}); + global.authl.insert({'type': 'token', token, userId, clientId}); } done(); };