Created
November 3, 2017 04:21
-
-
Save samthecodingman/54661827f6ceeb2d44afc1c9b2c285b7 to your computer and use it in GitHub Desktop.
Initializes a limited-access Firebase admin worker.
This file contains 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
/*! adminWorkerApp.js | Samuel Jones 2017 | MIT License | github.com/samthecodingman */ | |
/** | |
* @file Initializes a limited-access Firebase admin worker and returns it. | |
* @author Samuel Jones (github.com/samthecodingman) | |
*/ | |
// Example Database Security Rules: | |
// 1. User must have the isAdmin claim: | |
// - ".read": "auth != null && auth.token.isAdmin == true" | |
// 2. User must be the admin worker: | |
// - ".read": "auth != null && auth.uid === 'admin-worker'" | |
// 3. User can be the admin worker or a have the isAdmin claim (verbose, use option 1 when possible): | |
// - ".read": "auth != null && (auth.uid === 'admin-worker' || auth.token.isAdmin === true)" | |
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const ADMIN_WORKER_UID = 'admin-worker'; | |
const AUTH_VARIABLE = { | |
provider: 'custom', | |
uid: ADMIN_WORKER_UID, | |
token: { | |
isAdmin: true | |
} | |
} | |
const app = admin.initializeApp(Object.assign( | |
{}, | |
functions.config().firebase, | |
{ databaseAuthVariableOverride: AUTH_VARIABLE } | |
), ADMIN_WORKER_UID) | |
module.exports = app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The 'admin-worker' App
Purpose
Initialize an limited-access
firebase-admin
app (named'admin-worker'
by default) without requiring the use of try-catch blocks and repeated code. Separating this file out also allows you to include it only when needed - which is particularly useful if using an on-demand function-loader as discussed in Issue #170 of firebase/functions-samples.Usage
Example
If your database security rules are configured as follows:
The following operation using
adminWorkerApp
should always fail.But the following operation using
adminWorkerApp
should succeed.