8

isLoggedIn()用 odic-client确定最简洁准确的方法是什么?

就像Angualr2 示例一样,我的第一个方法是:

// return true if user (token) exists.
public isLoggedIn(): Promise<boolean> {
    return this.userManager.getUser().then(user => !!user);
}

然后处理过期的令牌:

// return true if user (token) exists and not expired.
public isLoggedIn(): Promise<boolean> {
    return this.userManager.getUser().then(user => {
        if (!user) {
            return false;
        }
        return !user.expired;
    });
}

我的应用程序的一个要求是,如果 OP 已撤销会话,它不会将用户显示为已登录,因此,按照这里的逻辑,我的下一个方法是:

// return true if user (token) and session (cookie) exists and sub matches and not expired.
public async isLoggedIn(): Promise<boolean> {
    const session = await this.userManager.querySessionStatus().catch(() => null);
    const user = await this.userManager.getUser();
    if (!user || !session) {
        return false;
    }
    if (session.sub !== user.profile.sub) {
        return false;
    }
    return !user.expired;
}

现在我注意到,如果我使用过期的令牌启动我的应用程序,那么在我的isLoggedIn()逻辑之后,静默更新会获得一个带有会话 cookie 的新令牌,所以我有一个误报。有userLoaded事件,但如果令牌仍然有效,则不会触发。

4

0 回答 0