salix/services/auth/server/boot/routes.js

44 lines
1.3 KiB
JavaScript

var url = require ('url');
module.exports = function(app) {
let applications = app.get('applications');
app.get('/',function(req, res){
res.render('index.ejs');
});
app.post('/login', function(req, res) {
app.models.User.login({
email: req.body.email,
password: req.body.password
}, 'user', function(err, token) {
if (err) {
res.status (401);
res.send(JSON.stringify({
title: 'Login failed',
content: err,
redirectTo: '/',
redirectToLinkText: 'Try again'
}));
} else {
let query = url.parse(req.body.location, true).query;
let loginUrl = applications[query.apiKey];
if (!loginUrl)
loginUrl = applications.default;
res.send(JSON.stringify({
token: token.id,
continue: query.continue,
loginUrl: loginUrl,
}));
}
});
});
app.get('/logout', function (req, res) {
app.models.User.logout(req.accessToken.id, function(err) {
res.redirect('/');
});
});
};