Created
April 28, 2019 00:29
-
-
Save andresmijares/c78dd0907961ad3810a1728853addf54 to your computer and use it in GitHub Desktop.
firebasecustomTokenClient
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
import firebase from 'firebase/app' | |
import axios from 'axios' | |
require('firebase/auth') | |
const YOUR_BACKEND_PATH = 'https://mylambdaapigatewayendpoint.com/auth' | |
const config = { | |
apiKey: process.env['FIREBASE_API_KEY'], | |
authDomain: process.env['FIREBASE_AUTH_DOMAIN'], | |
databaseURL: process.env['FIREBASE_DATABASE_URL'], | |
projectId: process.env['FIREBASE_PROJECT'], | |
storageBucket: process.env['FIREBASE_STORAGE'], | |
messagingSenderId: process.env['FIREBASE_MESSAGE_ID'], | |
} | |
firebase.initializeApp(config) | |
const withAuthHeader = () => { | |
const token = localStorage.getItem('id_token') // Whereever you are storaging your auth0 token | |
const instance = axios.create({ | |
headers: { | |
'Authorization': `Bearer ${token}`, | |
'Content-Type': 'application/json', | |
}, | |
}) | |
return instance | |
} | |
const setCustomToken = () => { | |
const axios = withAuthHeader() | |
const { data } = await axios.post(YOUR_BACKEND_PATH) | |
const firebaseToken = await firebase.auth().signInWithCustomToken(data.token) | |
return firebaseToken | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment