project/server/mocks/token.js
Martin Donnelly e06e53e5bd whats wrong?
2016-04-06 16:53:06 +01:00

85 lines
2.2 KiB
JavaScript

/*jshint node:true*/
'use strict';
var db = require('../units/db-connector').dbConnection;
var dbAccounts = require('../units/db-accounts')(db);
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) {
res.status(400).send(
'{ "error": "No account could be found with those details" }');
});
} else {
res.status(400).send(
'{ "error": "No account could be found with those details" }');
}
} else {
res.status(400).send(
'{ "error": "No account could be found with those details" }');
}
});
tokenRouter.get('/:id', function(req, res) {
res.send({
token: {
id: req.params.id
}
});
});
tokenRouter.put('/:id', function(req, res) {
res.send({
token: {
id: req.params.id
}
});
});
tokenRouter.delete('/:id', function(req, res) {
res.status(204).end();
});
// The POST and PUT call will not contain a request body
// because the body-parser is not included by default.
// To use req.body, run:
// Npm install --save-dev body-parser
// After installing, you need to `use` the body-parser for
// this mock uncommenting the following line:
//
app.use('/token', require('body-parser').json());
app.use('/token', require('body-parser').text());
app.use('/token', require('body-parser').urlencoded());
app.use('/token', tokenRouter);
};