Skip to content

Instantly share code, notes, and snippets.

@feulf
Created October 19, 2018 21: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 feulf/74c6a8989bc072b8ca445fa0f60cdd72 to your computer and use it in GitHub Desktop.
Save feulf/74c6a8989bc072b8ca445fa0f60cdd72 to your computer and use it in GitHub Desktop.
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp()
const db = admin.firestore()
const settings = {timestampsInSnapshots: true};
db.settings(settings);
const express = require('express')
const app = express()
const authenticate = async (req, res, next) => {
if (!req.headers.authorization || !req.headers.authorization.startsWith('Bearer ')) {
return res.status(403).send('Unauthorized')
}
const idToken = req.headers.authorization.split('Bearer ')[1]
try {
req.user = await admin.auth().verifyIdToken(idToken)
return next()
} catch (e) {
return res.status(403).send('Unauthorized')
}
}
app.get('/test', authenticate, async (req, res) => res.send(`Hello ${req.user.email}`))
app.get('/healthz', (req, res) => res.send({status: true}))
app.get('/me', (req, res) => db.collection('test')
.doc('QQmFkOhk01ifRNefscTX')
.get()
.then(userInfo => res.send(userInfo.data()))
.catch(error => res.send({error}))
)
exports.api = functions.https.onRequest(app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment