angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.64k stars 2.2k forks source link

feat(redirectUnauthorizedTo): allow passing query parameters #2287

Closed AmitMY closed 3 years ago

AmitMY commented 4 years ago

Version info

Angular: ~9.0.0-rc.7

Firebase: ^7.6.1-0

AngularFire: ^5.3.0-rc.4

Using the AngularFireAuthGuard works, but does not support redirect with query parameters. When a user isn't authenticated, I'd like to direct them to login, and once they are logged in send them back to where they tried to access.

const redirectUnauthorizedToLogin = (route: ActivatedRouteSnapshot) => {
  const path = route.pathFromRoot.map(v => v.url.map(segment => segment.toString()).join('/')).join('/');
  const params = String(new URLSearchParams(route.queryParams));

  // const url = '/?' + String(new URLSearchParams({redirect: path + '?' + params}));

  return redirectUnauthorizedTo(['/']);
};

But I can't redirect to this url as its not a valid segment, and redirectUnauthorizedTo doesn't support query parameters

jamesdaniels commented 4 years ago

Would allowing you to return your own UrlTree be sufficient?

AmitMY commented 4 years ago

I believe it would be, yes.

For now, instead of using urls, i'm using:

const redirectUnauthorizedToLogin = (route: ActivatedRouteSnapshot) => {
  const path = route.pathFromRoot.map(v => v.url.map(segment => segment.toString()).join('/')).join('/');
  const params = route.queryParams;

  return pipe(
    loggedIn,
    tap((isLoggedIn) => {
      if (!isLoggedIn) {
        console.log('Saving afterLogin');
        sessionStorage.setItem('afterLogin', JSON.stringify({path, params}));
      }
    }),
    map(loggedIn => loggedIn || ['/'])
  );
};
EdricChan03 commented 4 years ago

Isn't this issue a duplicate of #2144?

Anyways, I'm currently attempting to introduce a fix for this issue by allowing the consumer to specify a UrlTree to the pipes.

n0mer commented 3 years ago

@EdricChan03 any luck :) ?