Post-Login Rule/Action to create a new user in Heroku / Hasura Postgresql doesn't work?

Hello,

Using the guides here:

I have created both a Rule as well as an Action to create a new user in a Postgres database hosted by Heroku everytime a user logs in through my app, which uses Auth0 and is hosted on Heroku.

The Rule and Action were built according to template, yet nothing triggers or happens to my databases when someone logs in through Auth0 on my app, nor when I login via Getting Started on the Auth0 site.
(Note that my login, via LinkedIn, is successful).

My rule:

function (user, context, callback) {
const email = user.email;
const url = “https://bright-mullet-79.hasura.app/v1/graphql”;
const upsertUserQuery = mutation($userId: String!){ insert_users(objects: [{ id: $email }], on_conflict: { constraint: users_pkey, update_columns: [] }) { affected_rows } };
const graphqlReq = { “query”: upsertUserQuery, “variables”: { “email”: email } };

request.post({
headers: {‘content-type’ : ‘application/json’, ‘x-hasura-admin-secret’: configuration.HASURA_ADMIN_SECRET},
url: url,
body: JSON.stringify(graphqlReq)
}, function(error, response, body){
console.log(body);
callback(null, user, context);
});
}

My action:

const axios = require(“axios”);

exports.onExecutePostLogin = async (event) => {

const firstname = event.user.first_name;

const lastname = event.user.last_name;

const email = event.user.email;

const admin_secret = event.secrets.HASURA_SECRET;

const url = “https://bright-mullet-79.hasura.app/v1/graphql”;

const query = `mutation ( $email: String, $firstname: String, $lastname: String) {

    insert_users(objects: { email: $email, firstname: $firstname, lastname: $lastname}, on_conflict: {constraint: users_federated_id_key}) {

        affected_rows

    }

}`;

const variables = {

email: email,

firstname: firstname,

lastname: lastname,

};

const config = {

headers: {

  "content-type": "application/json",

  "x-hasura-admin-secret": admin_secret,

},

};

const data = JSON.stringify({

query: query,

variables: variables,

});

await axios.post(url, data, config);

};

Everything seems to be configured correctly, and setup according to specification… My ‘users’ database was set up with just four fields (email, first_name, last_name, picture), and only email is required.

Any suggestions on what might be the issue here?

Thanks,

Hi @robliou01,

Thanks for reaching out to the Auth0 Community!

To better assist you, could you please use the Real-time Webtask Logs Extension to trace the execution of your Rule/Action and share the error or success response?

While testing, you may find the built-in debugger in Actions helpful to test a sample request and see the response. With Rules, you must use the Real-time Webtask Logs extension.

Looking forward to your response.

Thanks.

“While testing, you may find the built-in debugger in Actions helpful to test a sample request and see the response.”

Hi Ruben, thanks for responding.

With regards to the built-in debugger for Actions; where or how do I find this? I don’t see it anywhere on my panel… is it in the Getting Started menu?

Would it be possible to screenshot how to access this?

Thanks and Best,

To better assist you, could you please use the Real-time Webtask Logs Extension to trace the execution of your Rule/Action and share the error or success response?

Ruben, for testing the Rule, I’ve gone ahead and installed the Real-time Webtask Logs Extension.

As you can see, when I run the rule using Linked-in login, I get the response that my login was successful.

Here is the most recent version of the Rule that I’ve been using:

function (user, context, callback) {
const userId = user.user_Id;
const email = user.email;
const url = “https://bright-mullet-79.hasura.app/v1/graphql”;
const upsertUserQuery = mutation($userId: String!){ insert_users(objects: [{ id: $userId, email: $email }], on_conflict: { constraint: users_pkey, update_columns: [] }) { affected_rows } };
const graphqlReq = { “query”: upsertUserQuery, “variables”: { “userId”: userId, “email”: email } };

request.post({
headers: {‘content-type’ : ‘application/json’, ‘x-hasura-admin-secret’: configuration.HASURA_ADMIN_SECRET},
url: url,
body: JSON.stringify(graphqlReq)
}, function(error, response, body){
console.log(body);
callback(null, user, context);
});
}

(Note that I am using sign-in with LinkedIn).

However, here is the error message that I receive in my Real-time Webtask Log:

" {“errors”:[{“extensions”:{“path”:“$”,“code”:“validation-failed”},“message”:“expecting a value for non-nullable variable: "userId"”}]}"

I’ve gone ahead and added a ‘userId’ and “user_id” variable to my heroku database, as well as modified the rule as above, but the same error persists.

Any idea here what is happening? Here is a screenshot of my Heroku PSQL database I created manually:

I am ultimately expecting my first name, last name, email, and profile picture to be returned.

Thanks,

Hi @robliou01,

Thank you for your responses.

The Actions debugger is located on the left column with a Play Button. Then, clicking on the Run button below tests the Action against a sample request. See below.


Next, after looking closely at your Real-time Webtask Logs, it appears to indicate an error about the value you passed in your request. Specifically, the value passed is null or undefined but expects a non-nullable value.

And this is consistent with the typo in your Rule. It appears that you called user.user_Id when the correct reference should be user.user_id. Could you please fix the typo and see how that works?

Please see the User Object Properties in Rules for the complete reference.

Thank you!

2 Likes

Rueben, this worked for me, thanks!

Any other questions, will be sure to let you know…

Best,

R

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.