本文介绍了Angular上的Keycloak-加载时未获得正确的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最新的angular(11),最新的keycloak-angular和keycloak-js.

Using latest angular (11), latest keycloak-angular and keycloak-js.

在页面加载时,当我初始化密钥斗篷时,我想获取登录状态.即使会话处于活动状态,我也总是假的.

On page load, when I initialize keycloak I want to get loggedin status. I'm getting always false, even if the session is active.

这是我的初始化代码:

import { KeycloakService } from 'keycloak-angular';
import { AppService } from 'src/app/app.service';
import { environment } from '../environments/environment';
 
export function initializer(keycloak: KeycloakService, appService: AppService): () => Promise<any> {
    return (): Promise<any> => {
        return new Promise<void>(async (resolve, reject) => {
          try {
            await keycloak.init({
                config: {
                    url: environment.keycloak.issuer,
                    realm: environment.keycloak.realm,
                    clientId: environment.keycloak.clientId
                },
              loadUserProfileAtStartUp: true,
              initOptions: {
                checkLoginIframe: true
              },
              bearerExcludedUrls: []
            }).then((a) => {
              console.log(a); // this returns always false
            });
            resolve();
          } catch (error) {
            reject(error);
          }
        });
      };
}

即使以下情况,它也会返回false:

This returns false even if:

推荐答案

正如我在评论中所写,我发现我必须具有onLoad属性.我不想拥有 onLoad:'login-required',因为大多数页面不需要用户登录,所以我不得不添加 onLoad:'check-sso'.

As I wrote in my comments, I found that I have to have onLoad property.I did not want to have onLoad: 'login-required', because users are not required to be logged in for most of pages, so I had to add onLoad: 'check-sso'.

这篇关于Angular上的Keycloak-加载时未获得正确的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:36