ios - 如何使用 Auth0 注销用户
问题描述
我一直致力于将 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)
}
我希望会话管理器清除存储的数据并向我显示登录屏幕,但我可以在没有任何凭据的情况下再次登录。
解决方案
对于注销,您似乎正在清除设备中的令牌,但实际上并未将用户重定向到Auth0 Logout端点https://YOUR_DOMAIN/v2/logout
。
您需要将用户重定向到注销以确保 Auth0 用户会话和可选的身份提供程序会话(使用federated
查询参数选项)被清除,如果您正在寻求真正的注销并重新验证用户。
您再次登录的原因可能是因为无缝 SSO,Auth0 检测到用户在该浏览器上仍然具有有效会话,因此他们无需凭据即可登录用户。在该浏览器上删除 Auth0 cookie 的唯一方法是将它们重定向到上面的 Logout API。
推荐阅读
- c - 在 STM32f779ii 上设置双组模式
- python - 使用 Pyinstaller 运行迁移
- postgresql - Query optimisation on large join
- unit-testing - ABAP单元测试中的模拟sy-uname
- clickhouse - 如何使用用户事件在平面表上创建用户会话事件
- java - 我怎样才能将上一个任务保留在我的 todolist 中?
- java - XML header is not shown
- c - ZeroMQ - Send and receive multiple messages to a client before accepting new client from queue
- scala - How to aggregate an array of struct in spark with group by
- elm - 如何将函数应用于字符串的所有字符