【问题标题】:How to log out an user using Auth0如何使用 Auth0 注销用户
【发布时间】:2023-03-31 22:42:01
【问题描述】:

我一直致力于将 Auth0 与我的应用程序集成。当我单击登录时,auth0 会将我重定向到提供的登录 URL。成功登录后,我将返回另一个 ViewController。在该视图控制器中,我有一个退出按钮,可将我重定向到登录页面。现在,当我再次单击登录时,它不会要求登录凭据并将我带到 ViewController,因为 auth0 存储登录用户的缓存数据。但是当我单击注销按钮时,我想要 auth0删除缓存数据,因为我希望用户重定向到登录页面,用户每次登录时都应该输入凭据。有可能吗?

func signIn() {
        print("Sign In")

        guard let clientInfo = plistValues(bundle: Bundle.main) else { return }
        Auth0
            .webAuth()
            .scope("openid profile")
            .audience("https://" + clientInfo.domain + "/userinfo")
            .start {
                switch $0 {
                case .failure(let error):
                    print("Error: \(error)")
                case .success(let credentials):
                    if let accessToken = credentials.accessToken, let refreshToken = credentials.refreshToken, let idToken = credentials.idToken {
                        UserDefaults.standard.setValue(accessToken, forKeyPath: "accessToken")
                        UserDefaults.standard.setValue(refreshToken, forKeyPath: "refreshToken")
                        UserDefaults.standard.setValue(idToken, forKeyPath: "idToken")

                        let loadingAlert = UIAlertController.customAlert(title: "Success", message: accessToken)
                        loadingAlert.presentInViewController(self)
                    }

                    if (!SessionController.shared.store(credentials: credentials)) {
                        print("Failed to store credentials")
                    }
                    else {
                        SessionController.shared.retrieveProfile { error in
                            DispatchQueue.main.async {
                                guard error == nil else {
                                    print("Failed to retrieve profile: \(String(describing: error))")
                                    return self.signIn()
                                }

                                let storyBoard = UIStoryboard(name: "Main", bundle: nil)
                                let vc = storyBoard.instantiateViewController(withIdentifier: "profileViewController") as! ProfileViewController
                                self.present(vc, animated: true, completion: nil)
                            }
                        }
                    }
                }
        }
    }
    @IBAction func btnActionSignOut(_ sender: UIButton) {

        let alertController = UIAlertController(title: AppSettings.shared.appName, message: "Are you sure you want to logout?", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "Yes", style: .default) { (action:UIAlertAction) in
            // Clear access tokens and logout user.
            UserDefaults.standard.setValue(nil, forKeyPath: "accessToken")
            UserDefaults.standard.setValue(nil, forKeyPath: "refreshToken")
            UserDefaults.standard.setValue(nil, forKeyPath: "idToken")

            _ = SessionController.shared.logout()
            self.dismiss(animated: true, completion: nil)
        }
        let noAction = UIAlertAction(title: "No", style: .default) { (action:UIAlertAction) in
            // Do nothing. Alert dismissed.
        }
        alertController.addAction(okAction)
        alertController.addAction(noAction)
        self.present(alertController, animated: true, completion: nil)
    }

我希望会话管理器清除存储的数据并向我显示登录屏幕,但我可以在没有任何凭据的情况下再次登录。

【问题讨论】:

    标签: ios swift auth0


    【解决方案1】:

    看起来您正在从设备中清除令牌,但实际上并未将用户重定向到Auth0 Logout 端点https://YOUR_DOMAIN/v2/logout

    您需要将用户重定向到注销以确保 Auth0 用户会话和可选的身份提供者会话(使用 federated 查询参数选项)如果您正在寻求真正的注销并重新验证用户身份。

    您再次登录的原因可能是因为Seamless SSO,Auth0 检测到用户在该浏览器上仍有有效会话,因此他们无需凭据即可登录用户。删除该浏览器上的 Auth0 cookie 的唯一方法是将它们重定向到上面的 Logout API。

    【讨论】:

    • 但是我在 Auth0 应用程序详细信息中在哪里提供注销 API。
    猜你喜欢
    • 2021-02-16
    • 2021-10-21
    • 2018-02-13
    • 2023-04-10
    • 2014-07-17
    • 2012-06-13
    • 1970-01-01
    • 2020-11-12
    • 2020-09-27
    相关资源
    最近更新 更多