moved auth methods to their own standalone class
parent
ea1795e0fa
commit
52bb5f36a9
@ -0,0 +1,68 @@
|
||||
import * as DataEvent from '../../src/com/events/DataEvent';
|
||||
const bCrypt = require('bcrypt-nodejs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
export default class Auth {
|
||||
//--------------------------
|
||||
// constructor
|
||||
//--------------------------
|
||||
constructor() {}
|
||||
//--------------------------
|
||||
// methods
|
||||
//--------------------------
|
||||
start() {}
|
||||
|
||||
/**
|
||||
* Makes sure access token is legit
|
||||
* @parameter req
|
||||
*/
|
||||
|
||||
authCheck(req) {
|
||||
let self = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let hash = req.headers['x-access-token'];
|
||||
let response = [];
|
||||
//Checks if token is a proper hash, if not reject
|
||||
if (!self.isTokenValid(req.session.token, hash)) {
|
||||
response = {
|
||||
status: false,
|
||||
type: DataEvent.API_REQUEST_LAME,
|
||||
message: 'No Token Present. Auth Blocked'
|
||||
};
|
||||
reject(response);
|
||||
//res.json();
|
||||
} else {
|
||||
var member = req.session.user;
|
||||
jwt.verify(req.session.token, member.key, function (err, decoded) {
|
||||
if (err) {
|
||||
response = {
|
||||
status: false,
|
||||
type: DataEvent.API_REQUEST_LAME,
|
||||
message: 'Invalid Token. Auth Blocked'
|
||||
};
|
||||
reject(response);
|
||||
}
|
||||
response = {
|
||||
status: true,
|
||||
type: DataEvent.API_REQUEST_GOOD,
|
||||
message: 'Token Verified',
|
||||
token: decoded
|
||||
};
|
||||
resolve(response);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to make sure received token matches
|
||||
* @parameter token: created token
|
||||
* @parameter hashedToken: encrypted token
|
||||
*/
|
||||
isTokenValid(token, hashedToken) {
|
||||
return bCrypt.compareSync(token, hashedToken);
|
||||
}
|
||||
//--------------------------
|
||||
// event handlers
|
||||
//--------------------------
|
||||
}
|
Loading…
Reference in New Issue