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

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

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for Henry Lim Henry Lim
October 27, 2018

Bringing Firebase Admin SDK to your server - DevFest x BizFest Cebu 2018

Avatar for Henry Lim

Henry Lim

October 27, 2018
Tweet

More Decks by Henry Lim

Other Decks in Technology

Transcript

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

    of SDKs, called Admin SDKs, for developing back-end software that interact with Firebase.
  2. #DevFest18 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'
 }); #DevFest18
  5. // Initialize the SDK // Cloud Functions for Firebase
 


    const functions = require('firebase-functions');
 const admin = require('firebase-admin');
 
 admin.initializeApp(functions.config().firebase); #DevFest18
  6. #DevFest18 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",
 }
 }
 } #DevFest18
  8. document.querySelector('.form').addEventListener('submit', event => {
 event.preventDefault();
 database.ref('admin').push(document.querySelector('.form #email').value)
 .then(() => {


    alert('Added successfully!');
 window.location.replace('/');
 })
 .catch(error => {
 alert(error);
 })
 }); #DevFest18
  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);
 })
 }) #DevFest18
  10. 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);
 })
 }) #DevFest18
  11. // Firebase Realtime Database Rules
 
 {
 "rules": {
 "items":

    {
 ".read": true,
 ".write": "auth.token.admin === true",
 }
 }
 } #DevFest18
  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);
 });
 });
 #DevFest18
  13. 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);
 });
 });
 #DevFest18
  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 FireLechon’,
 body: `We just added "${data.name}" to our collection. Enjoy!`
 }
 }
 return admin.messaging().send(msg)
 .then((resp) => {
 return resp;
 })
 .catch(() => {})
 }) #DevFest18
  15. 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 FireLechon’,
 body: `We just added "${data.name}" to our collection. Enjoy!`
 }
 }
 return admin.messaging().send(msg)
 .then((resp) => {
 return resp;
 })
 .catch(() => {})
 }) #DevFest18