How to Keep a User Logged In? Swift

How to keep user logged in using Firebase in Swift?

By default, users are already kept logged into the Firebase after closing the app.

You can add an auth state listener to see this in action. If at app startup (after Firebase is configured), you listen for the authorization state, you'll see that the app gets notified that the user is logged in already, assuming they had a valid authorization state at the time the app was last closed.

handle = Auth.auth().addStateDidChangeListener { auth, user in
// ...
}

See documentation: https://firebase.google.com/docs/auth/ios/start#listen_for_authentication_state

How do I keep a user logged in? Swift

If you don't logOut. User keeps at login state. For check is logged in user exist :

Auth.auth().addStateDidChangeListener { auth, user in
if user != nil{
//Logged in user exist.
}
}

This your segue id. Write whatever you want :
Sample Image

Edited for your code :

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.

setUpElements()

Auth.auth().addStateDidChangeListener { auth, user in
if user != nil{
// User is signed in.
print("User is not logged out.")
self.performSegue(withIdentifier: "yourSegueId", sender: nil)
} else {
// No user is signed in.
print("No user is signed in.")
}
}
}

Hope it helps...

How to keep the user logged in, in Swift (iOS)?

The use of a navigation controller does not affect the user defaults. Anywhere in the app, you can call

let defaults = UserDefaults.standard

You can then write to defaults by calling

defaults.setValue(value, forKey: "key")

And later retrieve this value by doing

let savedValue = default.value(forKey: "key")

Depending on what you're using for login, you will need to decide what needs to be saved and retrieved, and where and when that needs to happen

swift firebase keep user signed in

I ran into this issue before and I have the exact solution for this.

Add these two variables to the top of your AppDelegate:

let userDefault = UserDefaults.standard
let launchedBefore = UserDefaults.standard.bool(forKey: “usersignedin”)

Those allow you to use userDefaults and then allows you to set a key called 'launchedBefore'.

Within your Firebase Login Function add this:

//You should have the next line for whatever method you're using to authenticate your users using firebase

Auth.auth().signInAndRetrieveData(with: credential) { (result, error) in
if error == nil {
self.userDefault.set(true, forKey: "usersignedin")
self.userDefault.synchronize()
print(result?.user.email)

If there is no error with login set 'usersignedin' bool to true.

In your login View Controller add this to viewDidAppear:

            if userDefault.bool(forKey: "usersignedin") {
performSegue(withIdentifier: "Verified", sender: self)
}

If the user is logged in to the app perform segue to the protected page.

Save Logged in state for appropriate user in IOS and Firebase

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
}

}


Related Topics



Leave a reply



Submit