Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(redirectUnauthorizedTo): allow passing query parameters #2287

Closed
AmitMY opened this issue Jan 15, 2020 · 4 comments · Fixed by #2448
Closed

feat(redirectUnauthorizedTo): allow passing query parameters #2287

AmitMY opened this issue Jan 15, 2020 · 4 comments · Fixed by #2448

Comments

@AmitMY
Copy link

AmitMY commented Jan 15, 2020

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
Copy link
Member

Would allowing you to return your own UrlTree be sufficient?

@AmitMY
Copy link
Author

AmitMY commented Feb 9, 2020

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
Copy link
Contributor

EdricChan03 commented May 9, 2020

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.

EdricChan03 added a commit to EdricChan03/angularfire that referenced this issue May 9, 2020
EdricChan03 added a commit to EdricChan03/angularfire that referenced this issue May 9, 2020
EdricChan03 added a commit to EdricChan03/angularfire that referenced this issue May 9, 2020
EdricChan03 added a commit to EdricChan03/angularfire that referenced this issue May 23, 2020
EdricChan03 added a commit to EdricChan03/angularfire that referenced this issue Jun 15, 2020
@n0mer
Copy link

n0mer commented Jul 9, 2020

@EdricChan03 any luck :) ?

jamesdaniels added a commit that referenced this issue Nov 11, 2020
#2448)

* feat(auth-guard): add support for specifying a `string` to redirect to

Closes #2287, #2144

* refactor(auth-guard): make ternary easier to read

Co-authored-by: James Daniels <jamesdaniels@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants