Last active
April 12, 2020 21:13
-
-
Save BadPirate/4366e8bf3b72f7fc2f43be89a4a20803 to your computer and use it in GitHub Desktop.
Cloud function to allow for realtime update of basic claims
This file contains hidden or 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
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