Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bringing Firebase Admin SDK to your server - Fi...

Henry Lim
October 07, 2018

Bringing Firebase Admin SDK to your server - Firebase Dev Day Thailand 2018

Henry Lim

October 07, 2018
Tweet

More Decks by Henry Lim

Other Decks in Technology

Transcript

  1. #FirebaseDevDay Firebase has got you covered Firebase provides a suite

    of SDKs, called Admin SDKs, for developing back-end software that interact with Firebase.
  2. #FirebaseDevDay Firebase Admin SDKs Access Firebase from … • Servers

    owned or managed by app developers • Cloud IaaS and PaaS environments • Serverless platforms
  3. Firebase Admin SDK Features Feature Node.js Java Python Go C#

    Custom Token Minting ✓ ✓ ✓ ✓ ✓ ID Token Verification ✓ ✓ ✓ ✓ ✓ User Management ✓ ✓ ✓ ✓ Control Access With Custom Claims ✓ ✓ ✓ ✓ Refresh Token Revocation ✓ ✓ ✓ ✓ Import Users ✓ ✓ ✓ ✓ Session Cookie Management ✓ ✓ ✓ Realtime Database ✓ ✓ ✓ ✓ Cloud Messaging ✓ ✓ ✓ ✓ Manage Topic Subscriptions ✓ ✓ ✓ ✓ Cloud Storage ✓ ✓ ✓ ✓ Cloud Firestore ✓ ✓ ✓ ✓
  4. // Initialize the SDK const admin = require('firebase-admin'); const serviceAccount

    = require('path/to/serviceAccountKey.json'); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), databaseURL: 'https://<DATABASE_NAME>.firebaseio.com' });
  5. // Initialize the SDK // Cloud Functions for Firebase const

    functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase);
  6. #FirebaseDevDay A custom permissions model We want to define two

    classes of users - admin and regular users.
  7. // Firebase Realtime Database Rules { "rules": { "items": {

    ".read": true, ".write": "auth.token.admin === true", } } }
  8. exports.newAdmin = functions.database.ref(`admin/{email}`) .onCreate((snapshot, context) => { const email =

    context.params.email; admin.auth().getUserByEmail(email) .then(userRecord => { return userRecord.uid; }) .then(uid => { return admin.auth().setCustomUserClaims(uid, {admin: true}); }) .catch(error => { console.log(error); }) })
  9. exports.newAdmin = functions.database.ref(`admin/{email}`) .onCreate((snapshot, context) => { const email =

    context.params.email; admin.auth().getUserByEmail(email) .then(userRecord => { return userRecord.uid; }) .then(uid => { return admin.auth().setCustomUserClaims(uid, {admin: true}); }) .catch(error => { console.log(error); }) })
  10. // Firebase Realtime Database Rules { "rules": { "items": {

    ".read": true, ".write": "auth.token.admin === true", } } }
  11. var messaging = firebase.messaging(); var database = firebase.database(); if ('serviceWorker'

    in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/firebase-messaging-sw.js') .then((registration) => { messaging.useServiceWorker(registration); }) .then(() => { return messaging.requestPermission(); }) .then(() => { return messaging.getToken(); }) .then((token) => { database.ref('notification/' + token).set(true); }) .catch((err) => { console.log('ServiceWorker registration failed: ', err); }); }); };
  12. var messaging = firebase.messaging(); var database = firebase.database(); if ('serviceWorker'

    in navigator) { window.addEventListener('load', () => { navigator.serviceWorker.register('/firebase-messaging-sw.js') .then((registration) => { messaging.useServiceWorker(registration); }) .then(() => { return messaging.requestPermission(); }) .then(() => { return messaging.getToken(); }) .then((token) => { database.ref('notification/' + token).set(true); }) .catch((err) => { console.log('ServiceWorker registration failed: ', err); }); }); };
  13. exports.sendNotification = functions.database.ref(`items/{itemId}`) .onCreate((snapshot, context) => { const data =

    snapshot.val(); const msg = admin.messaging.Message = { topic: 'new-restaurant', notification: { title: 'New Restaurant in FireTomyam', body: `We just added "${data.name}" to our collection. Enjoy!` } } return admin.messaging().send(msg) .then((resp) => { return resp; }) .catch(() => {}) })
  14. exports.sendNotification = functions.database.ref(`items/{itemId}`) .onCreate((snapshot, context) => { const data =

    snapshot.val(); const msg = admin.messaging.Message = { topic: 'new-restaurant', notification: { title: 'New Restaurant in FireTomyam', body: `We just added "${data.name}" to our collection. Enjoy!` } } return admin.messaging().send(msg) .then((resp) => { return resp; }) .catch(() => {}) })