Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A proper way to read scope and expiresAt #1285

Open
pavdev64 opened this issue Feb 8, 2024 · 3 comments
Open

A proper way to read scope and expiresAt #1285

pavdev64 opened this issue Feb 8, 2024 · 3 comments

Comments

@pavdev64
Copy link

pavdev64 commented Feb 8, 2024

I'd like to read scope value from access token and value of expiresAt.

First I tried to get accessTokenPayload using useOidcAccessToken() like this

const { accessToken, accessTokenPayload } = useOidcAccessToken();

but accessTokenPayload was undefined all the time while accessToken had a value.

With version 5.x I used

    const tokens = Oidc.get().tokens;
    const scope = (tokens?.accessTokenPayload?.scope ?? '').split(' ');
    const expiresAt = tokens?.expiresAt ?? undefined;

This worked, until version 6.x where Oidc.get() started to throw an exception. And it isn't mentioned the in breaking changes.

I changed code to use OidcClient.get() instead

    const tokens = OidcClient.get().tokens;
    const scope = (tokens?.accessTokenPayload?.scope ?? '').split(' ');
    const expiresAt = tokens?.expiresAt ?? undefined;

but still wondering if this is a correct way if we have hooks and actually why useOidcAccessToken() returns accessTokenPayload with undefined value.

Is this a proper way how to do that?

  • Installed packages:
    "@axa-fr/react-oidc": "7.15.4",
@guillaume-chervet
Copy link
Contributor

hi @pavdev64 , Thank you for you issue.

undefined is not a normal behavior.
Do you have an error in your console?

@guillaume-chervet
Copy link
Contributor

What happen if you set up your configuration on this repository demo @pavdev64 ?

@pavdev64
Copy link
Author

I tested that. The accessTokenPayload is null. It seems to be correct. The scope is actually a property of tokens. However the scope is missing in the tokens type.

export type Tokens = {
    refreshToken: string;
    idTokenPayload:any;
    idToken:string;
    accessTokenPayload:any;
    accessToken:string;
    expiresAt: number;
    issuedAt: number;
};

so I had to retype it to any. This works for me:

    const tokens = OidcClient.get().tokens;
    const scope = ((tokens as any)?.scope ?? '').split(' ');
    const expiresAt = tokens?.expiresAt ?? undefined;

The scope is present here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants