title: Firebase role authentication Angular
body: <p>I’m trying to create user authentication using Firebase.</p>
  
  <p>I have two roles in my application: student and teacher which I add to a firebase collection named <code>users</code> upon registration where there is a field role.</p>
  
  <p>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. </p>
  
  <p>When I enter the data of a teacher at the student login I want to receive the error as the user does not exist. </p>
  
  <p>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.</p>
  
  <p>That's my login for students in my auth.service</p>
  
  <pre class="lang-js prettyprint-override"><code>    loginEmailStudent(email: string, pass: string) {
          return new Promise((resolve, reject) =&gt; {
              this.auth.signInWithEmailAndPassword(email, pass)
              .then( (user) =&gt; {
                  this.afs.collection("users").ref.where("email","==", user.user.email).onSnapshot(snap =&gt; {
                      snap.forEach(userRef =&gt; {
                          if(userRef.data().role == "student"){
                              this.router.navigate(['/student/profile']);
                          } else {
                              //error                   
                          }
                      })
                  })
              }).catch(err =&gt; console.log(reject(err)));
          });
      }
  </code></pre>
  
  <p>The same is for loginTeachers except the role.
  And this is in my auth component where submitting the form for students login.</p>
  
  <pre class="lang-js prettyprint-override"><code>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) =&gt; {
        this.isLoading = false;
        }).catch(errorRes =&gt; {
          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) =&gt; {
          this.isLoading = false;
          this.authService.isAuth().subscribe( student =&gt; {
            if(student) {
              student.updateProfile({
                displayName: '',
                photoURL: this.inputImageUser.nativeElement.value
              }).then( () =&gt;  {
                this.onLoginStudentRedirect();
              }).catch( (error) =&gt; console.log('error', error));
            }
          });
        }).catch(errorRes =&gt; { 
          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();
    }
  </code></pre>