v1.6.0/lifecycle (api logger)
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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: "<system>",
|
||||
name: "",
|
||||
uid: '<system>',
|
||||
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!' });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user