Integrate logout functionality into the app

This topic enables the iOS mobile app to add OIDC logout functionality that allows a logout from the app and OpenID provider using the SDK

Build the logout functionality in the iOS mobile app using the CyberArk Identity mobile SDK. The logout functionality enables the app to log the user out of the mobile app and the OpenID provider. The logout functionality also revokes the tokens from the iOS keychain.

Use the closeSession() method to close the session as shown in the code snippet below:

func closeSession() {
        guard let config = plistValues(bundle: Bundle.main) else { return }
        guard let account =  CyberArkAuthProvider.webAuth()?
                .set(clientId: config.clientId)
                .set(domain: config.domain)
                .set(redirectUri: config.redirectUri)
                .set(applicationID: config.applicationID)
                .set(presentingViewController: self)
                .setCustomParam(key: "", value: "")
                .set(webType: .sfsafari)
                .build() else { return }
        CyberArkAuthProvider.closeSession(account: account)
      
  }

Declare a logout observer to receive the logout session result, and then clear the keychain data using KeyChainWrapper. Use the following code snippet for the logout observer:

func addLogoutObserver(){
        CyberArkAuthProvider.didReceiveLogoutResponse = { (result, message) in
            if result {
                DispatchQueue.main.async {
                    self.dismiss(animated: true) {
                    try KeyChainWrapper.standard.delete(key: KeyChainStorageKeys.accessToken.rawValue)

                     try KeyChainWrapper.standard.delete(key: KeyChainStorageKeys.grantCode.rawValue)

                     try KeyChainWrapper.standard.delete(key: KeyChainStorageKeys.refreshToken.rawValue)

                     try KeyChainWrapper.standard.delete(key: KeyChainStorageKeys.access_token_expiresIn.rawValue)   
                    }
                }
            }
        }
    }