Add a New Trusted Device

Steps

There are 2 steps that's needed to add a new Trusted Device:

  1. Show the QR Code of the New Device

  2. Scan the New Device's QR Code using the Trusted Device

Step 1: Show the QR Code of the New Device

To show the QR Code of the new device:

Example:

class SomeUIVC: UIViewController {
  var cotter: Cotter?

  override func viewDidLoad() {
    super.viewDidLoad()

    self.cotter = Cotter(
      apiSecretKey: "588d6f67-0981-4718-899b-bcd512de1aca",
      apiKeyID: "w4FK6Zz0XIhtGY3o5biI",
      cotterURL: "https://www.cotter.app/api/v0",
      userID: "hello@example.com",
      configuration: [:]
    )
  }

  // On Button Click
  @IBAction func registerThisDeviceAsTrusted(_ sender: UIButton) {
    func callback(token:String, err: Error?) {
        if err != nil {
          print(err?.localizedDescription)
          self.textLabel.text = "Failed to register new device!"
          return
        }
        self.textLabel.text = "Successfully registered new device!"
    }

    /* Open QR Code that can be scanned from the Trusted Device. */
    self.cotter?.registerNewDevice(vc: self, cb: callback)
  }
}

This will open a View Controller with a QR Code that can be scanned from the Trusted Device. After registration is done, the above-defined callback function would be called.

For the above callback, a text label in the SomeUIVC view controller will be populated to let the user know whether registration succeeded or failed. Otherwise, if desired, the user can also perform a segue to another view controller (on success or failure) in the callback function as well.

Case 1: Successfully registered as a Trusted Device

Once it's scanned and registered successfully as, the View Controller will show a success image, and close after 3 seconds.

Case 2: The new device is still not registered as a Trusted Device after 3 minutes

If after 3 minutes, the new device is still not registered as a Trusted Device, it will show an error message.

Step 2: Scanning the QR Code from a Trusted Device

To scan the QR Code from a Trusted Device, call the function:

class SomeUIVC: UIViewController {
  var cotter: Cotter?

  override func viewDidLoad() {
    super.viewDidLoad()

    self.cotter = Cotter(
      apiSecretKey: "588d6f67-0981-4718-899b-bcd512de1aca",
      apiKeyID: "w4FK6Zz0XIhtGY3o5biI",
      cotterURL: "https://www.cotter.app/api/v0",
      userID: "hello@example.com",
      configuration: [:]
    )
  }

  // On Button Click
  @IBAction func executeScanOfOtherDevice(_ sender: UIButton) {
    /* Open a View Controller that opens the Camera to scan the
       QR Code on the other non-trusted device. We do not need 
       to pass in a callback, as the user will be redirected
       back to this view controller. */
    self.cotter?.scanNewDevice(vc: self, cb: nil)
  }
}

This will open a View Controller that opens the Camera. It will ask for permission to access the camera. When the QR Code is detected and scanned, it will automatically attempt to register the new device. After scanning is done, the user will be redirected back to the above SomeUIVC view controller.

When the QR Code is detected and registered, the SDK will automatically show whether it was successful or if there's an error.

🎉 You're done!

Now you can authenticate from the new device without requiring approval.

Last updated