This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
bold=$(tput bold) | |
normal=$(tput sgr0) | |
set -e | |
echo "-----------------------------------------------" | |
echo " 🚀 Creating a new release 🚀" | |
echo "-----------------------------------------------" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example document in `completedTasks` collection | |
{ | |
"completedAt": "2019-10-17T20:14:24.132Z", | |
"expirationTimestamp": "2019-10-31T08:25:00.000Z", | |
"userId": "xyz890", | |
"taskId": "abc123", | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const User = require('../models/User.js'); | |
await User.deleteUserById(id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Deletes a user by ID in firestore user collection & firebase auth | |
static async deleteUserById(id) { | |
const userRef = firestore | |
.collection(this.collectionName()) | |
.doc(id); | |
const batch = firestore.batch(); | |
batch.delete(userRef); | |
const completedTasksToDelete = await firestore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new User model instance | |
const harryPotter = new User({ | |
id: 'abc123', | |
email: '[email protected]', | |
displayName: 'The Boy Who Lived', | |
... | |
}); | |
// Add it as a document in the `users` collection in Firestore | |
harryPotter.create(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fetches the session user from cache if it exists, or sets it if it does not | |
*/ | |
const getOrSetUserFromCache = async headers => { | |
const User = require('../models/User'); | |
const userUid = headers['x-userid']; | |
try { | |
const firebaseUserUid = await User.getUserIdFromToken(headers.authorization); | |
if (!userUid || firebaseUserUid !== userUid) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// models/User.js | |
class User extends Base { | |
// Expected schema of the User record | |
get schema() { | |
return { | |
id: Joi.string(), | |
createdAt: Joi.string().isoDate(), | |
displayName: Joi.string().allow('').allow(null).default(''), | |
email: Joi.string().email({ minDomainSegments: 2 }).default(''), | |
isAdmin: Joi.bool().default(false), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// models/Base.js | |
class Base { | |
constructor(args) { | |
const results = this.validatedData(args); | |
Object.assign(this, results); | |
} | |
get schema() { | |
throw ('You must declare a schema'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Delete a user with the given ID | |
*/ | |
const deleteUser = async (req, res, next) => { | |
const { id } = req.params; | |
try { | |
await User.deleteUserById(id); | |
res.status(202).json({ message: `User ${id} deleted!` }); | |
} catch (e) { | |
const err = new Error('Could not delete user'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check if a user has access to the app admin | |
*/ | |
const isAdmin = async (req, res, next) => { | |
if (req.headers && req.headers.authorization) { | |
if (!req.user || !req.user.isAdmin) { | |
return res.status(403).json({ message: 'You must have admin permissions.' }); | |
} | |
// If we get here, we're good so pass it along | |
next(); |