User Authentication with Password Change

To execute authentication on a mobile platform server with password change, create the HHFWAuthWithChangePassword application, which uses framework methods:

Method name Brief description
initWithCredentials (_: host: environment: project: device:) The method initializes the framework.
auth (_: password: newPassword: confirmPassword: handler:) The method authenticates the user with password change.

The HHFWAuthWithChangePassword application includes one screen, the UITextField text boxes, the UITextView text view, and the UIButton button:

To execute the example:

  1. Enter the current login and password to the Login and the Password text boxes.

  2. Enter the new password to the New Password box, then confirm it in the Confirm New Password box.

  3. Click the Change Password and Auth button.

Clicking the button requests to execute authentication on a mobile platform server with password change. If the request is successful, a new token is returned.

Application code:

import UIKit
 
class ViewController: UIViewController {
 
    // Determine output variables
    @IBOutlet weak var loginTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var newPasswordTextField: UITextField!
    @IBOutlet weak var confirmPasswordTextField: UITextField!
    @IBOutlet weak var authButton: UIButton!
    @IBOutlet weak var resultTextView: UITextView!
 
    // Redetermine method
    override func viewDidLoad() {
        super.viewDidLoad()
        self.authButton.isEnabled = true
        self.initializeFramework()
    }
 
    // Set method that will be executed on button click
    @IBAction func authWithChangePassword(_ sender: UIButton) {
        self.authButton.isEnabled = false
        self.resultTextView.text = "Changing password"
        self.authWithChangePassword() { (jsonResult) in
            self.authButton.isEnabled = true
            self.resultTextView.text = String(format: "%@", jsonResult)
        }
    }
 
    // Initialize framework
    private func initializeFramework() {
        let apiVersion: String = "v1"
        let host: String = "http://testmasterfmp.fsight.cloud/"
        let environment: String = "DocumentationExampleEnv"
        let project: String = "DocumentationExampleProj"
        let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
        HHFWController.sharedInstance().initWithCredentials(
            apiVersion,
            host: host,
            environment: environment,
            project: project,
            device: device
        )
    }
 
    // Authenticate
    private func authWithChangePassword(completion: @escaping (NSDictionary)->()) {
        let username: String = self.loginTextField.text ?? ""
        let password: String = self.passwordTextField.text ?? ""
        let newPassword: String = self.newPasswordTextField.text ?? ""
        let confirmPassword: String = self.confirmPasswordTextField.text ?? ""
        HHFWController.sharedInstance().auth(username, password: password, newPassword: newPassword, confirmPassword: confirmPassword) { (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")
            }
        }
    }
}

See. also:

Examples of Authentication on Mobile Platform Server | User Authentication by Login and Password