如果该用户使用该页面离子,则重定向用户

时间:2017-08-20 13:05:50

标签: angular firebase ionic-framework routes

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

情景:

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

我计划在我的User |_UID |_Checker:"1/0" // Either 1 or 0 上,首次登录时,在我的火力基础上,我将设置一个像1(如果用户完成测试)和0&#39的检查器; s(如果用户未完成测试)

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();
      });
    })

以下是登录的代码:

 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)
    }
  })}

Auth.ts

    string connstring1 = "Data Source=server1;Initial Catalog=northwind;user=xxx;password=xxx";

    //Serializing an IDataReader into a ProtoBuf:

    Stream buffer = new MemoryStream();
    using (var c1 = new SqlConnection(connstring1))
    {
       c1.Open();
        // Serialize SQL results to a buffer
        using (var command = new SqlCommand("SELECT * FROM products", c1))
        using (var reader = command.ExecuteReader())
            DataSerializer.Serialize(buffer, reader);

        // Deserialize, Read them back
        buffer.Seek(0, SeekOrigin.Begin);
        using (var reader = DataSerializer.Deserialize(buffer))
        {
            while (reader.Read())
            {
                Console.WriteLine(reader["ProductName"]);
            }
        }
    }

}

1 个答案:

答案 0 :(得分:0)

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

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

假设checker字段是在用户auth数据下创建的自定义字段。而且_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)
}