如何从Firebase获取其他用户信息?

时间:2018-08-07 14:53:44

标签: angular firebase ionic-framework angularfire

所以我正在使用ionic 3和firebase来构建我的应用程序,但我目前仍处于停滞状态,因为我想以某种方式获得用户首次登录的信息,如果是这样,我想将他发送到页面他在那里设置个人资料信息。如果没有,他将被重定向到主页。当前,当用户登录时,他将被重定向到TabsPage(主页)。

> login.ts

import { Component, ViewChild } from '@angular/core';
import { NavController, AlertController, Nav } from 'ionic-angular';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import { TabsPage } from '../tabs/tabs';
import { RegisterPage } from '../register/register';
// Models //
import { User } from '../../models/user';
import { Profile } from '../../models/profile';
////////////
import { AngularFireAuth } from 'angularfire2/auth';
import { MenuController, LoadingController } from 'ionic-angular';
import { ProfilePage } from '../profile/profile';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {

    user = {} as User;
    profile = {} as Profile;


    @ViewChild(Nav) nav: Nav;

  constructor(private afAuth: AngularFireAuth, public loadingCtrl: LoadingController, 
    public menuCtrl: MenuController,
    public navCtrl: NavController,
  public afd: AngularFireDatabase, public alertCtrl: AlertController) {
    //Including the functions created below in the constructor
    this.menuCtrl.enable(false, 'myMenu');
  }


   async signIn(user: User) {
    let loading = this.loadingCtrl.create({
        content: ''
      });

      loading.present();

      setTimeout(() => {
        loading.dismiss();
      }, 1000);
      try {
      const result = await this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password);
      console.log(result);
      if (result) {
        this.navCtrl.setRoot(TabsPage);
        }
    }
      catch(e){
          console.log(e);
          if (e.code == "auth/invalid-email"){
            let alert = this.alertCtrl.create({
                title: 'Login Failed',
                subTitle: 'The email/password is invalid, please try again.',
                buttons: ['OK']
            });
            alert.present();
          } else if (e.code == "auth/user-not-found"){
            let alert = this.alertCtrl.create({
                title: 'Login Failed',
                subTitle: 'No Account found with this Email-Adress.',
                buttons: ['OK']
            });
            alert.present();
          } else if (e.code == "auth/wrong-password"){
            let alert = this.alertCtrl.create({
                title: 'Login Failed',
                subTitle: 'The email/password is invalid, please try again.',
                buttons: ['OK']
            });
            alert.present();
          }
      }
  }
        /*if(this.email.value == "admin" && this.password.value == "admin") {
        this.navCtrl.push(TabsPage);
      } else {
          let alert = this.alertCtrl.create({
              title: 'Login Failed',
              subTitle: 'The email/password is invalid, please try again.',
              buttons: ['OK']
          });
          alert.present();
      }*/

  regPagePush() {
      this.navCtrl.push(RegisterPage);
  }

}

0 个答案:

没有答案