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
71
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
59
What's New in Web 2020
limhenry
0
650
Building a Better Website with Chrome DevTools
limhenry
0
200
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
52
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
63
Other Decks in Technology
See All in Technology
猫付きpingコマンドを自作
uyuki234
0
290
AI時代のアウトプット――変わったこと、変わらないこと / Devsumi 2026 Kansai #devsumi
jnchito
0
510
AIで変わるエンジニアの働き方(仮)
naoinaoi
0
450
平文パスワードはログに“残り” ── 肝心の侵入は“痕跡すら残らない”
kuroneko13
0
120
FDEの心得
noriakioji
5
6.4k
AIペネトレーションテスト・ セキュリティ検証「AgenticSec」紹介資料
laysakura
2
9.4k
VLMで2.3万枚のPyCon JP写真を検索!
terapyon
1
310
Head First モブプログラミング / Head First Mobprogramming
takaking22
10
12k
【書籍出版記念】 10周回って、エージェント開発は RAGがすべてだった。〜RAGの歴史と開発現場で見えた実践知〜
akiratameto
18
6.5k
MIXIで活躍できるエンジニアを 若手社員目線で考えてみる
mixi_engineers
PRO
0
130
暗号化?某ファイルストレージはどうなるの!? 3rd Partyとうまく付き合う秘密度ラベル設計
kasada
0
140
AI・HPC開発を支えるGPU環境の新しい選択肢 液冷GPUシステム「AquSys」の取り組み
gpuunite_official
0
230
Featured
See All Featured
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
380
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
480
YesSQL, Process and Tooling at Scale
rocio
174
15k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Making Projects Easy
brettharned
120
6.7k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
430
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.1k
Fireside Chat
paigeccino
42
4k
The Cult of Friendly URLs
andyhume
79
7k
Balancing Empowerment & Direction
lara
6
1.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
410
Building AI with AI
inesmontani
PRO
1
1.2k
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