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.

- (void)loginWithIdentity:(NSString * _Nonnull)identity password:(NSString * _Nonnull)password responseHandler:^(BackendlessUser * _Nonnull)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func login(identity: String, password: String, responseHandler: ((BackendlessUser) -> Void)!, errorHandler: ((Fault) -> Void)!)

where:

Argument                Description
identity a value for the property marked as identity.
password user's password

Return value

The login API returns a BackendlessUser object representing the logged in user. The object has the values for all the properties stored in Backendless database including those for which the "auto-load" option is enabled.

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
[Backendless.shared.userService loginWithIdentity:@"james.bond@mi6.co.uk" password:@"iAmWatchingU" responseHandler:^(BackendlessUser *loggedInUser) {
    NSLog(@"User has been logged in");
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Backendless.shared.userService.login(identity: "james.bond@mi6.co.uk", password: "iAmWatchingU", responseHandler: { loggedInUser in
    print("User has been logged in")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})

Remembering User Login

It is a frequent requirement for the applications to store user's login so next time application starts, the user is not required to login. The user interface for this scenario is typically a "Remember me" checkbox.

There is a stayLoggedInproperty in user service that must be set after application has been initialized with the initApp call before the login API is used:

Example

Backendless.shared.userService.stayLoggedIn = YES;
Backendless.shared.userService.stayLoggedIn = true

Additionally this property indicates if the current state of the system is set to allow persistent logins:

BOOL isStayLoggedIn = Backendless.shared.userService.stayLoggedIn;
let isStayLoggedIn = Backendless.shared.userService.stayLoggedIn

If the persistent logins are enabled, the property returns Yes/true, otherwise No/false.

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:

-(void)isValidUserTokenWithResponseHandler:^(BOOL)responseHandler errorHandler:^(Fault * _Nonnull)errorHandler;
func isValidUserToken(responseHandler: ((Bool) -> Void)!, errorHandler: ((Fault) -> Void)!)

Return value

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

Example

[Backendless.shared.userService isValidUserTokenWithResponseHandler:^(BOOL isValid) {
    NSLog(@"User token is valid: %i", isValid);
} errorHandler:^(Fault *fault) {
    NSLog(@"Error: %@", fault.message);
}];
Backendless.shared.userService.isValidUserToken(responseHandler: { isValid in
    print("User token is valid: \(isValid)")
}, errorHandler: { fault in
    print("Error: \(fault.message ?? "")")
})