I was wondering if anyone has used ionic and firebase and allowed persistent authentication. When I create an IPA/APK and download the app to my device, every time I close the app I have to log in again.
Upon login with $authWithPassword the call back includes uid and token. If I use get import ngStorage as a dependency, how can I use uid and token to persist auth?
For login, the user login calls the login function which is linked to the Auth.login function in my factory.
login: function(user) {
return auth.$authWithPassword({
email: user.email,
password: user.password
}, function(error, authData) {
switch(error.code) {
case "INVALID_EMAIL":
console.log("Log in with a valid email.");
break
case "INVALID_PASSWORD":
console.log("Password or email is incorrect");
break
default:
console.log("Enter a valid email and password");
}
})
.then(function(authData) {
console.log("login: Logged in with uid: ", authData);
$localStorage.uid = authData.uid;
$localStorage.token = authData.token;
})
.catch(function(error) {
alert("Error: " + error);
});
I am not sure how I would persist authentication with uid and token. Is it possible to do it without the user password?
Thanks in advance for help.
I did find the answer quite some time back ago, but forgot to update here. Hopefully it will be useful for people who use firebase auth with ionic framework.
As shown in the question part, just make sure the token is saved upon log in. This should work for social media logins firebase provide also.
It was not obvious to myself when I first tried as a first time developer, but a saved token can be used to log in the user back each time the app is opened.
On the .run() part of your ionic app, inject $localStorage, $firebaseAuth and $state add the following:
In summary if there is a token saved to localstorage, use it to log in using firebases $authWithCustomToken().