Skip to content

Instantly share code, notes, and snippets.

@lucacasonato
Created October 27, 2018 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucacasonato/923cd8d0f20e51b29c3e3cf96cd52586 to your computer and use it in GitHub Desktop.
Save lucacasonato/923cd8d0f20e51b29c3e3cf96cd52586 to your computer and use it in GitHub Desktop.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// On sign up.
exports.processSignUp = functions.auth.user().onCreate(event => {
const user = event.data; // The Firebase user.
// Check if user meets role criteria.
if (user.email &&
user.email.indexOf('@admin.example.com') != -1 &&
user.emailVerified) {
const customClaims = {
admin: true,
accessLevel: 9
};
// Set custom user claims on this newly created user.
return admin.auth().setCustomUserClaims(user.uid, customClaims)
.then(() => {
// Update real-time database to notify client to force refresh.
const metadataRef = admin.database().ref("metadata/" + user.uid);
// Set the refresh time to the current UTC timestamp.
// This will be captured on the client to force a token refresh.
return metadataRef.set({refreshTime: new Date().getTime()});
})
.catch(error => {
console.log(error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment