2019-11-23 17:40:51 +01:00
|
|
|
import * as DataEvent from '../../../src/com/events/DataEvent';
|
2019-10-12 01:10:55 +02:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
|
|
|
const bCrypt = require('bcrypt-nodejs');
|
|
|
|
const jwt = require('jsonwebtoken');
|
2019-11-16 23:20:04 +01:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const _ = require('lodash');
|
2019-12-02 22:07:16 +01:00
|
|
|
//const crypto = require('crypto'); // for setting up new accounts
|
2019-10-12 01:10:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Auth Status
|
|
|
|
*/
|
|
|
|
router.get('/', function(req, res) {
|
|
|
|
var token = req.headers['x-access-token'];
|
|
|
|
if (!token) return res.status(401).send({ auth: false, message: 'No token provided.' });
|
|
|
|
|
|
|
|
jwt.verify(token, 'super-secret-string', function(err, decoded) {
|
|
|
|
if (err)
|
|
|
|
return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
|
|
|
|
res.status(200).send(decoded);
|
|
|
|
});
|
|
|
|
});
|
2019-12-02 22:07:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get Auth Status
|
|
|
|
*/
|
|
|
|
router.get('/status', function(req, res) {
|
|
|
|
if (req.session.user) {
|
|
|
|
let session = req.session;
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.API_REQUEST_GOOD,
|
|
|
|
message: 'Auth is Good',
|
2019-12-03 02:59:04 +01:00
|
|
|
token: session.hashToken
|
2019-12-02 22:07:16 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.API_REQUEST_LAME,
|
|
|
|
message: 'NOT AUTHORIZED'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 01:10:55 +02:00
|
|
|
/**
|
|
|
|
* Login Member and return token
|
|
|
|
*/
|
|
|
|
router.post('/login', function(req, res) {
|
2019-11-23 19:30:34 +01:00
|
|
|
fs.readJson('site/folks.json').then(folks => {
|
2019-11-16 23:20:04 +01:00
|
|
|
let found = _.find(folks, { handle: req.body.handle });
|
|
|
|
if (found) {
|
|
|
|
if (!isValidPassword(found, req.body.password)) {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
|
|
|
message: 'CHECK YOUR PASSWORD'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-02 22:07:16 +01:00
|
|
|
let token = jwt.sign({ id: found.id }, found.key, {
|
2019-11-16 23:20:04 +01:00
|
|
|
expiresIn: 86400 // expires in 24 hours
|
|
|
|
});
|
|
|
|
|
|
|
|
let session = req.session;
|
|
|
|
session.user = found;
|
|
|
|
session.token = token;
|
2019-12-03 02:59:04 +01:00
|
|
|
session.hashToken = hashToken(token);
|
2019-11-24 21:23:01 +01:00
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_GOOD,
|
|
|
|
message: 'Welcome Back',
|
2019-12-03 02:59:04 +01:00
|
|
|
token: session.hashToken
|
2019-11-24 21:23:01 +01:00
|
|
|
});
|
2019-11-16 23:20:04 +01:00
|
|
|
} else {
|
|
|
|
res.json({
|
|
|
|
type: DataEvent.REQUEST_LAME,
|
2019-11-25 22:58:34 +01:00
|
|
|
message: 'Need to see some id, champ.'
|
2019-11-16 23:20:04 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-10-12 01:10:55 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
//router.post('/logout', function(req, res) {});
|
|
|
|
module.exports = router;
|
|
|
|
|
|
|
|
function isValidPassword(user, password) {
|
|
|
|
return bCrypt.compareSync(password, user.password);
|
|
|
|
}
|
2019-12-03 02:59:04 +01:00
|
|
|
|
|
|
|
function hashToken(token) {
|
|
|
|
return bCrypt.hashSync(token, bCrypt.genSaltSync(10), null);
|
|
|
|
}
|