Я хочу знать, вошел ли пользователь в систему или нет, тогда я хочу дождаться ответа onAuthStateChanged, тогда я могу перенаправить на свою домашнюю страницу, потому что я знаю, что пользователь вошел в систему ..
Как я могу это сделать?
Вот мой app.component.ts
import { Component, ViewChild } from '@angular/core';
import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { LoginPage } from '../pages/login/login';
import { HomePage } from '../pages/home/home';
import { ListPage } from '../pages/list/list';
import { WizardSignupPage } from '../pages/wizard-signup/wizard-signup';
import { FireauthServices } from '../providers/fireauth-service/fireauth-service';
import { Storage } from '@ionic/storage';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any;
pages: Array<{title: string, component: any}>;
menuSelected: string;
constructor(public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
private fireauth: FireauthServices,
private storage: Storage) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: HomePage },
{ title: 'List', component: ListPage },
{ title: 'Login', component: LoginPage },
{ title: 'WizardSignup', component: WizardSignupPage }
];
//this.rootPage = LoginPage;
this.rootPage = WizardSignupPage;
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
console.log(this.fireauth.isLoggedIn());
}
openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
this.nav.setRoot(page.component);
}
clickMenu(option){
switch(option){
case 'logout':
this.nav.setRoot(LoginPage);
break;
default:
break;
}
this.menuSelected = option;
}
}
А вот и мой fireauth-services.ts
import { HomePage } from '../../pages/home/home';
import { Nav } from 'ionic-angular';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as firebase from 'firebase/app';
import { AngularFireAuth } from 'angularfire2/auth';
@Injectable()
export class FireauthServices {
// @ViewChild(Nav) nav: Nav;
private user: firebase.User;
constructor(public http: HttpClient,
public fireauth: AngularFireAuth) {
fireauth.authState.subscribe(user => {
this.user = user;
});
console.log('Hello FireauthServiceProvider Provider');
}
signInWithEmail(credentials) {
console.log('Sign in with email');
return this.fireauth.auth.signInWithEmailAndPassword(credentials.email,
credentials.password);
}
signUpWithEmail(credentials) {
console.log('Sign up with email');
return this.fireauth.auth.createUserWithEmailAndPassword(credentials.email, credentials.password);
}
getCurrentUser() {
return this.fireauth.auth.currentUser;
}
isLoggedIn() {
this.fireauth.auth.onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
console.log(user);
return true;
} else {
// No user is signed in.
console.log('nope');
}
});
}
getUID() {
console.log(this.user);
return this.user.uid;
}
}
console.log на app.component.ts возвращает ноль, потому что функция еще не получила ответа.
Другое решение, о котором я думал, - это nav.setRoot из моего fireauth-services.ts, это возможно? используя navCtrl от провайдера?
Извините за мой плохой английский, кстати.