Skip to content

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

Closed

Description

@BinhDuongNguyen

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.

Activity

  1. BinhDuongNguyen commented on Aug 13, 2019

    @BinhDuongNguyen
    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions