> 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/docs/v3/api-reference/vanilla-api/createoidc.md).

# createOidc()

The createOidc() function is a function that assists in creating an OpenID Connect (OIDC) client, which can be utilized in your application for authentication purposes.

### Parameters

`params`: An object containing the following properties:

* `issuerUri: string` The URI of the OpenID Connect issuer.
* `clientId: string` The client ID assigned to your application.
* `transformUrlBeforeRedirect?: (url:string) => string`  (Optional), A function that transforms the string URL before redirection.
* `getExtraQueryParams?: () => Record<string,string>` (Optional), A function that returns extra query parameters.
* `publicUrl?: string`  (Optional), The public URL of your application, useful when your app is not hosted at the origin of the domain.

### Returns

A Promise that resolves to an object of type `Oidc`, which can be either `Oidc.LoggedIn` or `Oidc.NotLoggedIn`.

**`Oidc.LoggedIn`**

Represents a logged-in state.

* `isUserLoggedIn`: `true`.
* `renewTokens: () => Promise<void>` A function to renew tokens.
* `getTokens: () => Tokens`: A function that returns the current tokens.

  ```typescript
  type Tokens = {
          accessToken: string;
          accessTokenExpirationTime: number;
          idToken: string;
          refreshToken: string;
          refreshTokenExpirationTime: number;
      };
  ```
* `subscribeToTokensChange` A function to subscribe to token changes.
  * Params&#x20;
    * `onTokenChange: () => void` A function to execute when token changes
  * Returns
    * `unsubscribe: () => void`
* `logout` A function to log out.
  * Params
    * `redirectTo: "home" | "current page"` to be redirected after logout to home or current page
    * **OR** `redirectTo: "specific url", url: string` to be redirected to specific url
  * Returns
    * `Promise<never>`

**`Oidc.NotLoggedIn`**

Represents a not-logged-in state.

* `isUserLoggedIn`: `false`.
* `login`A function to initiate the login process.
  * Params
    * `doesCurrentHrefRequiresAuth` (boolean): Whether the current href requires authentication.
    * `extraQueryParams` (optional, object): Extra query parameters for login.
  * Returns&#x20;
    * `Promise<never>`This promise never resolves because we are redirected to oidc web service&#x20;

### Example

```typescript
import { createOidc } from 'oidc-spa';

const oidc = await createOidc({
  issuerUri: 'https://your-issuer.com',
  clientId: 'your-client-id',
  transformUrlBeforeRedirect: (url) => `${url}&ui_locales=fr`,
  getExtraQueryParams: () => {kc_idp_hint: "idp1" }/* your extra query params */,
  publicUrl: '/my-app',
});
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.oidc-spa.dev/docs/v3/api-reference/vanilla-api/createoidc.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
