【发布时间】: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)
}
我希望会话管理器清除存储的数据并向我显示登录屏幕,但我可以在没有任何凭据的情况下再次登录。
【问题讨论】: