refactored auth system

This commit is contained in:
Ro 2019-11-16 14:20:04 -08:00
parent 12d19bc3e9
commit c549e5ba7c
3 changed files with 42 additions and 15 deletions

View file

@ -1,7 +1,10 @@
import * as DataEvent from '../../utils/events/DataEvent';
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const bCrypt = require('bcrypt-nodejs'); const bCrypt = require('bcrypt-nodejs');
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const fs = require('fs-extra');
const _ = require('lodash');
/** /**
* Get Auth Status * Get Auth Status
@ -21,6 +24,34 @@ router.get('/', function(req, res) {
* Login Member and return token * Login Member and return token
*/ */
router.post('/login', function(req, res) { router.post('/login', function(req, res) {
let form = req.body;
fs.readJson('config/folks.json').then(folks => {
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'
});
}
let token = jwt.sign({ id: found.id }, 'super-secret-string', {
expiresIn: 86400 // expires in 24 hours
});
let session = req.session;
session.user = found;
session.token = token;
res.json({ type: DataEvent.REQUEST_GOOD, message: 'Yes', token: session.token });
} else {
res.json({
type: DataEvent.REQUEST_LAME,
message: 'MEMBER NOT FOUND'
});
}
});
/** /**
Models.User.findOne({ Models.User.findOne({
where: { where: {

View file

@ -1,3 +1,4 @@
export const AUTH_STATUS = 'authStatus';
export const REQUEST_GOOD = 'requestGood'; export const REQUEST_GOOD = 'requestGood';
export const REQUEST_LAME = 'requestLame'; export const REQUEST_LAME = 'requestLame';
export const IMG_REQUEST_GOOD = 'imgRequestGood'; export const IMG_REQUEST_GOOD = 'imgRequestGood';
@ -21,17 +22,12 @@ export const LOCAL_DB_READY = 'localDBReady';
export const SETTINGS_UPDATED = 'settingsUpdated'; export const SETTINGS_UPDATED = 'settingsUpdated';
export const AVATAR_UPLOADED = 'avatarUploaded'; export const AVATAR_UPLOADED = 'avatarUploaded';
export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded'; export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded';
class DataEvent class DataEvent {
{ //--------------------------
// methods
//-------------------------- //--------------------------
// methods //--------------------------
//-------------------------- // event handlers
//--------------------------
//--------------------------
// event handlers
//--------------------------
} }
export default new DataEvent export default new DataEvent();

View file

@ -130,8 +130,8 @@ class DataUtils extends EventEmitter {
} }
object[key].push(value); object[key].push(value);
}); });
let json = JSON.stringify(object); //let json = JSON.stringify(object);
return json; return object;
} }
//-------------------------- //--------------------------
// event handlers // event handlers