#Pass data from Guard to next Component

10 messages · Page 1 of 1 (latest)

dusky gate
#

Hello.

I have this canActivate which checks if the recieved client id through a parameter really returns an existing client or not


canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any {
    const client = route.params.client
    return this.hrAgileService.getClientById(client)
      .pipe(

        map((res: any) => {
          console.log('res', res)
          GlobalSettings.currentClient = res.id
          return !!res.id
        }),

        catchError((err: any) => {
          this.router.navigateByUrl('/home')

          return of(false)

        })

      )

  }```

If it returns true (in this case !!res.id) is there a way to access this id from the component i'm going to?
Right now I'm just saving the id in a static variable in GlobalSettings class to retrieve it right away, but I'd like to avoid that.

Thanks!
hearty stag
dusky gate
#

yeah, I suppose that's the right way to do it

#

thanks!

hearty stag
#

stores are the best solution here but are really hard to understand
well at least i find them

if you choose a store ngxs here is the best of the 2 if you ask me

elfin flame
#

Isn't the id you're checking the same of the client.id you're retrieving, in the positive case?
What's the point of all the GlobalSettings stuff?

pale kettle
#

Cant you just access the activatedRoute.params in the component, just as you are doing in the guard ?

elfin flame
#

That's why I'm asking the reason for

          GlobalSettings.currentClient = res.id

He probably just needs

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot, router: Router): any {
    return this.hrAgileService.getClientById(route.params.client)
      .pipe(catchError((err: any) => this.router.parseUrl('/home'))
  }
#

Supposing his API reply with some kind of error status code when user is not found

#

P.S.
better changing that param name from client to clientId into your path definition.