getCurrentUser always null in JS

working with JS…this always returns null for the current user even though the login shows valid. I was expecting a User object with details to be present…am I using this wrong?

Backendless.UserService.isValidLogin()
    .then( () => {
      Backendless.UserService.getCurrentUser(true)
        .then(currentUser => {
          console.log( currentUser )
        })
        .catch(error => {
          console.error( error )
        });
      //popProfile()
      console.log( 'Login is successful' );
    })
    .catch( () => { 
      console.error( 'Error...not successful' );
    });

Your code works well for me.

Could you show how did you make your login?

I should clarify…i am using Google API for the login. The Google auth is successful and returns me to my first app page which needs user data. I’m trying to get the user details of the login, but it just returns null.

Do I need to manually set the current user after logging in with Google API?

Weird…I just ran the same code again and it worked. I’ll keep tinkering with it.

Nope, not working. I’m not sure why the isValidLogin is returning true if there isn’t a current user set yet?

This runs automatically when Google successfully logs me in and I get redirected to the page:

( () => {
  Backendless.UserService.isValidLogin()
  .then( () => {
    console.log( 'Login is successful...getting user' );
    Backendless.UserService.getCurrentUser(true)
      .then(currentUser => {
        console.log( currentUser )
      })
      .catch(error => {
        console.error( error )
      });    
  })
  .catch( () => { 
    console.error( 'Error...not successful' );
  });
})();

if anyone else has this problem it seems like you need to manually set the current user with:

Backendless.UserService.setCurrentUser({ 'user-token': userToken, objectId: userId })

pull the token and id out of the URL.

Once you set the current user it works. Here is the full code:

( () => {
  Backendless.UserService.isValidLogin()
  .then( () => {
    //set current user
    var queryString = window.location.search;
    var urlParams = new URLSearchParams(queryString);
    console.log(urlParams);
    var userToken = urlParams.get('userToken')
    var userId = urlParams.get('userId')
    console.log( userToken, userId )
    Backendless.UserService.setCurrentUser({ 'user-token': userToken, objectId: userId })

    console.log( 'Login is successful...getting user' );

    Backendless.UserService.getCurrentUser(true)
      .then(currentUser => {
        console.log( currentUser )
      })
      .catch(error => {
        console.error( error )
      });    
  })
  .catch( () => { 
    console.error( 'Error...not successful' );
  });
})();

spoke too soon…found that :slight_smile:

Backendless.UserService.isValidLogin()

always returns false…even right after a successful login. In the code above i wasn’t testing the return value.

guessing that this function is only for Backendless native login and not third party API…can someone confirm?

Hello @David_Thompson,

The isValidLogin method checks if there is a valid user-token in the app. It does not require having a full user object. So, after logging in with the Google API, the valid user-token is being set in your app, which is why isValidLogin returns true.

Now, if you need to set the current user manually, you’ve found the correct method for this - setCurrentUser. However, if you want to keep the user logged in even after refreshing or reopening the app, you need to pass true as a second argument to the setCurrentUser method (the argument name is stayLoggedIn). Then, it will remember your user.

Here’s the implementation of all these methods:

You can find all the code here:

Hi @stanislaw.grin

the documentation is not clear on what methods I ‘should’ execute to complete a full login so I’m a little confused. i also want to state that i was wrong above that the isValidLogin was returning true…it never was. Even after a successful login (i can see the userToken and userId in the url) this method STILL returns a false variable.

  1. Do I need to manually run a function that tells the app it was successful?
  2. After the google login page do I need to manually do Step 3 in the documentation and run this?
Backendless.UserService.loginWithOauth2(providerCode, accessToken, guestUser, fieldsMapping, stayLoggedIn): Promise<Backendless.User>;