Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Check multiple condition before login in Using auth guard

I am trying to check multiple condition to let user login. I was able implement authguard when user is logged In successfully. Not only user need validate but also need to meet the criteria to login. Here is How I have implemented auth guard to check if user is Sign In or not.

export class AuthGuard implements CanActivate {
  access: boolean;
  constructor(
    private auth: AuthService,
    private router: Router,
  ) {}
  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ):
    | Observable<boolean | UrlTree>
    | Promise<boolean | UrlTree>
    | boolean
    | UrlTree {
    return this.auth.user$.pipe(
      take(1),
      map((user) => (user ? true : false)),
      tap((isLoggedIn) => {
        if (!isLoggedIn) {
          this.router.navigate(['/login']);
          return false;
        }
        return true;
      })
    );
  }
}

While registration user has a field called employee which is boolean. So I want to implement auth guard to login so that when user login it should meet the condition that the user credentials are valid and the user is employee I tried Following by using following way but empolyee was undefined

this.auth.user$.subscribe((res) => {
  this.isEmployee = res.isEmployee;
});
if (!this.isEmployee) {
  console.log(this.isEmployee);
  this.router.navigate(['/login']);
  console.log('user is not employee');
  return false;
}
console.log('user is approved', this.isEmployee);
return true;
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Assuming you have keys isLoggedIn, isEmployee you can do following:

return this.auth.user$.pipe(
  map((user) => {
    if(user.isLoggedIn && user.isEmployee){
      return this.router.navigate(['login']);
    } else {
      return false;
    }
  })
);

There is no point of using operator take(1) in a guard, user can attempt any number of times to get inside a page.

about 4 years ago · Juan Pablo Isaza Relatório

0

It seems that you are trying to implement authentication and authorization actions at the same time in your auth-guard.

You can implement user credentials and role check on your backend authentication action and avoid complications in your guard.

Or you can visit this https://www.npmjs.com/package/keycloak-angular#authguard and check how both is implemented within single auth-guard.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda