obrand-admin-server/server/app/token.js
2016-04-06 17:02:43 +01:00

59 lines
1.6 KiB
JavaScript

/*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" }');
}
});
app.use('/token', require('body-parser').urlencoded());
app.use('/token', tokenRouter);
};