From b9c8de2285310db463df4494ea8bcf9c6d63972c Mon Sep 17 00:00:00 2001 From: Ayushya Amitabh Date: Mon, 10 Aug 2020 22:39:09 -0400 Subject: [PATCH] v1.6.0/lifecycle (api logger) --- functions/Logger.js | 66 ++++++++++++++++++++++++++++++ functions/modules/authenticator.js | 17 ++++++-- functions/modules/createRoom.js | 42 ++++++++----------- 3 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 functions/Logger.js diff --git a/functions/Logger.js b/functions/Logger.js new file mode 100644 index 0000000..1c009cb --- /dev/null +++ b/functions/Logger.js @@ -0,0 +1,66 @@ +class Logger { + /** + * + * @param {string} m Module Name + * @param {string} e Endpoint Function Name + */ + constructor(m, e) { + this.__module = m; + this.__endpoint = e; + this.log = this.log.bind(this); + this.start = this.start.bind(this); + this.error = this.error.bind(this); + this.success = this.success.bind(this); + this.__getVString = this.__getVString.bind(this); + } + + /** + * + * @param {object} v variables to log + */ + __getVString(v = {}) { + var vString = ''; + Object.keys(v).map((k) => (vString += `${k}: ${v[k]} `)); + return vString; + } + + /** + * + * @param {string} e error message + * @param {object} v variables to log + */ + error(e = 'ERROR LOG', v = {}) { + console.error(`[ERROR] ${this.__module} / ${this.__endpoint} / ${e}\t ${this.__getVString(v)}`); + } + + /** + * + * @param {object} v variables to log + */ + start(v = {}) { + console.info(`[START] ${this.__module} / ${this.__endpoint}\t ${this.__getVString(v)}`); + } + + /** + * + * @param {string} m log message + * @param {object} v variables to log + */ + success(m = '', v = {}) { + console.info(`[DONE] ${this.__module} / ${this.__endpoint} / ${m}\t ${this.__getVString(v)}`); + } + + /** + * + * @param {string} d description like [DONE, ERROR, INFO] + * @param {string} m log message + * @param {object} v variables to log + */ + log(d = 'CUSTOM', m = '', v = {}) { + console.info( + `[(${d})] ${this.__module} / ${this.__endpoint} / ${m} \t ${this.__getVString(v)}` + ); + } +} + +module.exports = Logger; diff --git a/functions/modules/authenticator.js b/functions/modules/authenticator.js index b7aa87a..e0419b5 100644 --- a/functions/modules/authenticator.js +++ b/functions/modules/authenticator.js @@ -1,8 +1,12 @@ const admin = require('firebase-admin'); +const Logger = require('../Logger'); +const mLogger = Logger('modules', 'authenticator'); module.exports = (req, res, next) => { + mLogger.start(); res.set('Access-Control-Allow-Origin', ['*']); if (req.method === 'OPTIONS') { + mLogger.success('OPTIONS CALL', { URL: req.url }); res.status(200).send(); return next(); } @@ -16,7 +20,6 @@ module.exports = (req, res, next) => { .verifyIdToken(reqToken) .then((user) => { if (user.uid === reqUid) { - console.info('Verified Token - UID - ', reqUid); if (req.method === 'POST') { const bodydata = JSON.parse(req.body); bodydata.uid = user.uid; @@ -25,18 +28,24 @@ module.exports = (req, res, next) => { if (req.method === 'GET' || req.method === 'DELETE' || req.method === 'PUT') { req.body = JSON.stringify({ uid: user.uid }); } + mLogger.success('AUTHORIZED', { URL: req.url, UID: reqUid }); return next(); } else { + mLogger.success('NOT AUTHORIZED', { URL: req.url }); return res.status(401).send({ message: 'Unauthorized' }); } }) .catch((err) => { - console.error('Failed Auth - ', req.headers.authorization, '\n', err); + mLogger.error('AUTH VALIDATE FIREBASE ERROR', { URL: req.url, Error: err }); return res.status(500).send({ message: 'Unable to verify user token' }); }); } catch (err) { - console.log(err); + mLogger.error('AUTH VALIDATE ERROR', { URL: req.url, Error: err }); return res.status(401).send({ message: 'Something went wrong when authorizing' }); } - } else return res.status(401).send({ message: 'Got no user token' }); + } else { + mLogger.error('NO AUTH TOKEN', { URL: req.url }); + res.status(401).send({ message: 'Got no user token' }); + } + return 0; }; diff --git a/functions/modules/createRoom.js b/functions/modules/createRoom.js index 0ab8529..2a9b22c 100644 --- a/functions/modules/createRoom.js +++ b/functions/modules/createRoom.js @@ -1,27 +1,21 @@ -const admin = require("firebase-admin"); +const admin = require('firebase-admin'); module.exports = async (req, res) => { const bodydata = JSON.parse(req.body); - console.log("Requested Create - UID - ", bodydata.uid); - if (!bodydata.userName) - res.status(400).send({ message: "Missing user name" }); - if (!bodydata.partyName) - res.status(400).send({ message: "Missing party name" }); - if (!bodydata.partyPin) - res.status(400).send({ message: "Missing party pin" }); + console.log('Requested Create - UID - ', bodydata.uid); + if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' }); + if (!bodydata.partyName) res.status(400).send({ message: 'Missing party name' }); + if (!bodydata.partyPin) res.status(400).send({ message: 'Missing party pin' }); const roomNameTaken = await admin .firestore() - .collection("rooms") - .doc(bodydata.partyName.replace("/", "\\")) + .collection('rooms') + .doc(bodydata.partyName.replace('/', '\\')) .get() .then((docSnap) => docSnap.exists); - if (roomNameTaken) - return res - .status(409) - .send({ message: "That party name is already taken" }); + if (roomNameTaken) return res.status(409).send({ message: 'That party name is already taken' }); await admin .firestore() - .collection("rooms") + .collection('rooms') .doc(bodydata.partyName) .set({ name: bodydata.partyName, @@ -32,17 +26,17 @@ module.exports = async (req, res) => { adminOnly: bodydata.adminOnly, }) .catch((err) => { - res.status(500).send({ message: "Unable to start party." }); + res.status(500).send({ message: 'Unable to start party.' }); console.error(err); }); await admin .firestore() - .collection("rooms") + .collection('rooms') .doc(bodydata.partyName) - .collection("messages") + .collection('messages') .add({ - uid: "", - name: "", + uid: '', + name: '', text: `${bodydata.userName} started the party.`, time: new Date(), }) @@ -54,9 +48,9 @@ module.exports = async (req, res) => { }); await admin .firestore() - .collection("rooms") + .collection('rooms') .doc(bodydata.partyName) - .collection("users") + .collection('users') .doc(bodydata.uid) .set({ admin: true, @@ -68,7 +62,5 @@ module.exports = async (req, res) => { err ); }); - res - .status(201) - .send({ roomId: bodydata.partyName, message: "Started a party!" }); + res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' }); };