traxxx/src/web/auth.js

47 lines
821 B
JavaScript

'use strict';
const { login, signup } = require('../auth');
const { fetchUser } = require('../users');
async function loginApi(req, res) {
const user = await login(req.body);
req.session.user = user;
res.send(user);
}
async function logoutApi(req, res) {
req.session.destroy((error) => {
if (error) {
res.status(500).send();
}
res.status(204).send();
});
}
async function fetchMeApi(req, res) {
if (req.session.user) {
req.session.user = await fetchUser(req.session.user.id, false, req.session.user);
res.send(req.session.user);
return;
}
res.status(401).send();
}
async function signupApi(req, res) {
const user = await signup(req.body);
req.session.user = user;
res.send(user);
}
module.exports = {
login: loginApi,
logout: logoutApi,
fetchMe: fetchMeApi,
signup: signupApi,
};