0

I have to reset my router config dynamically So I used this

//to redirect from AppComponent to LoggedInComponent
//without changing the path
this._router.resetConfig([
    {
      path: '', component: LoggedInComponent
    }
  ]);

It worked fine in straight code by when I tried to run it asynchronously like

//af is AngularFire object
this.af.auth.subscribe(auth=> {
      if (auth) {

          this._router.resetConfig([
                 {
                   path: '', component: LoggedInComponent
                 }
           ]);
      }

      else {
          this._router.resetConfig([
                 {
                   path: '', component: NotLoggedInComponent
                 }
           ]);

     }

    });

it fails .... I even tried to enclose it with a Promise

const logined = new Promise<Boolean>(
  (resolve, reject) => {
    this.af.auth.subscribe(auth=> {
      if (auth) {
        resolve(true);
      }
      else {
        resolve(false);
      }
    })

  }
);

logined.then((res) => {
  console.log('Yeyy resolved as' + res);

      if (res) {

          this._router.resetConfig([
                 {
                   path: '', component: LoggedInComponent
                 }
           ]);
      }

      else {
          this._router.resetConfig([
                 {
                   path: '', component: NotLoggedInComponent
                 }
           ]);


});

logined.catch((err) => {
  console.log('Yeyy Error'); 
});

But it's not working (no error but the router config isn't changing)

The sole reason I am doing this is to maintain the same domain for all like

www.facebook.com

is not same to for logged in and new user but URL doesn't change

I do not want it to be like

 this._router.resetConfig([
             {
               path: 'loggedin', component: LoggedInComponent
             },
             {
               path: 'notloggedin', component: NotLoggedInComponent
             }
       ]);

Please help me out. I am a real noob in Angular2

Mohammad Aasif
  • 328
  • 2
  • 3
  • 15

1 Answers1

0

You will need Auth Guard in angular2. Read here:

https://medium.com/@blacksonic86/angular-2-authentication-revisited-611bf7373bf9#.q0wwwsdem

codetinker
  • 754
  • 10
  • 9