Auth0 Home Blog Docs

Rules error with Webtask "Are you calling callback"

rules

#1

Hi,
I created a rule following a Firebase Rule and an tutorial provided by Auth0 on Rules.
With this rule I want to be able to pass user data to my backend, I am able to pass the data, but the webtask connection is not closing, and I am getting an error “Are you calling callback”, and i don’t know what is wrong… below is the rules code

function (user, context, callback) {
  var baseURL = 'http://blabla.ngrok.io/api';
  var usr_id = new Buffer(user.user_id).toString('base64');
  var newUser = {
    'identity': {
      'user_email': user.email,
      'user_id': user.user_id
    }
  };
  var putURL = baseURL + "/users/" + usr_id + ".json";
  request.post({
    "url": putURL,
    "json": newUser
  },
  function(err, response, body) {
    if(err) return callback(err);
    return callback(null, user, context);
  });
}

Is there any better way for above code ?

PS.
How would it be best to add above this function another function that if the user is already signed up, just pass and do not run this code ?

Looking forward for help from someone,
Cheers


#2

This is the error:
Script execution did not complete within 20 seconds. Are you calling the callback function?"


#3

As far as checking if the user is signed up, you should make a GET request to your backend where you select users with the user_email or user_id, if you get an empty json then you run your request.post{} call, if you get a non-empty json you skip the request.post{}. I use my own Nodejs Api to handle accounts so I am not sure about the exact syntax you will need here to make the GET request.

Are you sure the data is getting added to your backend, when you test this rule does the user_email and user_id get added correctly? If it does not get added, that could possibly be your IP address is not allowing incoming requests from outside ports.

For the sake of testing something, try adding a return callback(null, user, context); before your last curly brace:

function (user, context, callback) {
   var baseURL = 'http://blabla.ngrok.io/api';
   var usr_id = new Buffer(user.user_id).toString('base64');
   var newUser = {
     'identity': {
       'user_email': user.email,
       'user_id': user.user_id
     }
   };
   var putURL = baseURL + "/users/" + usr_id + ".json";
   request.post({
     "url": putURL,
     "json": newUser
   },
   function(err, response, body) {
     if(err) return callback(err);
     return callback(null, user, context);
   });
return callback(null, user, context);
 }

#4

Hi @nje2n Thank you for you reply.

Based on your proposal I created something but I can’t get it to work right :frowning:
As per the code above, I was getting so tired that I was just console.log on my backend :frowning: that was the issue.

I did the following code so far, I am trying to be able to first get route and if the user exist, I want to continue and not try to add the user to db.
I will be looking forward any help for the right code below:

function (user, context, callback) {
  var baseURL = 'http://tttt.ngrok.io/api';
  var usr_id = new Buffer(user.user_id).toString('base64');
  var newUser = {
    'identity': {
      'user_email': user.email,
      'user_id': user.user_id
    }
  };
  var test = 'testtesttest';
  var getURL = baseURL + "/users/" + user.user_id;
  request.get({
    "url": getURL,
  },
  function(err, response, body) {
    if(response.body === 'null') {
      var putURL = baseURL + "/users/" + usr_id;
      request.post({
        "url": putURL,
        "json": newUser
      });
      console.log("user created" + usr_id);
      console.log(response.body);
    }
    if(err) return callback(err);
    console.log("user exist");
    return callback(null, user, context);
  });
}

my simple backend code:

router.post('/users/*', (req, res) => {
  User.create(new User({
    email: req.body.identity.user_email,
    authUserId: req.body.identity.user_id
  }))
  res.json(console.log("User Created"))
})
router.get('/users/:id', (req, res, next) => {
  User.findOne({authUserId: req.params.id}, (err, userr) => {
    if(err) {
      return next(err);
    } else if (userr) {
      res.json(userr.email);
    } else {
      res.json(null)
    }
  });
});

#5