Firebase角色认证角度

问题描述 投票:1回答:1

我正在尝试使用Firebase创建用户身份验证。

我在应用程序中有两个角色:学生和老师,我在注册后将其添加到名为users的Firebase集合中,并且具有现场角色。

我的问题是,我想将学生重定向到学生资料页面,并将老师重定向到老师资料页面。我有两种不同的形式,一种用于学生的登录,一种用于教师的登录。

当我在学生登录名中输入老师的数据时,由于该用户不存在,我希望收到该错误。

现在,我没有任何错误,如果我只有一种登录形式,那么一切都可以正常工作,但是有两种登录形式,所以如果用户是老师并登录到学生,我希望它给我带来错误。] >

这是我为我的身份验证服务中的学生登录的信息

    loginEmailStudent(email: string, pass: string) {
        return new Promise((resolve, reject) => {
            this.auth.signInWithEmailAndPassword(email, pass)
            .then( (user) => {
                this.afs.collection("users").ref.where("email","==", user.user.email).onSnapshot(snap => {
                    snap.forEach(userRef => {
                        if(userRef.data().role == "student"){
                            this.router.navigate(['/student/profile']);
                        } else {
                            //error                   
                        }
                    })
                })
            }).catch(err => console.log(reject(err)));
        });
    }

除角色以外,loginTeachers的情况相同。这是在我的auth组件中,用于提交学生登录的表单。

onSubmit(form: NgForm) {
    if(!form.valid) {
      return
    }
    const email = form.value.email;
    const password = form.value.password;
    this.isLoading = true;
    if(this.isLoginMode) {
      this.authService.loginEmailStudent(email, password)
      .then( (res) => {
      this.isLoading = false;
      }).catch(errorRes => {
        console.log(errorRes);
        switch(errorRes.code) {
          case 'auth/user-not-found' :
            this.error = 'There is no existing user with this email address!';
            break;
          case 'auth/wrong-password' :
            this.error = 'The password is invalid!';
            break;
          case 'auth/invalid-email' :
            this.error = 'The email address is badly formatted!';
            break;
          default:
            this.error = 'An error occured!';
        }
        this.isLoading = false;
      });
    } else {
      this.authService.registerStudent(email, password)
      .then((res) => {
        this.isLoading = false;
        this.authService.isAuth().subscribe( student => {
          if(student) {
            student.updateProfile({
              displayName: '',
              photoURL: this.inputImageUser.nativeElement.value
            }).then( () =>  {
              this.onLoginStudentRedirect();
            }).catch( (error) => console.log('error', error));
          }
        });
      }).catch(errorRes => { 
        console.log(errorRes);
        switch(errorRes.code) {
          case 'auth/email-already-in-use' :
            this.error = 'The email address is already in use by another account!';
            break;
          case 'auth/invalid-email' :
              this.error = 'The email address is badly formatted!';
              break;
          default:
              this.error = 'An error occured!';
        }
        this.isLoading = false;
      });
    }
    form.reset();
  }

我正在尝试使用Firebase创建用户身份验证。我在应用程序中扮演两个角色:学生和老师,我在注册时将它们添加到名为用户的firebase集合中,那里有一个...

angular firebase authentication collections roles
1个回答
0
投票

第一个选项:

© www.soinside.com 2019 - 2024. All rights reserved.