angularfirebase-authentication icon indicating copy to clipboard operation
angularfirebase-authentication copied to clipboard

not routing after successful login

Open suriyaJaay opened this issue 5 years ago • 21 comments

Hi,

I'm not able to route to dashboard even though my login is successful. once click on sign-in button. app is loading for a seconds and remains in login page. but it's supposed to go to dashboard.

could you pls tell me, how to resolve this.?

and one more thing i want to know, what is the purpose of firebase/app in service? i dont find ' auth' in node_modules under firebase folder.

thank you

suriyaJaay avatar Jun 05 '19 10:06 suriyaJaay

Same issue here. Nothing happens on a successful login. I've tried removing 'this.ngZone.run' without any results. Updated all packages to Angular 8 and also updated Firebase packages, still no change.

billbarni avatar Sep 04 '19 19:09 billbarni

This is also happening to me, only with username + password login, not via a social provider. When using something like Google it redirects as expected.

Vixxd avatar Sep 15 '19 20:09 Vixxd

There's a conflict with the VerifiedEmail field. The "isSignedIn" condition is based on also on the Email Verification value. If you'd check your firestore, you'd see that it's set to False from some reason.

So, You aren't being redirected to the Dashboard since you're basically not logged in.

ofirSparka19 avatar Nov 17 '19 08:11 ofirSparka19

You can do it this way.

// Sign in with email/password
SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
    .then((result) => this.SetUserData(result.user))
    .then(() => this.router.navigate(['/dashboard']))
    .catch((error) => window.alert(error.message));
}

MihajloNesic avatar Feb 12 '20 17:02 MihajloNesic

You can do it this way.

// Sign in with email/password
SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
    .then((result) => this.SetUserData(result.user))
    .then(() => this.router.navigate(['/dashboard']))
    .catch((error) => window.alert(error.message));
}

Still doesn't work for me :( Node Version: v12.14.1 Angular CLI Version: 9.0.2 Fire: 5.4.2

blouflashdb avatar Feb 13 '20 17:02 blouflashdb

Still doesn't work for me :( Node Version: v12.14.1 Angular CLI Version: 9.0.2 Fire: 5.4.2

Did you notice the slash before dashboard?

I am using lower versions tho. Node: 10.16.3 Angular CLI: 8.3.25 Fire: 5.1.0

Edit: Tested with Fire 5.4.2, it works perfectly

MihajloNesic avatar Feb 13 '20 21:02 MihajloNesic

Still doesn't work for me :( Node Version: v12.14.1 Angular CLI Version: 9.0.2 Fire: 5.4.2

Did you notice the slash before dashboard?

I am using lower versions tho. Node: 10.16.3 Angular CLI: 8.3.25 Fire: 5.1.0

Edit: Tested with Fire 5.4.2, it works perfectly

Yes, I replaced my SignIn methode with yours. I guess its a bug in the Angular CLI. ngOnInit in the Dashboard is not being called for some reason.

blouflashdb avatar Feb 14 '20 10:02 blouflashdb

the reason is not routing is because of the fact that the email is not verified. should you log out and back in the user info is being refreshed. I fixed this problem in my forked branch and also added phone SMS login, custom claims, better flow etc check it out https://github.com/danmincu/angularfirebase-authentication

danmincu avatar Feb 19 '20 06:02 danmincu

Hello, I solved it with setInterval () in signIn(email, password)

SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.ngZone.run(() => {
          let i = 0;
        const loginTime = setInterval (() => {
              if( i = 2 ){
                this.router.navigate(['/dashboard']);
                clearInterval(loginTime)
              } //console.log(i)
        i++
},1000) 
        });
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }

and secureInnerPage.guard.ts

canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
      if(this.authService.isLoggedIn){
        //window.alert("your text")
        this.router.navigate(['/dashboard'])
      }
    return true;
  }

JDmko avatar Feb 21 '20 12:02 JDmko

Hello, I solved it with setInterval () in signIn(email, password)

SignIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.ngZone.run(() => {
          let i = 0;
        const loginTime = setInterval (() => {
              if( i = 2 ){
                this.router.navigate(['/dashboard']);
                clearInterval(loginTime)
              } //console.log(i)
        i++
},1000) 
        });
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }

In my case, Login was not happening on First click on login button, while the user token were being set successfully in localstorage. I used your method of setinterval and now it is working fine on first click. What is the reason for such behavior?

ghost avatar Feb 26 '20 08:02 ghost

I have the same problem. I think that the SetInterval solution is not a viable solution over time.

foch01 avatar Mar 14 '20 19:03 foch01

This is how I did it

signIn(email, password) {
    return this.afAuth.auth.signInWithEmailAndPassword(email, password)
    .then((result) => this.setUserData(result.user))
    .then(() => this.router.navigate(['/dashboard']));
}

My account has a verified email

MihajloNesic avatar Mar 14 '20 20:03 MihajloNesic

Doesn't work for me :/

foch01 avatar Mar 14 '20 21:03 foch01

@foch01 it's easy to debug why isn't it working for you; Routing after logging to dashboard is guarded by the Authguard that depends on isLoggedIn(); I bet that ! user.emailIsVerified==false is the culprit. I forked this repo here and solved that problem by "waiting" for the end-user to verify the email and re-read the settings before navigating to dashboard. You can also relax the isEmailVerified rule.

danmincu avatar Mar 15 '20 01:03 danmincu

Even removing the user.emailVerified condition! == false it does not work.

My email address is verified and equal to true

foch01 avatar Mar 15 '20 09:03 foch01

You signIn and try to navigate to a guard protected route, thus the canActivate is used, but the canActivate uses localStorage user to determine if you can perform navigation and the localStorage user is set in the authState.subscribe, which is called AFTER canActivate. A simple solution to prove this is just set local storage with user data inside signIn method before this.router.navigate. Then you'll see that canActivate is able to get the user from localStorage and can navigate. Idk if this is the best solution, I'm just learning Angular.

RodriFerretty avatar May 13 '20 07:05 RodriFerretty

What I did, was to change my canActivate function, so now it look like this: next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { if(this.authService.isLoggedIn !== true) { this.router.navigate(['sign-in']) } return true; }

then, i changed my isLogged function, so now it looks like this get isLoggedIn(): boolean { const user = JSON.parse(localStorage.getItem('user')); return (user === null) ? false : true; }

Remenber that you have to check that your function is saving locally the user in json format. Actually, i just removed the verificateEmail function, it didn't work for me.

IvanMtze avatar May 14 '20 18:05 IvanMtze

Solution:

  SignIn(email: string, password: string) {
    return this.afAuth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.SetUserData(result.user);
        this.afAuth.authState.subscribe((user) => {
          if (user) {
            this.router.navigate(['dashboard']);
          }
        })
      })
      .catch((error) => {
        window.alert(error.message)
      })
  }

stayacid avatar Jun 16 '20 12:06 stayacid

Solution:

// Auth logic to run auth providers
  async authLogin(provider: auth.AuthProvider) {
    try {
      await this.afAuth.signInWithPopup(provider).then(value => {
        this.setUserData(value.user).then(() => {
          this.ngZone.run(() => {
            this.router.navigate(['dashboard']);
          });
        });
      });
    }
    catch (error) {
      window.alert(error);
    }
  }

omararturo avatar Jun 17 '20 02:06 omararturo

Solution:

  SignIn(email: string, password: string) {
    return this.afAuth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.SetUserData(result.user);
        this.afAuth.authState.subscribe((user) => {
          if (user) {
            this.router.navigate(['dashboard']);
          }
        })
      })
      .catch((error) => {
        window.alert(error.message)
      })
  }

This your solution is the best just tried and it works perfectly

lepatlamass avatar Dec 21 '20 11:12 lepatlamass

Solution:

  SignIn(email: string, password: string) {
    return this.afAuth.signInWithEmailAndPassword(email, password)
      .then((result) => {
        this.SetUserData(result.user);
        this.afAuth.authState.subscribe((user) => {
          if (user) {
            this.router.navigate(['dashboard']);
          }
        })
      })
      .catch((error) => {
        window.alert(error.message)
      })
  }

perfect, thanks

cflorioluis avatar Aug 23 '21 22:08 cflorioluis