Merge pull request #40 from KevinMidboe/fix/api

Fix/api
This commit is contained in:
2017-10-06 12:15:08 +02:00
committed by GitHub
2 changed files with 18 additions and 7 deletions

View File

@@ -8,24 +8,34 @@ const mustBeAuthenticated = require('./middleware/mustBeAuthenticated');
// this will let us get the data from a POST // this will let us get the data from a POST
// configure app to use bodyParser() // configure app to use bodyParser()
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // router.use(bodyParser.urlencoded({ extended: true }));
/* Decode the Authorization header if provided */ /* Decode the Authorization header if provided */
app.use(tokenToUser); // router.use(tokenToUser);
var port = 31459; // set our port var port = 31459; // set our port
var router = express.Router(); var router = express.Router();
var allowedOrigins = ['https://kevinmidboe.com', 'http://localhost:8080'] var allowedOrigins = ['https://kevinmidboe.com', 'http://localhost:8080']
// router.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* Decode the Authorization header if provided */
router.use(tokenToUser);
router.use(function(req, res, next) { router.use(function(req, res, next) {
// TODO add logging of all incoming // TODO add logging of all incoming
console.log('Request: ', req.originalUrl); console.log('Request: ', req.originalUrl);
var origin = req.headers.origin; var origin = req.headers.origin;
if (allowedOrigins.indexOf(origin) > -1) { if (allowedOrigins.indexOf(origin) > -1) {
console.log('allowed');
res.setHeader('Access-Control-Allow-Origin', origin); res.setHeader('Access-Control-Allow-Origin', origin);
} }
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
res.header('Access-Control-Allow-Methods', 'POST, GET', 'PUT');
next(); next();
}); });
@@ -36,9 +46,9 @@ router.get('/', function(req, res) {
/** /**
* User * User
*/ */
app.post('/api/v1/user', require('./controllers/user/register.js')); router.post('/v1/user', require('./controllers/user/register.js'));
app.post('/api/v1/user/login', require('./controllers/user/login.js')); router.post('/v1/user/login', require('./controllers/user/login.js'));
app.get('/api/v1/user/history', mustBeAuthenticated, require('./controllers/user/history.js')); router.get('/v1/user/history', mustBeAuthenticated, require('./controllers/user/history.js'));
/** /**
* Seasoned * Seasoned

View File

@@ -14,6 +14,7 @@ const userSecurity = new UserSecurity();
function loginController(req, res) { function loginController(req, res) {
const user = new User(req.body.username); const user = new User(req.body.username);
const password = req.body.password; const password = req.body.password;
// console.log('login: ', req.body)
userSecurity.login(user, password) userSecurity.login(user, password)
.then(() => { .then(() => {