Authenticate the user by login and password

To execute authentication on a mobile platform server, create the HHFWAuth application, which uses framework methods:

Method name Brief description
initWithCredentials (_: host: environment: project: device:) The method initializes the framework.
auth (_: password:) The method authenticates the user by login and password.
unAuth () The method cancels user authentication.

The HHFWAuth application includes one screen, the UITextView text view, and UIButton buttons:

NOTE. When the application is initialized, only the Authentication button is available.

To execute the example:

  1. Click the Authentication button.

Clicking the button requests to execute authentication from a mobile platform server. If the authentication is successful, a token is returned.

The Authentication button is not available, and the Unauthentication button is available.

  1. Click the Unauthentication button.

As a result, the authentication will be canceled. The Unauthentication button is not available, and the Authentication button is available.

Application code:

import UIKit
 
class ViewController: UIViewController {
 
    // Determine output variables
    @IBOutlet weak var resultTextView: UITextView!
    @IBOutlet weak var authButton: UIButton!
    @IBOutlet weak var unAuthButton: UIButton!
 
    // Redetermine method
    override func viewDidLoad() {
        super.viewDidLoad()
        self.activateAuth()
        self.initializeFramework()
    }
 
    // Set method that will be executed on button click
    @IBAction func authPressed(_ sender: UIButton) {
        self.auth() { (jsonResult) in
            self.activateUnAuth()
            self.resultTextView.text = String(format: "%@", jsonResult)
        }
    }
 
    @IBAction func unAuthPressed(_ sender: UIButton) {
        self.unAuth()
        self.activateAuth()
        self.resultTextView.text = "Unauth success"
    }
 
    // Initialize framework
    private func initializeFramework() {
        let apiVersion: String = "v1"
        let host: String = "http://testmasterfmp.fsight.cloud/"
        let environment: String = "Leonid_environment"
        let project: String = "Leonid_project"
        let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
        HHFWController.sharedInstance().initWithCredentials(
            apiVersion,
            host: host,
            environment: environment,
            project: project,
            device: device
        )
    }
 
    // Authenticate
    private func auth(completion: @escaping (NSDictionary)->()) {
        let username: String = "test"
        let password: String = "test123"
        HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
            if let jsonDict = jsonResult as? NSDictionary,
                let status = jsonDict["status"] as? String, status == "ok" {
                print("Auth success")
                completion(jsonDict)
            } else {
                print("Auth error")
            }
        }
    }
 
    private func unAuth() {
        HHFWController.sharedInstance().unAuth()
    }
 
    // Set methods to display application user interface elements
    private func activateUnAuth() {
        self.authButton.isEnabled = false
        self.unAuthButton.isEnabled = true
    }
 
    private func activateAuth() {
        self.authButton.isEnabled = true
        self.unAuthButton.isEnabled = false
    }
}

See. also:

Examples of User Authentication on Mobile Platform Server | User Authentication with Password Change