user.afterLogin event handler is not being called

Backendless Version (3.x / 6.x, Online / Managed / Pro )

6.x

Application ID

532CF1B7-693E-48CA-B1B1-F16FCB709609

Expected Behavior

I need to do some initialization logic on a user’s first login, however the handler is not being invoked at all.

Interestingly, the “beforeLogin” event is being called, but the “afterLogin” event appears to be ignored.

After successfully deploying, and logging in, I expected to see the print “abc” in the Backendless logger.

Actual Behavior

This is what I see after logging in:

“beforeLogin” handler is being invoked, but nothing about “afterLogin” nor the print “abc”.

Thank you

Hello @Nathan_Cousins!

The event “afterLogin” is ignored because you are returning some data from “beforeLogin”.

Regards,
Alexander

Hi @Alexander_Pavelko, thanks for the reply!

I believe that beforeLogin hook is being handled by a third party market plugin, which I require for my app though I don’t believe I have control over.

Basically what I’m trying to do is perform some initialization for a user when they log in for the first time, which is to automatically create some default relation objects for that user.

Is there any way for me to hook into this event even if it’s just for its side-effects? And if not, what would be the alternative way to perform some initializations?

I currently have it so on the client, as soon as they log in it calls the initialization service which will determine if it’s a first time login and if it needs to do what it needs to do. However, I’m trying to move away from that methodology as it’s putting trust in the client that it will invoke that service once they log in.

Thanks again

I hope I understood you correctly, you can use beforeLogin/afterLogin to solve your issues, but keep in mind that if you return something from “beforeLogin”, “afterLogin” will not handle the event.

Regards,
Alexander

I suppose the issue I’m having here is that it appears one of my installed marketplace services (not my own) is using beforeLogin and returning data for it, therefore based on what you’re saying I won’t be able to use afterLogin for my own purposes.

In that case I was hoping that there would be another way to watch for these events, not to handle the actual login process but just to perform some side-effect initialization.

Thanks

You can also disable the beforeLogin handler in EventHandlers and add your own with your own logic.

For example:

Backendless.ServerCode.User.beforeLogin(async function (req) {
  const user = await Backendless.Users.login(req.login, req.password);

  if (!user.phoneNumber) {
    // if the user doesn't have phoneNumber just return the logged-in user
    return user
  }

  //otherwise send SMS with secret code and return to the client the transaction id
  const transactionId = await Backendless.CustomServices.invoke('Twilio', 'sendVerificationCode', user.phoneNumber);

  if (transactionId) {
    return { transactionId }
  }
});

Regards,
Alexander

An interesting way to get around the issue, thank you! Much appreciate the effort. I will give it a shot and if I have any other issues or questions I’ll bump this thread.

Thank you for contacting us.
I look forward to helping you again!

Regards,
Alexander