Skip to content

Login

Registered users can login to establish their identity with the application using the API below. The login operation requires two properties: one marked as user identity and the second is password. Backendless automatically assigns the "AuthenticatedUser" role to all successfully logged in users. The role can be used to differentiate access to various resources (persistent objects, messaging channels, files) between authenticated users and guests.

Non-Blocking API

Backendless.UserService.login( login, password, stayLoggedIn )
 .then( function( loggedInUser ) {
   })
 .catch( function( error ) {
   });

where:

Argument                Description
login a value for the property marked as identity.
password user's password
stayLoggedIn a boolean value requesting user login information to be saved so it can be reused when the application restarts (or page is reloaded).
loggedInUser an instance of Backendless.User representing the logged in user.
error error object for an error thrown either out of the then block or from the server.

Blocking API

var loggedInUser = Backendless.UserService.loginSync( login, password, stayLoggedIn );

where:

Argument                Description
login a value for the property marked as identity.
password user's password
stayLoggedIn a boolean value requesting user login information to be saved so it can be reused when the application restarts (or page is reloaded).
loggedInUser an instance of Backendless.User representing the logged in user.

If the stayLoggedIn argument is set to true, the following APIs can be used to retrieved persisted information about the user:

// get objectId of the logged-in user:
var userObjectId = Backendless.LocalCache.get("current-user-id")

// get user-token of the logged-in user:
var userToken = Backendless.LocalCache.get("user-token")

// get current user object:
var userObject = Backendless.UserService.getCurrentUser()

Errors

The following errors may occur during the Login API call. See the Error Handling section for details on how to retrieve the error code when the server returns an error.

Error Code
Description
2002
Version is disabled or provided wrong application info (application id or secret key)
3000
Login has been disabled for the user account.
3001
Missing login settings, possibly invalid application id or version.
3002
User cannot login because Multiple Logins disabled and there is a logged in user for the account.
3003
Invalid login or password.
3006
Either login or password is an empty string value.
3034
User logins are disabled for the version of the application.
3036
Account locked out due to too many failed logins.
3038
One of the required parameters (application id, version, login or password) is null
3044
Multiple login limit for the same user account has been reached.
8000
Property value exceeds the length limit

Non-blocking Method Sample

// do not forget to call Backendless.initApp( appId, apiKey ); to initialize your app

function userLoggedIn( user )
{
  console.log( "user has logged in" );
}

function gotError( err ) // see more on error handling
{
  console.log( "error message - " + err.message );
  console.log( "error code - " + err.statusCode );
}

Backendless.UserService.login( login, password, true )
 .then( userLoggedIn )
 .catch( gotError );

Blocking Method Sample

// do not forget to call Backendless.initApp( appId, apiKey ) to initialize your app

var user;

try
{
  user = Backendless.UserService.loginSync( username, password );
}
catch( err ) // see more on error handling
{
  console.log( "error message - " + err.message );
  console.log( "error code - " + err.statusCode );
}

Validating User Login

The login operation provides a way to persist the user-token on the client side so it can be used when the application is restarted. This helps in streamlining the user experience since the user of the application does not need to login again. However, when the application restarts, it needs to check if the underlying user token, and hence the user session are still valid. This can be accomplished with the API below:

Methods

Non-Blocking API

function success( result )
{ 
  console.log( "Is login valid?: " + result );
} 

function error( err ) 
{ 
  console.log( err.message );
  console.log( err.statusCode );
} 

Backendless.UserService.isValidLogin()
 .then( success )
 .catch( error );

Blocking API

var userTokenIsValid = Backendless.UserService.isValidLoginSync(); 
console.log( "Is login valid?: " +  userTokenIsValid );

Return value

The server returns a boolean value of true if token is valid, false otherwise.