I am trying to implement the sign out functionality in my application. It works okay. However, when I sign out and log in as another user. I can still see the old user information. How can I erase the current user data on sign out so that when I log in as another user the information of the previous user is not visible. I will attach my sign out code for reference. if more info is needed please let me know.
@objc private func LogOutButtonTapped() {
let actionSheet = UIAlertController(title: "Log Out", message: "Are you sure, you want to log out?", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel",
style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Log Out", style: .destructive,
handler: { [weak self] _ in
guard let strongSelf = self else {
return
}
UserDefaults.standard.setValue(false, forKey: "email")
UserDefaults.standard.setValue(false, forKey: "name")
do {
try Auth.auth().signOut()
let vc = LoginViewController()
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = .fullScreen
strongSelf.present(nav, animated: true)
}
catch {
print("Failed to log out")
}
}))
present(actionSheet, animated: true)
}
2
Answers
I have change your code on logout action block. You will have to remove object from user default on logout
I think the concern lies in your Signin function.
It is useful to remove the UserDefault values as pointed out in the first answer upon logout. But it is also a great idea to save new objects to the UserDefault upon signin, and to read these values on the User Profile View Controller.
Upon signin, fetch the User data using your preferred login method (Email login, FB / Google login, etc.), and save each detail on UserDefaults.
For example:
After which, upon segue to the User Profile VC (or whatever View Controller you are using to show the User details), read and display these values:
Update:
I checked the code on GitHub and saw that it uses Firebase Database for the content of the chats / conversations. The code itself has the function to refetch for the conversations upon opening the Conversations VC depending on the user’s email. What happens was that, because the email of the old user stays even after logout, thus, also the chat contents.
The App may be "restarted" if the correct email (of the new user) is saved upon signin and called upon opening the Conversations VC, so the ‘correct’ conversation content will be fetched.
Update:
Try adding some delay before showing the next VC (untested):