v1.6.0/lifecycle (api logger)
This commit is contained in:
@@ -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;
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
const admin = require('firebase-admin');
|
const admin = require('firebase-admin');
|
||||||
|
const Logger = require('../Logger');
|
||||||
|
const mLogger = Logger('modules', 'authenticator');
|
||||||
|
|
||||||
module.exports = (req, res, next) => {
|
module.exports = (req, res, next) => {
|
||||||
|
mLogger.start();
|
||||||
res.set('Access-Control-Allow-Origin', ['*']);
|
res.set('Access-Control-Allow-Origin', ['*']);
|
||||||
if (req.method === 'OPTIONS') {
|
if (req.method === 'OPTIONS') {
|
||||||
|
mLogger.success('OPTIONS CALL', { URL: req.url });
|
||||||
res.status(200).send();
|
res.status(200).send();
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
@@ -16,7 +20,6 @@ module.exports = (req, res, next) => {
|
|||||||
.verifyIdToken(reqToken)
|
.verifyIdToken(reqToken)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
if (user.uid === reqUid) {
|
if (user.uid === reqUid) {
|
||||||
console.info('Verified Token - UID - ', reqUid);
|
|
||||||
if (req.method === 'POST') {
|
if (req.method === 'POST') {
|
||||||
const bodydata = JSON.parse(req.body);
|
const bodydata = JSON.parse(req.body);
|
||||||
bodydata.uid = user.uid;
|
bodydata.uid = user.uid;
|
||||||
@@ -25,18 +28,24 @@ module.exports = (req, res, next) => {
|
|||||||
if (req.method === 'GET' || req.method === 'DELETE' || req.method === 'PUT') {
|
if (req.method === 'GET' || req.method === 'DELETE' || req.method === 'PUT') {
|
||||||
req.body = JSON.stringify({ uid: user.uid });
|
req.body = JSON.stringify({ uid: user.uid });
|
||||||
}
|
}
|
||||||
|
mLogger.success('AUTHORIZED', { URL: req.url, UID: reqUid });
|
||||||
return next();
|
return next();
|
||||||
} else {
|
} else {
|
||||||
|
mLogger.success('NOT AUTHORIZED', { URL: req.url });
|
||||||
return res.status(401).send({ message: 'Unauthorized' });
|
return res.status(401).send({ message: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.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' });
|
return res.status(500).send({ message: 'Unable to verify user token' });
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} 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' });
|
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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,27 +1,21 @@
|
|||||||
const admin = require("firebase-admin");
|
const admin = require('firebase-admin');
|
||||||
|
|
||||||
module.exports = async (req, res) => {
|
module.exports = async (req, res) => {
|
||||||
const bodydata = JSON.parse(req.body);
|
const bodydata = JSON.parse(req.body);
|
||||||
console.log("Requested Create - UID - ", bodydata.uid);
|
console.log('Requested Create - UID - ', bodydata.uid);
|
||||||
if (!bodydata.userName)
|
if (!bodydata.userName) res.status(400).send({ message: 'Missing user name' });
|
||||||
res.status(400).send({ message: "Missing user name" });
|
if (!bodydata.partyName) res.status(400).send({ message: 'Missing party name' });
|
||||||
if (!bodydata.partyName)
|
if (!bodydata.partyPin) res.status(400).send({ message: 'Missing party pin' });
|
||||||
res.status(400).send({ message: "Missing party name" });
|
|
||||||
if (!bodydata.partyPin)
|
|
||||||
res.status(400).send({ message: "Missing party pin" });
|
|
||||||
const roomNameTaken = await admin
|
const roomNameTaken = await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("rooms")
|
.collection('rooms')
|
||||||
.doc(bodydata.partyName.replace("/", "\\"))
|
.doc(bodydata.partyName.replace('/', '\\'))
|
||||||
.get()
|
.get()
|
||||||
.then((docSnap) => docSnap.exists);
|
.then((docSnap) => docSnap.exists);
|
||||||
if (roomNameTaken)
|
if (roomNameTaken) return res.status(409).send({ message: 'That party name is already taken' });
|
||||||
return res
|
|
||||||
.status(409)
|
|
||||||
.send({ message: "That party name is already taken" });
|
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("rooms")
|
.collection('rooms')
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.set({
|
.set({
|
||||||
name: bodydata.partyName,
|
name: bodydata.partyName,
|
||||||
@@ -32,17 +26,17 @@ module.exports = async (req, res) => {
|
|||||||
adminOnly: bodydata.adminOnly,
|
adminOnly: bodydata.adminOnly,
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
res.status(500).send({ message: "Unable to start party." });
|
res.status(500).send({ message: 'Unable to start party.' });
|
||||||
console.error(err);
|
console.error(err);
|
||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("rooms")
|
.collection('rooms')
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection("messages")
|
.collection('messages')
|
||||||
.add({
|
.add({
|
||||||
uid: "<system>",
|
uid: '<system>',
|
||||||
name: "",
|
name: '',
|
||||||
text: `${bodydata.userName} started the party.`,
|
text: `${bodydata.userName} started the party.`,
|
||||||
time: new Date(),
|
time: new Date(),
|
||||||
})
|
})
|
||||||
@@ -54,9 +48,9 @@ module.exports = async (req, res) => {
|
|||||||
});
|
});
|
||||||
await admin
|
await admin
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("rooms")
|
.collection('rooms')
|
||||||
.doc(bodydata.partyName)
|
.doc(bodydata.partyName)
|
||||||
.collection("users")
|
.collection('users')
|
||||||
.doc(bodydata.uid)
|
.doc(bodydata.uid)
|
||||||
.set({
|
.set({
|
||||||
admin: true,
|
admin: true,
|
||||||
@@ -68,7 +62,5 @@ module.exports = async (req, res) => {
|
|||||||
err
|
err
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
res
|
res.status(201).send({ roomId: bodydata.partyName, message: 'Started a party!' });
|
||||||
.status(201)
|
|
||||||
.send({ roomId: bodydata.partyName, message: "Started a party!" });
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user