首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >登录保护键斗篷和角

登录保护键斗篷和角
EN

Stack Overflow用户
提问于 2022-11-29 13:13:27
回答 1查看 25关注 0票数 0

在我的项目中,我使用的是密钥披肩,我想实现一个登录保护,以防止通过身份验证的用户导航到公共页面,如登录页和注册页面。我试着像下面这样实现它,但它只是重定向到公共区域。

代码语言:javascript
复制
export class LoginGuard extends KeycloakAuthGuard {
  constructor(protected override readonly router: Router, protected readonly keycloak: KeycloakService) {
    super(router, keycloak);
  }

  async isAccessAllowed(): Promise<boolean | UrlTree> {
    const isLoggedIn = await this.keycloak.isLoggedIn();
    if(isLoggedIn){
      this.router.navigate(['/']);
      return false;
    }
    return true;
  }
}

即使用户经过身份验证,isLoggedIn也始终是假的。

EN

回答 1

Stack Overflow用户

发布于 2022-11-29 14:46:49

将代码放在try/catch块中

代码语言:javascript
复制
async isAccessAllowed(): Promise<boolean|UrlTree> {
  try {
    const isLoggedIn = await this.keycloak.isLoggedIn();
    if(!isLoggedIn) return;
  
    this.router.navigate(['/']);
    return false;
  } catch(error) {

  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74614650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档