Home > Software engineering >  How to keep user logged in using Firebase in Swift?
How to keep user logged in using Firebase in Swift?

Time:02-10

I'm kinda new to Swift programming and I can't find any tutorial on how to keep users logged in after closing the app.

I already did log in and sign up functionality in my app, but what are my steps to keep users logged after closing the app?

CodePudding user response:

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

CodePudding user response:

You can also use Userdefault to persist login state, such that upon successful login, you would persist a value lets call it isLoggedIn which is of type Bool and then all you need to do is to check for isLoggedIn upon each launch of your app, if true, then user is loggedin, you show your landing page otherwise you show your login screen.

  • Related