2
votes

i'm using ionic 2 rc0 and angular 2 and i just added angularfire2 to use firebse auth.

I have all configured and tested (I just see my user logged in the firebase console) but i want to redirect to other page after login.

My code for login:

registerUserWithGoogle() {
   console.log('Google');
   this.af.auth.login();
}

My firebase config is:

export const myFirebaseAuthConfig = {
   provider: AuthProviders.Google,
   method: AuthMethods.Redirect
};

So there is a way to redirect after the method login()?

2

2 Answers

5
votes

AngularFire2 has a property named authState in AngularFireAuth, to which you can subscribe to get the auth state changes

This is recommended way to handle user auth, because you will subscribe to changes in the auth state, other methods might not work as expected, like when user refreshes the page user object might not exist.

import { AngularFireAuth } from 'angularfire2/auth';

@Component({
  templateUrl: 'app.html'
})
export class AppComponent {

  constructor(public afAuth: AngularFireAuth) {}

  ngOnInit() {
    this.afAuth.authState.subscribe(user => {
      if (user) {
        // go to home page
      } else {
        // go to login page
      }
    });
  }

}

Note: You don't need to subscribe on every page. Only subscribe in AppComponent or your app specific root component.

0
votes

I found this to work better in my situation.

loginGoogle(){
  this.af.auth.login({
    provider: AuthProviders.Google
  }).then((success) => {
    this.router.navigate(['/dashboard']);
  });
}