loginManager

Use the loginManager to integrate user login-related functionality into your own apps.

Overview

The Bringg Driver SDK for iOS loginManager allows you to integrate Bringg functionality into your own iOS apps using Bringg user login-related events and methods, including logging in, testing current user status, getting current user information, requesting verification codes, recovering passwords, and logging out. The Bringg Driver SDK for iOS provides the internal functionality and you can customize your apps using that functionality. You can also register your objects for login-related event notifications and customize your app for these events.

The loginManager conforms to LoginManagerProtocol which defines its API, including the methods that the you can use for Bringg login functionality.

👍

Objective-C

You can adapt the loginManager into Objective-C.

The following is the LoginManagerProtocol:

@objc public protocol LoginManagerProtocol: NSObjectProtocol {
    var currentUser: User? { get }
    func addDelegate(_ delegate: UserEventsDelegate)
    func removeDelegate(_ delegate: UserEventsDelegate)
    
    func login(withEmail email: String, password: String, merchantID: NSNumber?, completion: @escaping ([Merchant]?, Error?) -> Void)
    func requestVerificationCode(forPhone phone: String, completion: @escaping (Error?) -> Void)
    func login(withVerificationCode code: String, phone: String, merchantID: NSNumber?, completion: @escaping ([Merchant]?, Error?) -> Void)
    func login(withQRCode code: String, completion: @escaping (Error?) -> Void)
    func logout(completion: @escaping (Error?) -> Void)
    func recoverPassword(forEmail email: String, completion: @escaping (Error?) -> Void)
}

UserEventsDelegate

The Bringg Driver SDK for iOS uses the multiple delegation design pattern allowing you to register any object for login and logout events. To register for user events, the object must conform to the UserEventsDelegate protocol.

UserEventsDelegate is:

  • The protocol that defines the login and logout event methods a delegate must implement. A delegate might be any object in the system that requires notification on login or logout.
  • The protocol to which a class must conform for notifications of login or logout events. Objects of that class must be added as delegates of loginManager.

The following is the UserEventsDelegate protocol:

@objc public protocol UserEventsDelegate: class {
    func userDidLogin()
    func userDidLogout()
}

📘

UserEventsDelegate Is Optional

If your objects do not require user login event notifications, your objects do not need to be delegates and you do not need to use the UserEventsDelegate methods.

Feature Summary

loginManager Property

Functionality

currentUser

Optional

If the current user is logged-in, this property returns a Bringg User data type. If the current user is logged-out, the property returns nil.

Note: Bringg data types are immutable.



loginManager Method

Functionality

addDelegate(_:)

Registers an object as a UserEventsDelegate. In most cases, this method is called on an object's init or load.

removeDelegate(_:)

Unregisters an object as a UserEventsDelegate.

login(email:password: merchantId:completion:)

Logs the user in using an email and a password.

recoverPassword(email: completion:)

Recovers a password by email. If the email address is valid, sends a recovery email to that email address.

requestVerificationCode( phone:completion:)

Requests a verification code for a phone. If the phone number is valid, sends a message with the verification code to the phone number.

login(code:phone:merchantId: completion:)

Logs the user in using a verification code.

login(code:completion:)

Logs the user in using a QR code.

logout(completion:)

Logs the user out.



Delegate Method

Event Triggering the Method

userDidLogin()

This delegate method is called after a user login event.

userDidLogout()

This delegate method is called after a user logout event.

Registering for User Events

📘

Registering for User Events is Optional

If your objects do not require user event notifications, they do not need to register for events.

Step 1. Conforming to UserEventsDelegate

Your class must conform to the UserEventsDelegate.

For example, ProfileViewController uses loginManager and must conform to UserEventsDelegate.

class ProfileViewController: UIViewController, **UserEventsDelegate** {

Step 2. Implement UserEventsDelegate methods

Implement UserEventsDelegate methods in your class.

For example, ProfileViewController implements userDidLogin and userDidlogout to update the UI with the new state.

class ProfileViewController: UIViewController, UserEventsDelegate {
	
	func ** userDidLogin ** () {
		setChildViewControllerDependingOnLoginState()
	}

	func ** userDidlogout ** () {
		setChildViewControllerDependingOnLoginState()
	}
}

Step 3. Registering as a delegate

Call addDelegate(_:) on loginManager and pass it the object that needs to be notified on user events (the object that conforms to UserEventsDelegate).

For example, when ProfileViewController loads, viewDidLoad registers self as the UserEventsDelegate.

Note: To end notifications for events, call removeDelegate(_:) on loginManager

override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        
        **Bringg.shared.loginManager.addDelegate(self)**
        setChildViewControllerDependingOnLoginState()
    }

An Example Using loginManager

The following example shows submitButtonPressed using the Bringg Driver SDK for iOS method login(email:password:merchantId:completion:).

@objc private func submitButtonPressed() {
        guard let email = emailTextField.text, email.count > 0 else { showError("you must enter an email"); return }
        guard let password = passwordTextField.text, password.count > 0 else { showError("you must enter a password"); return }
        
        var merchantId: NSNumber?
        if let merchantIdString = merchantIdTextField.text, let merchantIdInt = Int(merchantIdString) {
            merchantId = NSNumber(value: merchantIdInt)
        }
        
        activityIndicator.startAnimating()
                
        **Bringg.shared.loginManager.login(withEmail: email, password: password, merchantID: merchantId) { (merchantList, error)** in
            self.activityIndicator.stopAnimating()
            
            if let error = error {
                self.showError("there was an error: \(error)")
                return
            }
            if let merchantList = merchantList {
                self.showMerchantList(merchantList, merchantChosen: { (merchantId) in
                    self.merchantIdTextField.text = "\(merchantId)"
                    self.submitButtonPressed()
                })
                return
            }
            
            print("logged in!")
        }
    }

📘

Additional Examples

For a complete example, please refer to the Example folder in the Bringg-iOS-DriverSDK GitHub repo.

loginManager Property

currentUser

Use to get the currently logged in user's information, if a user is currently logged in.

currentUser

User

OPTIONAL

A Bringg User data type. If no user is currently logged in, the value is nil.

Note: Bringg data types are immutable.

loginManager Methods

addDelegate(_:)

Use to register an object as a UserEventsDelegate. In most cases, this method is called on an object's init or load.

@objc public func addDelegate(_ delegate: UserEventsDelegate)

Input Parameters

UserEventsDelegate

delegate

REQUIRED

An object that needs to be notified on user events. The value should conform to UserEventsDelegate protocol (see Registering for User Events, Step 1. Conforming to UserEventsDelegate).

removeDelegate(_:)

Use to unregister an object as a UserEventsDelegate.

@objc public func removeDelegate(_ delegate: UserEventsDelegate)

Input Parameters

UserEventsDelegate

delegate

REQUIRED

An object that no longer needs to be notified on user events. The value should conform to UserEventsDelegate protocol (see Registering for User Events, Step 1. Conforming to UserEventsDelegate).

login(email:password:merchantID:completion:)

Use to login a user in using an email and a password. If successful, this method returns a Merchant data type object. If not successful, this method returns an error message.

func login(withEmail email: String, password: String, merchantID: NSNumber?, completion: @escaping ([Merchant]?, Error?) -> Void)

Input Parameters

email

String

REQUIRED

The login user name.

password

String

REQUIRED

The login password.

merchantID

NSNumber

The merchant Id. This is optional.

Completion

Merchant

A Bringg Merchant data type.

Note: Bringg data types are immutable.

Error

String

If an error occurs, the error message indicating the reason for failure.

login(code:phone:merchantId:completion:)

Use to login a user in using a verification code. If successful, this method returns a Merchant data type object. If not successful, this method returns an error message.

func login(withVerificationCode code: String, phone: String, merchantID: NSNumber?, completion: @escaping ([Merchant]?, Error?) -> Void)

Input Parameters

code

String

REQUIRED

The verification code to login.

phone

String

REQUIRED

The phone number from which the verification code is entered.

merchantID

NSNumber

The merchant Id. This is optional.

Completion

Merchant

Merchant

A Bringg Merchant data type.

Note: Bringg data types are immutable.

Error

String

If an error occurs, the error message indicating the reason for failure.

login(code:completion:)

Use to login a user in using a QR code. If successful, this method returns nothing (a QR code is unique and the merchant is, therefore, known). If not successful, this method returns an error message.

func login(withQRCode code: String, completion: @escaping (Error?) -> Void)

Input Parameters

code

String

REQUIRED

The scanned QR Code to login.

Completion

Error

string

If an error occurs, the error message indicating the reason for failure.

requestVerificationCode(phone:completion:)

Use to request a verification code for a phone. If successful, this method returns nothing. If not successful, this method returns an error message.

func requestVerificationCode(forPhone phone: String, completion: @escaping (Error?) -> Void)

Input Parameters

phone

String

REQUIRED

The phone number (country code + phone number) requesting the verification code.

Completion

Error

String

If an error occurs, the error message indicating the reason for failure.

recoverPassword(email:completion:)

Use to recover an email password for a user. If successful, this method returns nothing. If not successful, this method returns an error message.

func recoverPassword(forEmail email: String, completion: @escaping (Error?) -> Void)

Input Parameters

email

String

REQUIRED

The email address for which the password is recovered.

Completion

Error

String

If an error occurs, the error message indicating the reason for failure.

logout(:completion:)

Use to recover a password by email. If successful, this method returns nothing. If not successful, this method returns an error message.

func logout(completion: @escaping (Error?) -> Void)

Completion

Error

String

If an error occurs, the error message indicating the reason for failure.

UserEventsDelegate Methods

userDidLogin()

This delegate method is called after a user login event. After this method is called, the logged-in user data is available using the currentUser property. You must register for user events to use this method.

Note: In addition to userDidLogin, the completion block passed by the object that called the login method is also called.

func userDidLogout()

userDidLogout()

This delegate method is called after a user logout event. A user logout event can be the result of either calling logout method, or the SDK's internal logic triggering the logout event. You must register for user events to use this method.

Note: In addition to userDidLogout, the completion block passed by the object that called the logout method is also called.

func userDidLogin()