Skip to content

Instantly share code, notes, and snippets.

View bchiang7's full-sized avatar

Brittany Chiang bchiang7

View GitHub Profile
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
set -e
echo "-----------------------------------------------"
echo " 🚀 Creating a new release 🚀"
echo "-----------------------------------------------"
// Example document in `completedTasks` collection
{
"completedAt": "2019-10-17T20:14:24.132Z",
"expirationTimestamp": "2019-10-31T08:25:00.000Z",
"userId": "xyz890",
"taskId": "abc123",
}
const User = require('../models/User.js');
await User.deleteUserById(id);
// 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
// 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();
/**
* 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) {
@bchiang7
bchiang7 / User.js
Last active November 12, 2019 18:26
// 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),
@bchiang7
bchiang7 / Base.js
Last active November 12, 2019 18:25
// models/Base.js
class Base {
constructor(args) {
const results = this.validatedData(args);
Object.assign(this, results);
}
get schema() {
throw ('You must declare a schema');
/**
* 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');
/**
* 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();