【问题标题】:Redirecting user if that user if done with that page Ionic如果该用户完成了该页面,则重定向用户 Ionic
【发布时间】:2018-01-28 15:58:12
【问题描述】:

我对如何在登录时重定向用户感到困惑

场景:

-> 填写登录详细信息并提交后,用户被重定向到考试测试。填写完成后,用户现在将被重定向到主页。当用户决定退出,然后再次登录时,用户必须被重定向到主页。

我计划在我的auth.ts 上,在第一次登录时,在我的 firebase 上,我将设置一个检查器,例如 1(如果用户完成测试)和 0(如果用户没有完成测试)

User
|_UID
  |_Checker:"1/0" // Either 1 or 0

下面是登录代码:

loginUser() {
if (!this.loginForm.valid) {
  console.log(this.loginForm.value);
} else {
  this.authData.loginUser(this.loginForm.value.email, this.loginForm.value.password)
    .then(authData => {
      //if user is done with the test
      this.navCtrl.push(HomePage);
      //else
      this.navCtrl.push(TestPage);

    }, error => {
      this.loading.dismiss().then(() => {
        let alert = this.alertCtrl.create({
          message: error.message,
          buttons: [
            {
              text: "Ok",
              role: 'Cancel'
            }
          ]
        });
        alert.present();
      });
    })

Auth.ts

 loginUser(newEmail: string, newPassword: string): firebase.Promise<any> {
return this.afAuth.auth.signInWithEmailAndPassword(newEmail, newPassword)
   .then(newUser => {
    const x = this.afAuth.auth.currentUser.uid;

    if (x != null) {
      firebase.database().ref('/Users').child(newUser.uid).set({
        email: newEmail,
      })
    } else {
      firebase.database().ref('/Users').child(newUser.uid)
    }
  })}

【问题讨论】:

    标签: angular firebase ionic-framework routes


    【解决方案1】:

    如果您确实添加了该检查器,那么您只需使用条件添加它

    User._Checker ? this.navCtrl.push(HomePage) : this.navCtrl.push(TestPage);
    

    这是假设检查器字段是在用户身份验证数据下创建的自定义字段。而且_Checker 是一个布尔值

    更新

    因此,如果 uid 检查器字段不存在,那么您需要在 authdata 中创建一个自定义字段,我什至不确定您是否可以这样做,或者在用户首次完成注册过程时在数据库中创建该字段。如果您想添加其他用户信息,我认为这很常见。

    .then(newUser => {
    const x = this.afAuth.auth.currentUser.uid;
    
    if (x != null) {
      firebase.database().ref('/Users').child(newUser.uid).set({
        email: newEmail,
        _Checked:true // <- added
      })
    } else {
      firebase.database().ref('/Users').child(newUser.uid)
    }
    

    【讨论】:

    • UID 未定义
    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2014-11-24
    • 2016-04-21
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多