This repository has been archived on 2026-05-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
chatless/backend/service-privateroom/auth.js
2021-06-06 11:40:31 +02:00

34 lines
1.2 KiB
JavaScript

const request = require('request');
function getSession (req, callback) {
if(typeof req.headers.cookie !== 'undefined'){
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'http://127.0.0.1:3000/verify:token',
body: 'sessionid='+req.headers.cookie.replace('SESSIONID=','')
},function (error, response, body) {
console.log("body ; getSession auth private :",body);
if(typeof body !== 'undefined'){
const bodyJson = JSON.parse(body);
if (bodyJson && bodyJson.status && bodyJson.data) {
if (bodyJson.status === 'ok') {
return callback(bodyJson.data.token);
} else {
return callback(bodyJson.data.reason);
}
}
}
else{
return callback('Error');
}
});
}
return callback(undefined);
}
module.exports.getSession = getSession;
function getUsername(session) {
if (typeof session === 'undefined' || typeof session.username === 'undefined') return -1;
return session.username;
}
module.exports.getUsername = getUsername;