Removing Loki.js for cloud functions

This commit is contained in:
Omar 2019-02-25 00:09:11 -08:00
parent 634df68529
commit e684a2c926
5 changed files with 47 additions and 93 deletions

View File

@ -4,7 +4,6 @@ const uuid = require('uuid/v4');
const utils = require('../utils/utils');
const errors = require('../../config/errors');
const loki = require('../../config/loki');
const twilio = require('../../config/twilio');
const appConfig = require('../../config/app.json');
@ -114,16 +113,17 @@ function resetPassword(email) {
}
/**
* Verifies that the saved code is the same as the user inputed code
*
* @param {string} phone
* @param {number} usrCode
* @param {String} phoneNumber Phone Number in E.164 format
* @param {String} usrCode User supplied code
*/
function isVerificationCodeCorrect(phoneNumber, usrCode) {
async function isVerificationCodeCorrect(phoneNumber, usrCode) {
try {
const sysCode = getVerificationCode(phoneNumber);
return {result: usrCode == sysCode};
const sysCode = await getVerificationCode(phoneNumber);
return { result: usrCode == sysCode };
} catch (error) {
return {error};
return { error };
}
}
@ -132,8 +132,8 @@ function isVerificationCodeCorrect(phoneNumber, usrCode) {
* @param {string} phoneNumber
* @param {function} callback
*/
function sendVerificationCode(phoneNumber, callback) {
const code = createNewVerificationCode(phoneNumber);
async function sendVerificationCode(phoneNumber, callback) {
const code = await createNewVerificationCode(phoneNumber);
const message = `Tagfer PIN: ${code}`;
twilio.sendSMSTo(phoneNumber, message, callback);
}
@ -160,75 +160,36 @@ async function sendMassTextInvites(phoneNumbers = [], fullName, tagferId) {
}
}
/**
* Creates a new session for security of users and to maintain states of the requests.
* @returns Session identifier to manage state of the user
*/
function createNewSessionId(tagferId) {
function createNewSession(tagferId) {
const sessionId = uuid();
const sessions = loki.getCollection('sessions');
sessions.insert({ id: sessionId, tagferId });
return sessionId;
return admin.database().ref(`sessions/${sessionId}`).update({ tagferId }).then(() => sessionId).catch(utils.dbErrorHandler);
}
/**
* Gets the loki session containg the user's tagferId on the server
* @param {string} id
* @returns {object} Loki Session object containg user's tagferId or error if id param passed is invalid
* @throws AUTH_INVALID_SESSION_ID
*/
function getSession(id) {
const sessions = loki.getCollection('sessions');
const session = sessions.by('id', id);
if (session) {
return session;
async function getSession(sessionId) {
const session = await admin.database().ref(`sessions/${sessionId}`).once('value').catch(utils.dbErrorHandler);
if (session.exists()) {
return session.val();
} else {
throw errors.AUTH_INVALID_SESSION_ID;
}
}
/**
* Removes the loki session containing the tagferId on the server
* @param {String} id
*/
function deleteSession(id) {
const sessions = loki.getCollection('sessions');
const session = getSession(id);
sessions.remove(session);
function deleteSession(sessionId) {
admin.database().ref(`sessions/${sessionId}`).remove().catch(utils.dbErrorHandler);
}
/**
* Create new verification code for the phone number
*/
function createNewVerificationCode(phoneNumber) {
const code = Math.floor(((Math.random() * 899999) + 100000));
const verifications = loki.getCollection('verifications');
try {
verifications.insert({ phoneNumber, code });
} catch (error){
const record = verifications.by('phoneNumber', phoneNumber);
record.code = code;
verifications.update(record);
}
return code;
return admin.database().ref(`verifications/${phoneNumber}`).set(code).then(() => code).catch(utils.dbErrorHandler);
}
/**
* Gets the verification code from loki by phone number
* @param {string} phoneNumber phone number
*/
function getVerificationCode(phoneNumber) {
const verifications = loki.getCollection('verifications');
const record = verifications.by('phoneNumber', phoneNumber);
if (!record) {
async function getVerificationCode(phoneNumber) {
const verification = await admin.database().ref(`verifications/${phoneNumber}`).once('value').catch(utils.dbErrorHandler);
if (verification.exists()) {
return verification.val();
} else {
throw errors.AUTH_PHONE_NOT_CACHED;
}
return record.code;
}
function _doesKeyValueExist({email, tagferId, phoneNumber}) {
@ -265,7 +226,7 @@ module.exports = {
signinWithTagferId,
signinWithEmail,
createNewUser,
createNewSessionId,
createNewSession,
getSession,
deleteSession,
resetPassword,

View File

@ -30,14 +30,14 @@ function doesAttributeExist(req, res) {
* @param {Object} req {}
* @param {Object} res { result: Boolean }
*/
function doesSessionExist(req, res) {
async function doesSessionExist(req, res) {
const { sessionId } = req.params;
if (!utils.isAppSecretValid(req,res)) {
return;
}
try {
authDao.getSession(sessionId);
await authDao.getSession(sessionId);
res.json({ result: true});
} catch(error) {
res.json({ result: false });
@ -89,27 +89,20 @@ function verifyPhoneCode(req, res) {
* @param {Object} req {email: String, password: String} | {tagferId: String, password: String}
* @param {Object} res {sessionId: String} | {error: String}
*/
function signin(req, res) {
async function signin(req, res) {
const { email, tagferId, password } = req.body;
const verifier = () => (email || tagferId) && password;
if (!utils.isAppSecretValid(req,res) || !utils.isBodyValid(verifier, res)) {
return;
}
var promise;
if (email) {
promise = authDao.signinWithEmail(email, password);
} else {
promise = authDao.signinWithTagferId(tagferId, password);
}
promise.then( ({ user })=> {
const sessionId = authDao.createNewSessionId(user.uid);
try {
const { user } = await (email ? authDao.signinWithEmail(email, password) : authDao.signinWithTagferId(tagferId, password));
const sessionId = await authDao.createNewSession(user.uid);
res.json({ sessionId });
}).catch(error => {
res.json({ error: error.code });
});
} catch (error) {
res.json({ error: error.code ? error.code : error });
}
}
/**
@ -118,11 +111,11 @@ function signin(req, res) {
* @param {Object} req {}
* @param {Object} res { error: String } | {}
*/
function signout(req, res) {
async function signout(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req);
try {
authDao.deleteSession(sessionId);
await authDao.deleteSession(sessionId);
res.json({});
} catch(error) {
res.json({ error });
@ -153,7 +146,7 @@ async function signup(req, res) {
await authDao.signup(tagferId, user.phoneNumber, profileObject, invites.requests);
// CREATE NEW SESSION ID
const sessionId = authDao.createNewSessionId(tagferId);
const sessionId = await authDao.createNewSession(tagferId);
res.json({ sessionId });
// INVITES SENT IN THE BACKGROUND

View File

@ -7,7 +7,7 @@ async function getConnectionRequests(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
const tagferId = (await authDao.getSession(sessionId)).tagferId;
const requests = await connDao.getConnectionRequests(tagferId);
res.send(requests);
} catch (error) {
@ -20,7 +20,7 @@ async function getAllConnections(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
const tagferId = (await authDao.getSession(sessionId)).tagferId;
const conns = await connDao.getAllConnections(tagferId);
res.send({
profile1: conns[0],
@ -40,7 +40,7 @@ async function sendConnectionRequest(req, res) {
const toTagferId = req.body.toTagferId;
try {
const fromTagferId = authDao.getSession(sessionId).tagferId;
const fromTagferId = (await authDao.getSession(sessionId)).tagferId;
await connDao.createConnectionRequest(fromTagferId, fromProfileN, toTagferId);
res.send({});
} catch (error) {
@ -53,7 +53,7 @@ async function removeConnectionRequest(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const myTagferId = authDao.getSession(sessionId).tagferId;
const myTagferId = (await authDao.getSession(sessionId)).tagferId;
const fromTagferId = req.body.fromTagferId || myTagferId;
const toTagferId = req.body.toTagferId || myTagferId;
@ -70,7 +70,7 @@ async function acceptConnectionRequest(req, res) {
try {
// Connection request is accepted by the user is the TO_SIDE, he received a request FROM_SIDE
const toTagferId = authDao.getSession(sessionId).tagferId;
const toTagferId = (await authDao.getSession(sessionId)).tagferId;
const toProfileN = parseInt(req.params.profileN);
const fromTagferId = req.body.fromTagferId;
const fromProfileN = req.body.fromProfileN;

View File

@ -15,7 +15,7 @@ async function createNote(req,res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const fromTagferId = authDao.getSession(sessionId).tagferId;
const fromTagferId = (await authDao.getSession(sessionId)).tagferId;
const noteId = await notesDao.createNote(fromTagferId, toTagferId, noteObject);
res.json({ noteId });
} catch (error) {
@ -35,7 +35,7 @@ async function updateNote(req,res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const fromTagferId = authDao.getSession(sessionId).tagferId;
const fromTagferId = (await authDao.getSession(sessionId)).tagferId;
const noteId = await notesDao.updateNote(fromTagferId, toTagferId, noteObject);
res.json({ noteId });
} catch (error) {
@ -55,7 +55,7 @@ async function deleteNote(req,res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const fromTagferId = authDao.getSession(sessionId).tagferId;
const fromTagferId = (await authDao.getSession(sessionId)).tagferId;
await notesDao.deleteNote(fromTagferId, toTagferId, noteId);
res.json({});
} catch (error) {
@ -74,7 +74,7 @@ async function getAllNotes(req,res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const fromTagferId = authDao.getSession(sessionId).tagferId;
const fromTagferId = (await authDao.getSession(sessionId)).tagferId;
const allNotes = await notesDao.getAllNotes(fromTagferId, toTagferId);
res.json({ notes: allNotes });
} catch (error) {

View File

@ -17,7 +17,7 @@ async function updateUserProfile(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
const tagferId = (await authDao.getSession(sessionId)).tagferId;
await profileDao.updateProfile(profileObj, profileNumber, tagferId);
res.json({});
} catch (error) {
@ -30,7 +30,7 @@ async function getUserProfile(req, res) {
const sessionId = utils.getSessionIdFromAuthHeader(req, res);
try {
const tagferId = authDao.getSession(sessionId).tagferId;
const tagferId = (await authDao.getSession(sessionId)).tagferId;
const profile = await profileDao.getProfile(profileNumber, tagferId);
res.json({ ...profile });
} catch (error) {