mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
0ff26100bf
GitOrigin-RevId: 1f2a1d21bfb9b2908d56305fa2dfb69270deafdf |
||
---|---|---|
.. | ||
index.js | ||
README.md |
Sample Firebase Cloud Function Auth Webhook for Hasura GraphQL engine
Further reading: Firebase SDK for Cloud Functions
Install and deploy
- Create a Firebase Project using the Firebase Console.
- Clone or download this repo and go to
cd firebase-cloud-functions
directory. - You must have the Firebase CLI installed. If you don't have it install it with
npm install -g firebase-tools
and then configure it withfirebase login
. - Configure the CLI locally by using
firebase init
and select your project in the list. - Follow Add the Firebase Admin SDK to Your Server and save it as config.js
- Copy index.js and config.js to functions folder by:
cp index.js config.js functions/
- Install Cloud Functions dependencies locally by running:
cd functions; npm install
- Deploy to Firebase Cloud Functions by
firebase deploy
Once deployed endpoint like this will be created and displayed:
https://us-central1-xxxxx-auth.cloudfunctions.net/hasuraWebhook
Add webhook endpoint to Hasura GraphQL
Set --auth-hook
or HASURA_GRAPHQL_AUTH_HOOK
to the endpoint obtained above.
GraphQL engine server flags reference for details.
Create table and set permission
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)
How to query GraphQL Engine from frontend JS code (React, VueJS, Angular etc...)
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
}
}