Support Topics Documentation Slack YouTube Blog

Registered user automatically logged in?


(Jeroen Dierckx) #1

Hi,
I am using the javascript API of Backendless v3 to create an admin website. One of the functionalities is creating new users (only admins can create new users, no other user registration is enabled).
It seems that after calling Backendless.UserService.register to create a new user, that user is logged on automatically afterwards. Is that behaviour expected or would it be an error in my code (will elaborate if that is the case)?
Kind regards,
Jeroen


(Sergey Chupov) #2

Hi Jeroen,

This is impossible, the register call only creates a row in a database. To login a user the server needs to issue a user-token, which it definitely does not do on a register call.


(Jeroen Dierckx) #3

OK thanks for the answer, than I will search for a bug on my end. For some reason, the registered client’s ID is stored in the local cache (“current-user-id”).

Kind regards,
Jeroen


(Jeroen Dierckx) #4

Hi,

I checked my code and something strange does seem to be happening on the backendless side.

I register the new user using the following pseudo-code:

var newUser = new Backendless.User();
<fill in properties of newUser>
Backendless.UserService.register( newUser, new Backendless.Async( userRegistered, gotError ) );

the userRegistered callback is correctly called, and when I call the following code in that callback:

var user = Backendless.UserService.getCurrentUser();
var str = JSON.stringify(user, null, 4);
console.log( "Logged on user: " + str );

it shows me the information of the newly registered user.

Also strange: when I call the following code afterwards:

Backendless.UserService.getUserRoles(new Backendless.Async(rolesRetreived, rolesError) );

the roles received are the ones from the actual logged on user, not the newly registered user.

Is that behaviour expected? Does Backendless.UserService.getCurrentUser() not return the currently logged on user?

Kind regards,
Jeroen


(Sergey Chupov) #5

Hi Jeroen,
Just to clarify: you’re registering a new user while you currently have a logged in user, right? So in your case, the CurrentUser is not supposed to be empty neither before the register, nor after?


(Jeroen Dierckx) #6

Yes. Only certain operators are allowed to create new users, so one of those is logged on. Before registering the new user, CurrentUser contains that logged on operator. After registering the new user, CurrentUser contains the newly registered user, not the logged on operator.


(Sergey Chupov) #7

I understand. I tried to reproduce your use case with the following code - it does everything you mentioned:

Backendless.UserService.login('bob@email.com','qwe')
  .then(bobUser => console.log('Logged in user: ' + bobUser.email))
  .then(() => Backendless.UserService.getCurrentUser())
  .then(currentUser => console.log('Current user: ' + currentUser.email))
  .then(() => Backendless.UserService.register(new Backendless.User({email: 'alice@email.com', password: 'qwe'})))
  .then(aliceUser => console.log('Registered user: ' + aliceUser.email))
  .then(() => Backendless.UserService.getCurrentUser())
  .then(currentUser => console.log('Current user: ' + currentUser.email))
  .catch(console.log)

But I failed to run into the same problem, the current user still remains the same judging from the output:

* "Logged in user: bob@email.com"

* "Current user: bob@email.com"

* "Registered user: alice@email.com"

* "Current user: bob@email.com"

Can you try running the same code on your app to check if it would reproduce your issue? If it doesn’t, please prepare a similar script which results in the issue so that we could check it.


(Jeroen Dierckx) #8

Thanks for the swift reply.

I’ll try to create a minimal example. Does try and catch work for the v3 javascript? Note that we did not upgrade to version 4 or 5 yet.


(Sergey Chupov) #9

I’m not sure about version 3, probably not. Also I checked only with current version.


(Jeroen Dierckx) #10

Well, upgrading all our clients to the new version of the cloud is not really an option for us right now…


(Sergey Chupov) #11

If you prepare the sample to reproduce the problem we still may be able to help.


(Jeroen Dierckx) #12

Hi,

I finally got around thoroughly testing this and found the issue and a workaround.

In version 3 of the SDK, the .then() syntax does not work, so I modified your test code to the following, using synchronous API to test first (the password is different on our system of course):

var loggedOnUser = Backendless.UserService.login('JeDiAdmin','qwe');
console.log('Logged in user: ' + loggedOnUser.username + '(email: ' + loggedOnUser.email + ')');
var currentUser1 = Backendless.UserService.getCurrentUser();
console.log('Current user: ' + currentUser1.username + '(email: ' + currentUser1.email + ')');
var newUser = new Backendless.User();
newUser.username = 'test-created-user@bioracermotion.com'; newUser.password = 'qwe';
var registeredUser = Backendless.UserService.register(newUser);
console.log('Registered user: ' + registeredUser.username + '(email: ' + registeredUser.email + ')');
var currentUser2 = Backendless.UserService.getCurrentUser();
console.log('Current user: ' + currentUser2.username + '(email: ' + currentUser2.email + ')');`

and this works as expected:

Logged in user: JeDiAdmin(email: jeroen.dierckx@bioracermotion.com)
Current user: JeDiAdmin(email: jeroen.dierckx@bioracermotion.com)
Registered user: test-created-user@bioracermotion.com(email: null)
Current user: JeDiAdmin(email: jeroen.dierckx@bioracermotion.com)

But when I use the asynchronous API for the register call:

var loggedOnUser = Backendless.UserService.login('JeDiAdmin','qwe');
console.log('Logged in user: ' + loggedOnUser.username + '(email: ' + loggedOnUser.email + ')');
var currentUser1 = Backendless.UserService.getCurrentUser();
console.log('Current user: ' + currentUser1.username + '(email: ' + currentUser1.email + ')');
var newUser = new Backendless.User();
newUser.username = 'test-created-user@bioracermotion.com'; newUser.password = 'qwe';
Backendless.UserService.register(newUser, new Backendless.Async(function(registeredUser) {
    console.log('Registered user: ' + registeredUser.username + '(email: ' + registeredUser.email + ')');
    var currentUser2 = Backendless.UserService.getCurrentUser();
    console.log('Current user: ' + currentUser2.username + '(email: ' + currentUser2.email + ')');
}, function(err) {
    console.log('Error: ' + err.message);
}));

the described error occurs:

Logged in user: JeDiAdmin(email: jeroen.dierckx@bioracermotion.com)
Current user: JeDiAdmin(email: jeroen.dierckx@bioracermotion.com)
Registered user: test-created-user@bioracermotion.com(email: null)
Current user: test-created-user@bioracermotion.com(email: null)

So the bug seems to happen inside the backendless v3 asynchonous javascript API. I worked around this by using the synchronous version of the registerUser method and everything works correctly now.

Kind regards,
Jeroen


(Sergey Chupov) #13

Nice investigation, thank you!
Considering that the issue does not reproduce on version 4 and you’ve found a workaround, I’ll mark your answer as a solution, because we only fix critical bugs in version 3.