Created
April 28, 2019 00:11
-
-
Save andresmijares/31b3fc192bd6620b27877f11a33b43e7 to your computer and use it in GitHub Desktop.
firebaseCustomToken.js
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
'use strict' | |
const admin = require('firebase-admin') | |
const jwtDecode = require('jwt-decode') | |
const serviceAccount = require('path-to-your-firebase-key') | |
const getCustomToken = async sub => { | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount) | |
}) | |
const firebaseToken = await admin.auth().createCustomToken(sub) | |
return firebaseToken | |
} | |
const setCustomTokenFirebase = async (event) => { | |
const { headers } = event | |
const { Authorization } = headers | |
const jtwToken = Authorization.split('Bearer')[1] | |
const jwtDecoded = jwtDecode(jtwToken) | |
const token = await getCustomToken(jwtDecoded['sub']) | |
return { token } | |
} | |
module.exports.handler = async (event) => { | |
const { token } = await setCustomTokenFirebase(event) | |
return { token } | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment