简体   繁体   English

Firebase 角色认证 Angular

[英]Firebase role authentication Angular

I'm trying to create user authentication using Firebase.我正在尝试使用 Firebase 创建用户身份验证。

I have two roles in my application: student and teacher which I add to a firebase collection named users upon registration where there is a field role.我在我的应用程序中有两个角色:学生和老师,我在注册时添加到users集合中,其中有一个字段角色。

My issue is that I want to redirect the student to a student-profile page and the teacher to a teacher profile page.我的问题是我想将学生重定向到学生资料页面,将教师重定向到教师资料页面。 I have two different forms, one for student's login and one for teacher's login.我有两种不同的 forms,一种用于学生登录,一种用于教师登录。

When I enter the data of a teacher at the student login I want to receive the error as the user does not exist.当我在学生登录时输入老师的数据时,我想收到错误,因为用户不存在。

Now I don't get any errors, if I had only one form of login everything would work correctly, but I have two, so I want it to give me errors if the user is a teacher and logs in to students.现在我没有收到任何错误,如果我只有一种登录形式,一切都会正常工作,但我有两个,所以如果用户是教师并登录学生,我希望它给我错误。

That's my login for students in my auth.service这是我的 auth.service 中学生的登录信息

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

The same is for loginTeachers except the role.除了角色之外,loginTeachers 也是如此。 And this is in my auth component where submitting the form for students login.这是在我的身份验证组件中提交学生登录表单的地方。

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

First option:第一个选项:

Why don't you have a simple login method for both student and teacher that will resolve to an object that has a property of either student or teacher.为什么不为学生和教师提供一个简单的login方法,该方法将解析为具有学生或教师属性的 object。

On your onSubmit method you can deal with the error handling accordingly.在您的onSubmit方法上,您可以相应地处理错误处理。


The second option第二种选择

is to keep almost everything the same but instead of using the redirect ( this.router.navigate(['/student/profile']); ) in the login method you throw a specific error.是使几乎所有内容都保持不变,而不是在login方法中使用重定向( this.router.navigate(['/student/profile']); ),您会抛出特定错误。 And you catch that specific error inside onSubmit .然后您在onSubmit中捕获到该特定错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM