简体   繁体   中英

Save Logged in state for appropriate user in IOS and Firebase

I'm using only one login screen for different users on the app I'm developing and I added a code to save the logged in user so that the user doesn't have to log in again next time they open the app. But the fact that I have different users, I'm only able to send the user to one segue.

I've tried adding the code to keep the user logged in, but if I login with another user it sends the user to the same view controller as before. Here's what I tried:

    override func viewDidAppear(_ animated: Bool) {
        if Auth.auth().currentUser != nil {
            // User is signed in.
            performSegue(withIdentifier: "studentSegue", sender: self)
        } else {
            // No user is signed in.
            return
        }
    }

I'm trying to keep the user logged in for different users, but I'm not sure what code to use. To differentiate the users on the login page I used switch case to find the user "Type" and log in on that. But I want to keep the appropriate user logged in. Any help would be much appreciated

EDIT: What am trying to say is if two people on different phones try to login lets say student (phone A) and teacher (phone B) they should just log in once and not have to log in again when they close the app. But my login "PerformSegue" only lets me show the "studentSegue"

Let me restate the question

On my app, I have two different types of users, student and teacher. When a student logs in or restarts the app, it should take them to the student section via a segue. When a teacher logs in or restarts the app, it should take them to a teacher section via a segue.

A few things

Firebase doesn't have user types, only users - to Firebase, every user is the same as every other user. Your app code and structure determine the difference.

One solution is to ask Firebase what kind of user it is upon app start. here's some generic code:

override func viewDidAppear(_ animated: Bool) {
    if Auth.auth().currentUser != nil {
        let uid = currentUser.uid
        let thisUserRef = fbRoot.child("users").child(uid)
        thisUserRef.observeSingleEvent(..... {
           let userType = //get userType from snapshot
           if userType == "student" {
              performSegue(withIdentifier: "studentSegue", sender: self)
           } else {
              performSegue(withIdentifier: "teacherSegue", sender: self)
           }
        }

    } else {
        // No user is signed in.
        return
    }
}

Another option is to store the userType in the userDefaults. So if the user is auth'd, get the user type from the defaults and perform the appropriate segue. I am not a huge fan of this approach due to security but it can work.

After trying quite a lot i managed to find the answer myself. So what i did was set an integer with UserDefaults.standard.set(1, forKey: "isLoggedIn") each time a user would log in. I set number 1 for student and number 2 for teacher and in viewDidAppear i did this:

    override func viewDidAppear(_ animated: Bool) {

        if UserDefaults.standard.float(forKey: "isLoggedIn") == 1 {
            self.performSegue(withIdentifier: "studentSegue", sender: self)
        }

        if UserDefaults.standard.float(forKey: "isLoggedIn") == 2 {
            self.performSegue(withIdentifier: "lecturerSegue", sender: self)
        }

        else{
            return
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM