Adding reference docs + paramterized some environment variables + new firebase environments. + firebase ref docs.

This commit is contained in:
Patrick Fic
2020-05-01 17:06:52 -07:00
parent 4699b10093
commit c404ff377c
7 changed files with 153 additions and 63 deletions

View File

@@ -2,33 +2,32 @@ const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
//TODO Move this to an environment parameter.
const GRAPHQL_ENDPOINT = functions.config().auth.graphql_endpoint;
const HASURA_SECRET_ADMIN_KEY = functions.config().auth.hasura_secret_admin_key;
const UPSERT_USER = `
mutation upsert_user($authEmail: String!, $authToken: String!) {
insert_users(
objects: [
{
email:$authEmail,
authid:$authToken
}
],
on_conflict: {
constraint: users_pkey,
update_columns: [authid]
}
) {
returning {
authid
}
}
}
`;
// //TODO Move this to an environment parameter.
// const GRAPHQL_ENDPOINT = functions.config().auth.graphql_endpoint;
// const HASURA_SECRET_ADMIN_KEY = functions.config().auth.hasura_secret_admin_key;
// const UPSERT_USER = `
// mutation upsert_user($authEmail: String!, $authToken: String!) {
// insert_users(
// objects: [
// {
// email:$authEmail,
// authid:$authToken
// }
// ],
// on_conflict: {
// constraint: users_pkey,
// update_columns: [authid]
// }
// ) {
// returning {
// authid
// }
// }
// }
// `;
// On sign up.
exports.processSignUp = functions.auth.user().onCreate(user => {
console.log(user);
exports.processSignUp = functions.auth.user().onCreate((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;
@@ -37,16 +36,16 @@ exports.processSignUp = functions.auth.user().onCreate(user => {
"https://hasura.io/jwt/claims": {
"x-hasura-default-role": "admin",
"x-hasura-allowed-roles": ["user", "admin"],
"x-hasura-user-id": user.uid
}
"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
}
"x-hasura-user-id": user.uid,
},
};
}
@@ -65,17 +64,15 @@ exports.processSignUp = functions.auth.user().onCreate(user => {
// });
// 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);
});
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);
// });
});