mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-16 01:44:03 +03:00
42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
const functions = require('firebase-functions');
|
|
const admin = require('firebase-admin');
|
|
admin.initializeApp(functions.config().firebase);
|
|
|
|
// On sign up.
|
|
exports.processSignUp = functions.auth.user().onCreate(user => {
|
|
console.log(user);
|
|
// Check if user meets role criteria:
|
|
// Your custom logic here: to decide what roles and other `x-hasura-*` should the user get
|
|
let customClaims;
|
|
if (user.email && user.email.indexOf('@hasura.io') != -1) {
|
|
customClaims = {
|
|
'https://hasura.io/jwt/claims': {
|
|
'x-hasura-default-role': 'admin',
|
|
'x-hasura-allowed-roles': ['user', 'admin'],
|
|
'x-hasura-user-id': user.uid
|
|
}
|
|
};
|
|
}
|
|
else {
|
|
customClaims = {
|
|
'https://hasura.io/jwt/claims': {
|
|
'x-hasura-default-role': 'user',
|
|
'x-hasura-allowed-roles': ['user'],
|
|
'x-hasura-user-id': user.uid
|
|
}
|
|
};
|
|
}
|
|
// Set custom user claims on this newly created user.
|
|
return admin.auth().setCustomUserClaims(user.uid, customClaims)
|
|
.then(() => {
|
|
// Update real-time database to notify client to force refresh.
|
|
const metadataRef = admin.database().ref("metadata/" + user.uid);
|
|
// Set the refresh time to the current UTC timestamp.
|
|
// This will be captured on the client to force a token refresh.
|
|
return metadataRef.set({refreshTime: new Date().getTime()});
|
|
})
|
|
.catch(error => {
|
|
console.log(error);
|
|
});
|
|
});
|