skip to Main Content

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


  1. I have change your code on logout action block. You will have to remove object from user default on logout

        @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
                                                }
                                                
                                                let firebaseAuth = Auth.auth()
                                                do {
                                                    try firebaseAuth.signOut()
                                                } catch let signOutError as NSError {
                                                    print ("Error signing out: %@", signOutError)
                                                }
                                                **remove object on logout click**
                                                USER_DEFAULT.removeObject(forKey: "email") // remove email
    
                                                    let vc = LoginViewController()
                                                    let nav = UINavigationController(rootViewController: vc)
                                                    nav.modalPresentationStyle = .fullScreen
                                                    strongSelf.present(nav, animated: true)
                                               
    
                                            }))
        present(actionSheet, animated: true)
        
    }
    
    Login or Signup to reply.
  2. 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:

    let defaults = UserDefaults.standard
    defaults.set("RandomUserName", forKey: "username")
    defaults.set("[email protected]", forKey: "email")
    

    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:

    let defaults = UserDefaults.standard
    let username = defaults.object(forKey: "username") as? String
    let email = defaults.object(forKey: "email") as? String
    

    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):

    @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 }
                 
        do {
            try Auth.auth().signOut()
            
            // Remove UserDefaults
            let defaults = UserDefaults.standard
            defaults.removeObject(forKey: "email")
            defaults.removeObject(forKey: "name")
            defaults.synchronize()
    
            // Add some delay for the Firebase to perform logout function first before showing the VC
            
            DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(5000)) {
                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)
        } }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search