Created
October 19, 2018 21:15
-
-
Save feulf/74c6a8989bc072b8ca445fa0f60cdd72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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