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

Feature Request: Add queryParams into return url from custom AuthGuardPipe #2144

Closed
BinhDuongNguyen opened this issue Aug 12, 2019 · 1 comment · Fixed by #2448
Closed

Feature Request: Add queryParams into return url from custom AuthGuardPipe #2144

BinhDuongNguyen opened this issue Aug 12, 2019 · 1 comment · Fixed by #2448

Comments

@BinhDuongNguyen
Copy link

BinhDuongNguyen commented Aug 12, 2019

I'm trying use authGuard to return deep-link for user after success login but it's look like return url from authGuardPipe be encoded, therefore my link break.

my code:

const redirectUnauthorizedUser = (next: ActivatedRouteSnapshot, ) => map(user => {
  if (!user) {
    return ['/guest/login?returnUrl=${encodeURI(next.pathFromRoot
      .map(v => v.url.map(segment => segment.toString()).join('/'))
      .join('/'))}'];
  } else {
    return true;
  }
});

const routes: Routes = [
  {
    path: 'guest',
    loadChildren: () => import('./component/guest/guest.module').then(module => module.GuestModule),
    canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectLoggedInUser }
  },
  {
    path: 'account',
    loadChildren: () => import('./component/account/account.module').then(module => module.AccountModule),
    canActivate: [AngularFireAuthGuard], data: { authGuardPipe: redirectUnauthorizedUser }
  }
];

Request:
1, Is there anyway to add query param into url that return from custom authGuardPipe.
2, It would be great if i can use RouterState in custom pipe for get queryparams in a proper way, not like the way i did above.

Thank you.

UPDATED
bacause AngularFireAuthGuard implements CanActivate therefore i can use routerState like normal CanActivate. But i still not found solution for question 1.

@BinhDuongNguyen
Copy link
Author

I found that the canActivate is passed into createUrlTree like that,

export class AngularFireAuthGuard implements CanActivate {

  constructor(private afAuth: AngularFireAuth, private router: Router) {}

  canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const authPipeFactory: AuthPipeGenerator = next.data.authGuardPipe || (() => loggedIn);
    return this.afAuth.user.pipe(
        take(1),
        authPipeFactory(next, state),
        map(canActivate => typeof canActivate == "boolean" ? canActivate : this.router.createUrlTree(canActivate))
    );
  }

}

but with createUrlTree in this post how i can make queryParams without passing navigationExtras

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
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.

1 participant