Skip to content

Instantly share code, notes, and snippets.

@nvalerkos
Last active September 3, 2023 06:54
Show Gist options
  • Save nvalerkos/aebdbe4883a1911a4dfa07fc67798a40 to your computer and use it in GitHub Desktop.
Save nvalerkos/aebdbe4883a1911a4dfa07fc67798a40 to your computer and use it in GitHub Desktop.
Sync Users Between Auth0 and Hasura - Auth0 Custom Action
/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
var request = require('request');
const userId = event.user.user_id;
const userName = event.user.username;
const email = event.user.email;
const admin_secret = event.secrets.HASURA_ADMIN_SECRET;
const url = "https://your-domain-here.hasura.app/v1/graphql";
const query = `mutation($userId: String!, $userName: String, $email: String) {
insert_users(objects: [{
id: $userId, name: $userName, email: $email, last_seen: "now()"
}], on_conflict: {constraint: users_pkey, update_columns: [last_seen, name, email]}
) {
affected_rows
}
}`;
const variables = { userId, userName, email };
request.post({
url: url,
headers: {'content-type' : 'application/json', 'x-hasura-admin-secret': admin_secret},
body: JSON.stringify({
query: query,
variables: variables
})
}, function(error, response, body){
console.log(body);
});
};
/**
* Handler that will be invoked when this action is resuming after an external redirect. If your
* onExecutePostLogin function does not perform a redirect, this function can be safely ignored.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
// exports.onContinuePostLogin = async (event, api) => {
// };
@nvalerkos
Copy link
Author

nvalerkos commented Sep 3, 2023

Create a secret in Auth0 on the left (key icon):
Enter for Key HASURA_ADMIN_SECRET and put your Hasura Admin Secret got from the Hasura console on the value.

Create a dependency:
On the left (package Icon) where it reads name add:
request

Do not forget to replace https://your-domain-here.hasura.app/v1/graphql with your graphql url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment