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

docs: update example for startInteractiveLogin #1982

Merged
merged 3 commits into from
Mar 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 57 additions & 14 deletions EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
- [Custom routes](#custom-routes)
- [Testing helpers](#testing-helpers)
- [`generateSessionCookie`](#generatesessioncookie)
- [Programmatically starting interactive login](#programmatically-starting-interactive-login)
- [Passing authorization parameters](#passing-authorization-parameters-1)
- [The `returnTo` parameter](#the-returnto-parameter-1)
- [Redirecting the user after authentication](#redirecting-the-user-after-authentication-1)

## Passing authorization parameters

Expand Down Expand Up @@ -60,14 +64,18 @@ The `returnTo` parameter can be appended to the login to specify where you would

For example: `/auth/login?returnTo=/dashboard` would redirect the user to the `/dashboard` route after they have authenticated.

> [!NOTE]
> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**.


### Redirecting the user after logging out

The `returnTo` parameter can be appended to the logout to specify where you would like to redirect the user after they have logged out.

For example: `/auth/login?returnTo=https://example.com/some-page` would redirect the user to the `https://example.com/some-page` URL after they have logged out.

> [!NOTE]
> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**.
> The URL specified as `returnTo` parameters must be registered in your client's **Allowed Logout URLs**.

## Accessing the authenticated user

Expand Down Expand Up @@ -754,31 +762,66 @@ const sessionCookieValue = await generateSessionCookie(
```


## Programmatic Pushed Authentication Requests (PAR)
## Programmatically starting interactive login

The method `startInteractiveLogin` can be called with authorizationParams to initiate an interactive login flow.
The code collects authorization parameters on the server side rather than constructing them directly in the browser.
Additionally to the ability to initialize the interactive login process by redirecting the user to the built-in `auth/login` endpoint,
the `startInteractiveLogin` method can also be called programmatically.

```typescript
// app/api/auth/login/route.ts
import { auth0 } from "./lib/auth0";
import { NextRequest } from "next/server";

export const GET = async (req: NextRequest) => {
// Extract custom parameters from request URL if needed
const searchParams = Object.fromEntries(req.nextUrl.searchParams.entries());
return auth0.startInteractiveLogin();
};
```

### Passing authorization parameters

There are 2 ways to customize the authorization parameters that will be passed to the `/authorize` endpoint when calling `startInteractiveLogin` programmatically. The first option is through static configuration when instantiating the client, like so:

```ts
export const auth0 = new Auth0Client({
authorizationParameters: {
scope: "openid profile email",
audience: "urn:custom:api",
},
});
```

The second option is by configuring `authorizationParams` when calling `startInteractiveLogin`:

```ts
import { auth0 } from "./lib/auth0";
import { NextRequest } from "next/server";

export const GET = async (req: NextRequest) => {
// Call startInteractiveLogin with optional parameters
return auth0.startInteractiveLogin({
// a custom returnTo URL can be specified
returnTo: "/dashboard",
authorizationParameters: {
prompt: searchParams.prompt,
login_hint: searchParams.login_hint,
// Add any custom auth parameters if required
audience: "custom-audience"
scope: "openid profile email",
audience: "urn:custom:api",
}
});
};
```

## The `returnTo` parameter

```
### Redirecting the user after authentication

When calling `startInteractiveLogin`, the `returnTo` parameter can be configured to specify where you would like to redirect the user to after they have completed their authentication and have returned to your application.

```ts
import { auth0 } from "./lib/auth0";
import { NextRequest } from "next/server";

export const GET = async (req: NextRequest) => {
return auth0.startInteractiveLogin({
returnTo: '/dashboard',
});
};
```

> [!NOTE]
> The URLs specified as `returnTo` parameters must be registered in your client's **Allowed Callback URLs**.