Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Bringing Firebase Admin SDK to your server - De...
Search
Henry Lim
October 27, 2018
Technology
65
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Bringing Firebase Admin SDK to your server - DevFest x BizFest Cebu 2018
Henry Lim
October 27, 2018
More Decks by Henry Lim
See All by Henry Lim
Building Malaysia Vaccine Tracker Twitter Bot
limhenry
0
56
What's New in Web 2020
limhenry
0
650
Building a Better Website with Chrome DevTools
limhenry
0
190
Bringing your PWA to the Google Play Store with Trusted Web Activities
limhenry
0
1.2k
Building a better web for everyone - DevFest George Town 2018
limhenry
0
47
Building a better web for everyone - DevFest x BizFest Cebu 2018 / DevFest Bangkok 2018
limhenry
0
140
Bringing Firebase Admin SDK to your server - Firebase Dev Day Thailand 2018
limhenry
0
210
The Modern Web: State of the Union (I/O Extended '18 Kuala Lumpur)
limhenry
0
180
Building a better web with Web Components and Polymer 2.0
limhenry
0
59
Other Decks in Technology
See All in Technology
気軽に使える"情報のハブ"としてのNotion活用 〜フロー情報の集積点 と、 Claude Code × Notion AI〜
syucream
1
180
【FinOps】データドリブンな意思決定を目指して
z63d
0
300
元銀行員がAIだけでアプリを量産!「バイブコーディング実演セミナー 」
tatsuya1970
0
110
インシデントレスポンス演習 I / Incident Response Exercise I
ks91
PRO
0
120
OTel × Datadog で 「AI活用」を計測し、改善に繋げる
shihochan
2
630
不要なレビューをAIにまかせて AIコーディングの環境改善を加速した
shoota
1
260
AIはどのように 組織のアジリティを変えるのか?
junki
4
1.3k
スタートアップにAmazon EKSは早すぎる? マルチプロダクト戦略を加速する Platform Engineeringの実践 / Is Amazon EKS Too Soon for Startups? Practical Platform Engineering to Accelerate a Multi-Product Strategy
elmodev09
1
1.8k
Zenoh on Zephyr on LiteX
takasehideki
2
110
起点・思考・出力で分解する 〜PM業務の自動化設計〜
kazu_kichi_67
1
1k
感情と身体を置き去りにしない、エンジニアの生きのこり方 ──いまから、ここから「自分の状態」を扱うという選択
saorimurooka
0
330
入門!AWS Blocks
ysuzuki
1
180
Featured
See All Featured
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
210
The Invisible Side of Design
smashingmag
301
52k
Visualization
eitanlees
152
17k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
260
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Designing for Timeless Needs
cassininazir
1
260
AI Search: Where Are We & What Can We Do About It?
aleyda
0
7.6k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
410
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
370
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
450
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
430
Transcript
Bringing Firebase Admin SDK to your server Henry Lim @henrylim96
Kamusta! Ako diay si Henry #DevFest18
#DevFest18
#DevFest18
#DevFest18 Firebase by Platform • Android • iOS • Web
• Flutter • And more ...
#DevFest18
#DevFest18 Firebase Admin SDK
#DevFest18 Firebase has got you covered Firebase provides a suite
of SDKs, called Admin SDKs, for developing back-end software that interact with Firebase.
#DevFest18 Firebase Admin SDKs Access Firebase from … • Servers
owned or managed by app developers • Cloud IaaS and PaaS environments • Serverless platforms
#DevFest18 What can you do with Firebase Admin SDK
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 ✓ ✓ ✓ ✓
#DevFest18
#DevFest18 Introducing Fire Lechon
#DevFest18
#DevFest18
#DevFest18
#DevFest18 Adding the Firebase Admin SDK
// Download the SDK (NPM) npm install firebase-admin --save
#DevFest18
#DevFest18 Initialize the Firebase Admin SDK
// 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
#DevFest18
#DevFest18
// Initialize the SDK // Cloud Functions for Firebase
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); #DevFest18
#DevFest18 A custom permissions model We want to define two
classes of users - admin and regular users.
#DevFest18 Control Access with Custom Claims and Security Rules
// Firebase Realtime Database Rules { "rules": { "items":
{ ".read": true, ".write": "auth.token.admin === true", } } } #DevFest18
#DevFest18
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
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
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
// Firebase Realtime Database Rules { "rules": { "items":
{ ".read": true, ".write": "auth.token.admin === true", } } } #DevFest18
#DevFest18
#DevFest18 Send Messages to Topics
#DevFest18
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
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
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
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
#DevFest18
#DevFest18 What else you can do with Firebase Admin SDK
#DevFest18
#DevFest18
#DevFest18
Thank You Henry Lim @henrylim96