I am using firebase auth in my swift app, I am saving the logged user firebase uid in UserDefaults so when the user close the app and return if the uid exist in UserDefaults it will get the user's infos from the firebase and if it is nil he will show him the auth viewController. It works like charme but I found that when the app is not opened for a période the firebase disconnect the user and the uid in UserDefaults still found so the app will throw an exception on Auth.auth().currentUser! . So my question is how to keep the user connected to firebase ?
1
votes
You may want to review the Firebase documentation on Authentication which states When a user signs up or signs in, that user becomes the current user of the Auth instance. The instance persists the user's state, so that refreshing the page (in a browser) or restarting the application doesn't lose the user's information. So that would indicate that the Firebase handles persistence for the user - even with an app restart.
- Jay
Hey Jay ! I mentioned in my question that I don't lose the current user of the Auth instance when I close the app it only happen when I don't open the app for a period of time so the firebase disconnect the user, so I want the firebase to refresh the session when the default time of session has reach the end or to change the default time of the session to be forever.
- mark
So is this a scenario where you want to store the login info (username/password) in the user defaults so automatically log back in on App start?
- Jay
1 Answers
0
votes
Try this:
// Create a callback which logs the current auth state
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.onAuth(function(authData) {
if (authData) {
console.log("User " + authData.uid + " is logged in with " + authData.provider);
} else {
console.log("User is logged out");
}
});
You can also check here: https://www.firebase.com/docs/web/guide/user-auth.html#section-monitoring-authentication