Sometimes when my app launch with a bad internet connection the user got logged out.
I don't want this to happen.. I want the user to continue being logged in even if the app didn't launch properly because of bad internet connection..
"@angular/fire": "^5.1.0",
I attached my AuthService code, also my dependencies.. what I am doing wrong?
import { Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
@Injectable()
export class AuthService {
private user: Observable<firebase.User>;
public userDetails: firebase.User = null;
userAuthDoneSubject = new Subject();
constructor(
private firebaseAuth: AngularFireAuth) {
this.user = firebaseAuth.authState;
this.user.subscribe(
(userDetails) => {
if (userDetails) {
this.userDetails = userDetails;
}
else {
this.userDetails = null;
}
this.userAuthDoneSubject.next();
},(error) => {this.userAuthDoneSubject.error("AuthService:error authenticate"); }
);
}
isLoggedIn() {
if (this.userDetails == null ) {
return false;
} else {
return true;
}
}
logout() {
return this.firebaseAuth.auth.signOut();
}
signinUser(customToken:any){
return this.firebaseAuth.auth.signInWithCustomToken(customToken)
}
getJWT() {
return this.firebaseAuth.auth.currentUser.getIdToken(true);
}
}
"dependencies": {
"@agm/core": "^1.0.0",
"@angular/animations": "^6.1.0",
"@angular/common": "^6.1.0",
"@angular/compiler": "^6.1.0",
"@angular/core": "^6.1.0",
"@angular/fire": "^5.1.0",
"@angular/forms": "^6.1.0",
"@angular/http": "^6.1.0",
"@angular/platform-browser": "^6.1.0",
"@angular/platform-browser-dynamic": "^6.1.0",
"@angular/router": "^6.1.0",
"@types/googlemaps": "^3.30.19",
"angular2-multiselect-dropdown": "^4.6.2",
"core-js": "^2.5.4",
"css-toggle-switch": "^4.1.0",
"firebase": "^5.5.9",
"ng-lazyload-image": "^6.1.0",
"ngx-owl-carousel-o": "^0.1.2",
"ngx-page-scroll-core": "^6.0.2",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.3.3",
"zone.js": "~0.8.26"
},
When you use firebase into a client app, the firebase script will be loaded and then it will check if a previous session exists and then will validate it against auth server. If for some reason internet connection fails, this process will not succeed, so the event onAuthStateChanged will not be fired, which will cause you app state to be in a not logged in state.
Your code is based on a third party library (which I don't know its internals) that wraps firebase functionalities and probably will listen to firebase events.
I suggest you save the firebase token in localStorage when the user logs in, and in case internet connection fails, you can try to log in using the token that was saved in localStorage. Or you can listen to net connection events. Please check this docs