From c549e5ba7cd3f63e4f1ac6ad6d3f540649e13318 Mon Sep 17 00:00:00 2001 From: Ro Date: Sat, 16 Nov 2019 14:20:04 -0800 Subject: [PATCH] refactored auth system --- brain/api/v1/auth.js | 31 +++++++++++++++++++++++++++++++ brain/utils/events/DataEvent.js | 22 +++++++++------------- brain/utils/tools/DataUtils.js | 4 ++-- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/brain/api/v1/auth.js b/brain/api/v1/auth.js index 134139e..a91958b 100644 --- a/brain/api/v1/auth.js +++ b/brain/api/v1/auth.js @@ -1,7 +1,10 @@ +import * as DataEvent from '../../utils/events/DataEvent'; const express = require('express'); const router = express.Router(); const bCrypt = require('bcrypt-nodejs'); const jwt = require('jsonwebtoken'); +const fs = require('fs-extra'); +const _ = require('lodash'); /** * Get Auth Status @@ -21,6 +24,34 @@ router.get('/', function(req, res) { * Login Member and return token */ 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({ where: { diff --git a/brain/utils/events/DataEvent.js b/brain/utils/events/DataEvent.js index 5dbebde..d0b4358 100644 --- a/brain/utils/events/DataEvent.js +++ b/brain/utils/events/DataEvent.js @@ -1,3 +1,4 @@ +export const AUTH_STATUS = 'authStatus'; export const REQUEST_GOOD = 'requestGood'; export const REQUEST_LAME = 'requestLame'; export const IMG_REQUEST_GOOD = 'imgRequestGood'; @@ -21,17 +22,12 @@ export const LOCAL_DB_READY = 'localDBReady'; export const SETTINGS_UPDATED = 'settingsUpdated'; export const AVATAR_UPLOADED = 'avatarUploaded'; export const SITE_BACKGROUND_UPLOADED = 'siteBackgroundUploaded'; -class DataEvent -{ - - //-------------------------- - // methods - //-------------------------- - - - - //-------------------------- - // event handlers - //-------------------------- +class DataEvent { + //-------------------------- + // methods + //-------------------------- + //-------------------------- + // event handlers + //-------------------------- } -export default new DataEvent +export default new DataEvent(); diff --git a/brain/utils/tools/DataUtils.js b/brain/utils/tools/DataUtils.js index fadf42f..1ccec78 100644 --- a/brain/utils/tools/DataUtils.js +++ b/brain/utils/tools/DataUtils.js @@ -130,8 +130,8 @@ class DataUtils extends EventEmitter { } object[key].push(value); }); - let json = JSON.stringify(object); - return json; + //let json = JSON.stringify(object); + return object; } //-------------------------- // event handlers