Event Handlers Logic not triggering after OAuth 2.0 login/signup is successful

Hi Support Team!

I successfully deployed my OAuth 2.0 services for google and Microsoft. However, whenever I test out my login/sign up flow. It creates a new row in the users table with objectId, and every field except for stripe_customer_id, its always null. This means that my logic for the after login event handler is not being triggered. Does it have to do with the name of the event handler and/or any other functions I need to have in the logic? Wondering if there is any documentation I can read and reference to? I tried to look into your API docs but couldn’t find anything. I even checked my logs and don’t see it being triggered and don’t see any errors.

Hi @Nasser_Othman,

As far as I understand, your issue is specifically related to the event handler — it seems that it’s not registered correctly on your side.

When creating a new handler, the system provides you with a template showing the expected structure. Here’s an example:

/**
* @param {Object} req The request object contains information about the request
* @param {Object} req.context The execution context contains information about the application, current user, and event
* @param {Object} req.user 
*
* @param {Object} res The response object
* @param {Object} res.result Execution Result
* @param {Object} res.error
*
* @returns {Object|Promise.<Object>|void} By returning a value you overwrite the server's result
*/
Backendless.ServerCode.User.afterRegister(async function(req, res) {
  // add your code here
});

To ensure the handler is properly registered and triggered, you need to place your logic inside the designated section in this template.

Please try implementing it this way. If you encounter any issues, we’ll be happy to help you resolve them!

Regards,
Sergey

Hi @Sergey_Androsov, thank you for your quick response. Can you confirm if I have my afterRegister.js in the correct location?

Is the red box the correct location or the green box the correct location?

I initially had it in the greenbox because I had another handler that was working. However, I created the one in the red box to see if the location was incorrect but still wasn’t triggering.

this is my afterRegister.js logic:

// events/users/register/afterRegister.js

// goes from events/users/register → ../../../services/CreateStripeCustomerService.js
const CreateStripeCustomerService = require('../../../services/CreateStripeCustomerService');
const stripeService = new CreateStripeCustomerService();

module.exports = async function(req, res) {
  const user = req.user;
  const { objectId, stripe_customer_id } = user;

  console.log(' afterRegister triggered for user:', objectId);

  if (stripe_customer_id) {
    console.log('stripe_customer_id already set:', stripe_customer_id);
    res.result = user;
    return;
  }

  try {
    // Pass an object with body.userId to match the service signature
    const response = await stripeService.createStripeCustomer({ body: { userId: objectId } });
    console.log('CreateStripeCustomerService response:', response);

    const newId = response.body?.stripe_customer_id;
    if (response.status === 200 && newId) {
      await Backendless.Data.of('Users').save({
        objectId,
        stripe_customer_id: newId
      });
      console.log('stripe_customer_id saved:', newId);
      user.stripe_customer_id = newId;
    }
    else {
      console.error('Invalid service response, no stripe_customer_id:', response);
    }

    res.result = user;
  }
  catch (err) {
    console.error('Error in afterRegister:', err);
    res.error = err.message || 'Error creating Stripe customer';
  }
};

If you believe I am missing something, please let me know! :smiley:

Hello @Nasser_Othman

Correct location for handler is (screenshot).

You can use the EventHandlers section of the Backendless Console. Where you will create the required handlers and then just add the required logic.

Regards,
Inna