refactored auth system

pull/20/head
Ro 5 years ago
parent 12d19bc3e9
commit c549e5ba7c

@ -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: {

@ -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();

@ -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

Loading…
Cancel
Save