2016-03-31 13:24:21 +00:00
|
|
|
/*jshint node:true*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var db = require('../units/db-connector').dbConnection;
|
|
|
|
var dbAccounts = require('../units/db-accounts')(db);
|
|
|
|
var logger = require('log4js').getLogger();
|
|
|
|
|
|
|
|
module.exports = function(app) {
|
|
|
|
var express = require('express');
|
|
|
|
var tokenRouter = express.Router();
|
|
|
|
|
|
|
|
tokenRouter.get('/', function(req, res) {
|
|
|
|
res.send({
|
|
|
|
token: []
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tokenRouter.post('/', function(req, res) {
|
|
|
|
if (req.body.hasOwnProperty('grant_type')) {
|
|
|
|
if (req.body.grant_type === 'password') {
|
|
|
|
dbAccounts.findAccount({
|
|
|
|
email: req.body.username,
|
|
|
|
password: req.body.password
|
|
|
|
})
|
|
|
|
.then(function(d) {
|
|
|
|
let loginObj = {
|
|
|
|
access_token: 'secret token!',
|
|
|
|
account_id: d.uid,
|
|
|
|
username: d.username,
|
|
|
|
account: d.email
|
|
|
|
};
|
|
|
|
// Res.status(200).send('{ "access_token": "secret token!", "account_id": d.id }');
|
|
|
|
res.status(200).send(loginObj);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
logger.error(err);
|
|
|
|
res.status(400).send(
|
|
|
|
'{ "error": "No account could be found with those details" }');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
logger.error('A');
|
|
|
|
res.status(400).send(
|
|
|
|
'{ "error": "No account could be found with those details" }');
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
logger.error('B');
|
|
|
|
logger.warn(req.body.hasOwnProperty('grant_type'));
|
|
|
|
res.status(400).send(
|
|
|
|
'{ "error": "No account could be found with those details" }');
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-04-06 16:02:43 +00:00
|
|
|
app.use('/token', require('body-parser').urlencoded());
|
2016-03-31 13:24:21 +00:00
|
|
|
|
|
|
|
app.use('/token', tokenRouter);
|
|
|
|
};
|