> For the complete documentation index, see [llms.txt](https://docs.oidc-spa.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.oidc-spa.dev/release-notes/reading-decodedaccesstoken-within-shouldinjectaccesstoken.md).

# Reading decodedAccessToken within shouldInjectAccessToken()

If you are seeing this page this means that you are in a very edge case where you have providerAwaitsInitialization set to false: &#x20;

<pre class="language-typescript"><code class="lang-typescript">@Injectable({ providedIn: 'root' })
export class Oidc extends AbstractOidcService&#x3C;DecodedIdToken> {
<strong>  override providerAwaitsInitialization = false;
</strong>}
</code></pre>

And you are reading the decodedIdToken to decide if the the access token should be used as bearer for a given request, like for example by doing:&#x20;

```typescript
Oidc.createBearerInterceptor({
  shouldInjectAccessToken: (req) => {
    const oidc = inject(Oidc);

    if (req.context.get(INCLUDE_ACCESS_TOKEN_IF_ADMIN)) {
      return oidc.isUserLoggedIn && oidc.$decodedIdToken().realm_access?.roles.includes("admin");
    }

    return false;
  },
})
```

It's fine to do that but due to a technical detail in the API design we can't guarantiy you that the decision will always resolve as it should if the request is made BEFORE oidc.prInitialized has resolved. &#x20;

As a result you must do two things, the first one is to declare that you have read this message and understand the implication. &#x20;

<pre class="language-typescript"><code class="lang-typescript">@Injectable({ providedIn: 'root' })
export class Oidc extends AbstractOidcService&#x3C;DecodedIdToken> {
  override providerAwaitsInitialization = false;
  // see: https://docs.oidc-spa.dev/release-notes/reading-decodedaccesstoken-within-shouldinjectaccesstoken
<strong>  override allowDecodedIdTokenAccessInShouldInjectAccessToken = true;
</strong>}
</code></pre>

Then you must make sure that every requests that depend on this rule are delayed after oidc.prInitialized has resolved, like for example by doing:

```typescript
@Injectable({ providedIn: 'root' })
export class TodoService {
  private readonly http = inject(HttpClient);
  private readonly apiUrl = TODO_API_URL;
  private readonly oidc = inject(Oidc);

  getPublicAndAdminTodos(): Observable<Todo[]> {
    return from(this.oidc.prInitialized).pipe(
      switchMap(() =>
        this.http.get<Todo[]>(`${this.apiUrl}/todos`, {
          params: { _limit: 5 },
          context: new HttpContext().set(INCLUDE_ACCESS_TOKEN_IF_ADMIN, true),
        })
      )
    );
  }
```

Sorry for the inconvegnience.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.oidc-spa.dev/release-notes/reading-decodedaccesstoken-within-shouldinjectaccesstoken.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
