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

Unable to get accessToken in offline mode returning "null"? #997

Open
kavitha57 opened this issue Jul 9, 2024 · 3 comments
Open

Unable to get accessToken in offline mode returning "null"? #997

kavitha57 opened this issue Jul 9, 2024 · 3 comments

Comments

@kavitha57
Copy link

kavitha57 commented Jul 9, 2024

In offline mode, the Access token returns null even added the "offline_access" in the scope

Here the scope

API_ACCESS_SCOPE: [
'offline_access',
'api://xxxxxxxxxxxxxxxxxx/api-access',
],

getAzureApiAccessConfig() {
return {
issuer: ${MS_BASE_API_URL}${TENANT_ID}/v2.0,
clientId: APP_ID,
redirectUrl: REDIRECT_URL,
scopes: API_ACCESS_SCOPE,

  // additionalParameters: {prompt: 'select_account'},
  serviceConfiguration: {
    authorizationEndpoint: `${MS_BASE_API_URL}${TENANT_ID}/oauth2/v2.0/authorize`,
    tokenEndpoint: `${MS_BASE_API_URL}${TENANT_ID}/oauth2/v2.0/token`,
    revocationEndpoint: `${MS_BASE_API_URL}${TENANT_ID}/oauth2/v2.0/logout`,
  },
};

}

async getAccessTokenAsync() {
try {
const userInfo = await this.getUserInfo();

  const expireTime = userInfo.apiAccessTokens
    ? userInfo.apiAccessTokens.expiryTime
    : null;

  console.log('expireTime :>> ', expireTime);

  if (expireTime !== null) {
    // Get expiration time - 5 minutes

    // If it's <= 5 minutes before expiration, then refresh

    const expire = sub(parseISO(expireTime), {minutes: 5});

    const now = new Date();

    console.log(
      'Expire comparison :>> ',

      expire,

      now,

      compareAsc(now, expire),
    );

    if (compareAsc(now, expire) >= 0) {
      // Expired, refresh

      console.log('Refreshing token');

      const refreshToken = userInfo.apiAccessTokens
        ? userInfo.apiAccessTokens.refreshToken
        : null;

      console.log(`Refresh token: ${refreshToken}`);

      const result = await refresh(this.getAzureApiAccessConfig(), {
        refreshToken: refreshToken || '',
      });

      if (!result?.accessToken) {
        await this.logOut();

        return null;
      }

      // Store the new access token, refresh token, and expiration time in storage

      await SECURE_STORAGE.saveData(KEY_AUTH_USER_INFO, {
        ...userInfo,

        apiAccessTokens: {
          accessToken: result.accessToken,

          refreshToken: result.refreshToken ?? '',

          expiryTime: result.accessTokenExpirationDate,
        },
      });

      return result.accessToken;
    }

    // Not expired, just return saved access token

    const accessToken = userInfo.apiAccessTokens
      ? userInfo.apiAccessTokens.accessToken
      : null;

    return accessToken;
  }

  return null;
} catch (error) {
  console.log('error while fetching access token :>> ', error);

  await this.logOut();

  return null;
}

}

please help me what's went wrong?

@carbonrobot
Copy link
Contributor

Azure has specific requirements in order to get the access token. Please see the links in the our documentation here.

@mabdaleem
Copy link

I have the same issue with Azure B2C. I have referred the documentation referenced by @carbonrobot but unfortunately I could not find any info or extra steps related to getting the access token.
This is the config I used :
const config = {
issuer:
'https://mytenant.b2clogin.com/tfp/mytenantID/my_b2c_user_flow_name/v2.0/',
clientId: 'my-APP-ID-FROM-B2C',
redirectUrl: 'my-react-native-app-screen-url',
scopes: ['openid', 'profile', 'offline_access'],
iosPrefersEphemeralSession: true,
};
I need the access token as I need to call the UserInfo endpoint to get claims about the user.
On the same lines, I think it will be useful if this library also provides a method to get user details by calling the Userinfo endpoint of B2C

@mabdaleem
Copy link

mabdaleem commented Oct 8, 2024

I found the required info from @carbonrobot's another comment on similar issue

Get Access Token

It was just adding the Azure App ID to the scope , the new config which returned the access code is below :
const config = {
issuer:
'https://mytenant.b2clogin.com/tfp/mytenantID/my_b2c_user_flow_name/v2.0/',
clientId: 'my-APP-ID-FROM-B2C',
redirectUrl: 'my-react-native-app-screen-url',
scopes: ['my-APP-ID-FROM-B2C', 'openid', 'profile', 'offline_access'],
iosPrefersEphemeralSession: true,
};

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

3 participants