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-authentication/auth.js
2021-05-29 14:41:35 +02:00

34 lines
969 B
JavaScript

const sessionJwt = require ('./sessionJWT');
const queries = require('./mongodbQueries');
function getSession (sessionid,res) {
return sessionJwt.decodeSessionCookie(sessionid,res);
}
module.exports.getSession = getSession;
function setSessionCookie (req, res, session) {
sessionJwt.createSessionCookie(req, res, session);
}
module.exports.setSessionCookie = setSessionCookie;
function getUsername(session) {
if (typeof session.username === 'undefined') return -1;
return session.username;
}
module.exports.getUserId = getUsername;
async function authenticate(req, res) {
const login = req.body.login;
const password = req.body.password;
const user = await queries.checkLoginQuery(login, password);
if (user === 1){
setSessionCookie (req, res, { username: login});
return user;
} else {
setSessionCookie (req, res, {username: -1});
return -1;
}
}
module.exports.authenticate = authenticate;