This commit is contained in:
2020-08-11 16:06:40 -04:00
4 changed files with 119 additions and 29 deletions
+23
View File
@@ -0,0 +1,23 @@
# This is a basic workflow to help you get started with Actions
name: USER CLEAN UP
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
schedule:
- cron: "0 12 * * *"
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
webhookTrigger:
name: Webhook Trigger
runs-on: "ubuntu-latest"
steps:
- name: API CALL
uses: fjogeleit/http-request-action@master
with:
url: 'https://us-central1-watch-party-crx.cloudfunctions.net/webhooks/userCleanUp'
method: 'GET'
+66
View File
@@ -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;
+13 -4
View File
@@ -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;
};
+17 -25
View File
@@ -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!' });
};