mirror of
https://github.com/lasthead0/yandex2mqtt.git
synced 2025-08-07 16:50:30 +03:00
Add logging and some code improvement
This commit is contained in:
@@ -17,18 +17,18 @@ global.dbl = new loki('./loki.json', {
|
||||
module.exports.find = (key, done) => {
|
||||
const ltoken = global.authl.findOne({'token': key});
|
||||
if (ltoken){
|
||||
console.log('Token found');
|
||||
const {userId, clientId} = ltoken;
|
||||
return done(null, {userId, clientId})
|
||||
} else {
|
||||
return done(new Error('Token Not Found'));
|
||||
global.logger.log('error', {message: `${new Error('Token Not Found')}`});
|
||||
return done();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.findByUserIdAndClientId = (userId, clientId, done) => {
|
||||
const ltoken = global.authl.findOne({'userId': userId});
|
||||
if (ltoken){
|
||||
console.log('Load token by userId: User found');
|
||||
global.logger.log('info', {message: `Load token by userId (${userId}): User found`});
|
||||
const {token, userId: uid, clientId: cid} = ltoken;
|
||||
if (uid === userId && cid === clientId) return done(null, token);
|
||||
else return done(new Error('Token Not Found'));
|
||||
@@ -39,13 +39,13 @@ module.exports.findByUserIdAndClientId = (userId, clientId, done) => {
|
||||
};
|
||||
|
||||
module.exports.save = (token, userId, clientId, done) => {
|
||||
console.log('Start saving token');
|
||||
global.logger.log('info', {message: `Start saving token`});
|
||||
const ltoken = global.authl.findOne({'userId': userId});
|
||||
if (ltoken){
|
||||
console.log('User Updated');
|
||||
global.logger.log('info', {message: `User Updated`});
|
||||
global.authl.update(Object.assign({}, ltoken, {token, userId, clientId}));
|
||||
} else {
|
||||
console.log('User not Found. Create new...');
|
||||
global.logger.log('info', {message: `User not Found. Create new...`});
|
||||
global.authl.insert({'type': 'token', token, userId, clientId});
|
||||
}
|
||||
done();
|
||||
|
Reference in New Issue
Block a user