Skip to content

Instantly share code, notes, and snippets.

@BadPirate
Last active April 12, 2020 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BadPirate/4366e8bf3b72f7fc2f43be89a4a20803 to your computer and use it in GitHub Desktop.
Save BadPirate/4366e8bf3b72f7fc2f43be89a4a20803 to your computer and use it in GitHub Desktop.
Cloud function to allow for realtime update of basic claims
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const cors = require('cors')({ origin: true })
admin.initializeApp(functions.config().firebase)
const updateClaims = (uid) => admin.auth().setCustomUserClaims(uid, {
'https://hasura.io/jwt/claims': {
'x-hasura-default-role': 'user',
'x-hasura-allowed-roles': ['user'],
'x-hasura-user-id': uid,
},
})
exports.processSignUp = functions.auth.user().onCreate((user) => updateClaims(user.uid))
exports.refreshToken = functions.https.onRequest((req, res) => {
console.log('TOKEN REFRESH', req.query.uid)
cors(req, res, () => {
updateClaims(req.query.uid).then(() => {
res.status(200).send('success')
}).catch((error) => {
console.error('REFRESH ERROR', error)
res.status(400).send(error)
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment