``` extension ContainerController { @objc func buttonAction(_ sender: UIButton){ self.login() isConnectionBtnPressed = true } func checkToken() { guard let data = UserDefaults.standard.object(forKey: kAppAuthExampleAuthStateKey) as? Data else { setupLoginView() return } do { let authState = try NSKeyedUnarchiver.unarchivedObject(ofClass: OIDAuthState.self, from: data) self.setAuthState(authState) self.getUserInfo() } catch { print("catch loadState: \(error)") } } func login() { print("Got configuration: \(config)") if let clientId = kClientID { self.doAuthWithAutoCodeExchange(configuration: config, clientID: clientId, clientSecret: nil) } } func doAuthWithAutoCodeExchange(configuration: OIDServiceConfiguration, clientID: String, clientSecret: String?) { guard let redirectURI = URL(string: kRedirectURI) else { print("Error creating URL for : \(kRedirectURI)") return } guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { print("Error accessing AppDelegate") return } // builds authentication request let request = OIDAuthorizationRequest(configuration: configuration, clientId: clientID, clientSecret: clientSecret, scopes: [OIDScopeOpenID, OIDScopeProfile, "--kclientID--", "offline_access"], redirectURL: redirectURI, responseType: OIDResponseTypeCode, additionalParameters: ["p": "b2c_1_my_app_sign_in_up"]) // performs authentication request print("Initiating authorization request with scope: \(request.scope ?? "DEFAULT_SCOPE")") appDelegate.currentAuthorizationFlow = OIDAuthState.authState(byPresenting: request, presenting: self) { authState, error in if let authState = authState { self.setAuthState(authState) print("Got authorization tokens. Access token") self.getUserInfo() } else { self.setAuthState(nil) print("Authorization error: \(error?.localizedDescription ?? "DEFAULT_ERROR")") } } } func getUserInfo() { let currentAccessToken: String? = self.authState?.lastTokenResponse?.accessToken self.authState?.performAction() { (accessToken, idToken, error) in if error != nil { CoreDataHelper().deleteIDToken("idToken") AuthenticationService().login() print("Error fetching fresh tokens: \(error?.localizedDescription ?? "ERROR")") return } guard let accessToken = accessToken else { print("Error getting accessToken") return } if currentAccessToken != accessToken { print("Access token was refreshed automatically ") self.token = currentAccessToken } else { print("Access token was fresh and not updated ") self.token = accessToken } self.loginView.removeFromSuperview() self.setupHomeController() } } func saveState() { var data: Data? = nil if let authState = self.authState { do { data = try NSKeyedArchiver.archivedData(withRootObject: authState, requiringSecureCoding: false) } catch { print("catch saveState: \(error)") } } UserDefaults.standard.set(data, forKey: kAppAuthExampleAuthStateKey) UserDefaults.standard.synchronize() } func setAuthState(_ authState: OIDAuthState?) { if (self.authState == authState) { return; } self.authState = authState; self.authState?.stateChangeDelegate = self; self.saveState() } } extension ContainerController: ASWebAuthenticationPresentationContextProviding { func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor { return view.window! } ```