Backendless Support
 

User login API – loginning the registered users to a Backendless app

The registering app users using Backendless API article talks about how to create user accounts. If an application uses the user registration API, odds are you will need to use the Login API as well. 

The API is rather trivial – it requires two parameters: a value which uniquely identifies the user and his password.

The code below demonstrates the API in action. It uses the same user which was registered in the code from the user registration API article:

JAVA

package com.mbaas.samples.login;
import com.backendless.Backendless;
import com.backendless.BackendlessUser;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.exceptions.BackendlessFault;
public class Main
{
 public static String APP_ID = "YOUR-APP-ID";
 public static String SECRET_KEY = "YOUR-SECRET-KEY";
 public static String VERSION = "v1";
 public static void main( String[] args )
 {
 Backendless.initApp( APP_ID, SECRET_KEY, VERSION );
 loginUser();
 loginUserAsync();
 }
 private static void loginUser()
 {
 BackendlessUser loggedInUser = Backendless.UserService.login( "spidey@backendless.com", "greeng0blin" );
 System.out.println( "User has been logged in - " + loggedInUser.getObjectId() );
 }
 private static void loginUserAsync()
 {
 AsyncCallback<BackendlessUser> callback = new AsyncCallback<BackendlessUser>()
 {
 @Override
 public void handleResponse( BackendlessUser loggedInUser )
 {
 System.out.println( "User has been logged in - " + loggedInUser.getObjectId() );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
 };
 Backendless.UserService.login( "spidey@backendless.com", "greeng0blin" , callback );
 }
}
 

Once a user logs in, it is shown in Backendless console. For example, the screenshot below indicates that the user with ID spidey@backendless.com has logged in and you can see when. However, the other user has never logged in – the value for the lastLogin column for that user is empty. 

Review related topics:

Is article helpful?