community/boilerplates/auth-webhooks/firebase-cloud-functions/README.md
Further reading: Firebase SDK for Cloud Functions
cd firebase-cloud-functions directory.npm install -g firebase-tools and then configure it with firebase login.firebase init and select your project in the list.cp index.js config.js functions/cd functions; npm installfirebase deployOnce deployed endpoint like this will be created and displayed:
https://us-central1-xxxxx-auth.cloudfunctions.net/hasuraWebhook
Set --auth-hook or HASURA_GRAPHQL_AUTH_HOOK to the endpoint obtained above.
GraphQL engine server flags reference for details.
Follow Common roles and auth examples on Hasura doc for details of how to setup permission to a table.
Make sure to change id column of user table to TXT type as uid sent from webhook is firebase User UID format (e.g. 0LnvZc7405TjRTbjURhZYYVXPI52)
postAxios.js
import axiosBase from 'axios'
import * as firebase from 'firebase'
const getIdToken = async () => {
return new Promise((resolve, reject) => {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
resolve(firebase.auth().currentUser.getIdToken())
} else {
reject(Error('user logged out'))
}
})
})
}
export const postAxios = async (queryString) => {
const idToken = await getIdToken()
const axios = axiosBase.create({
baseURL: 'https://YOURHASURADOMAIN/v1/graphql',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + idToken
},
responseType: 'json',
method: 'post'
})
return await axios({
data: {
query: queryString
}
}).catch(({response: r}) => console.log(r))
}
userService.js
import { postAxios } from './postAxios'
export default {
async getUsers () {
const queryString = `
query {
user
{
id
name
}
}
`
const result = await postAxios(queryString)
return result.data.data.user
}
}