-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v5-upgrade-guide): Update upgrade guide based for ClerkJS SDKs @v5…
… (#636)
- Loading branch information
1 parent
a27d9f0
commit 12ba797
Showing
2 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
43 changes: 24 additions & 19 deletions
43
docs/references/backend/sessions/authenticate-request.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,60 @@ | ||
--- | ||
title: authenticateRequest() | ||
description: Networkless method to verify a token passed from the frontend. | ||
description: Use Clerk's Backend SDK to verify a token passed from the frontend. | ||
--- | ||
|
||
# `authenticateRequest()` | ||
|
||
Authenticates a token passed from the frontend. Networkless if the `secretKey` or `jwtKey` are provided. Otherwise, performs a network call to retrieve the JWKS from Clerk's Backend API. | ||
|
||
```tsx | ||
const authStatus = await clerkClient.authenticateRequest(); | ||
function authenticateRequest: (request: Request, options: AuthenticateRequestOptions) => Promise<RequestState>; | ||
``` | ||
|
||
## `AuthenticateRequestParams` | ||
## `authenticateRequest()` parameters | ||
|
||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| `request` | `Request` | `Request` object | | ||
| `options?` | [`AuthenticateRequestOptions`](#authenticate-request-options) | Optional options to configure the authentication. | | ||
|
||
## `AuthenticateRequestOptions` | ||
|
||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| `request` | `Request` | `req` object | | ||
| `secretKey?` | `string` | The Clerk secret key from the [API Keys](https://dashboard.clerk.com/last-active?path=api-keys) page in the Clerk Dashboard. | | ||
| `publishableKey?` | `string` | The Clerk publishable key from the [API Keys](https://dashboard.clerk.com/last-active?path=api-keys) page in the Clerk Dashboard. | | ||
| `domain?` | `string` | The domain for the application. For development, you can pass the localhost your application is running on. For example: `localhost:3001` | | ||
| `isSatellite?` | `boolean` | Set to `true` if the instance is a satellite domain in a multi-domain setup. | | ||
| `proxyUrl?` | `string` | The proxy URL from a multi-domain setup. | | ||
| `signInUrl?` | `string` | The sign-in URL from a multi-domain setup. | | ||
| `signInUrl?` | `string` | The sign-in URL from a multi-domain setup. | | ||
| `afterSignInUrl?` | `string` | The URL to navigate after sign-in completion. Defaults to `/`. | | ||
| `signUpUrl?` | `string` | The sign-up URL from a multi-domain setup. | | ||
| `afterSignUpUrl?` | `string` | The URL to navigate after sign-up completion. Defaults to `/`. | | ||
| `jwtKey?` | `string` | The PEM public key from the **[API Keys](https://dashboard.clerk.com/last-active?path=api-keys) page -> Advanced -> JWT public key** section of the Clerk Dashboard. | | ||
| `audience?` | `string \| string[]` | A string or list of [audiences](https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3). | | ||
| `frontendApi?` | `string` | The Clerk publishable key (deprecated in favor of `publishableKey`) | | ||
| `apiKey?` | `string` | The Clerk API key (deprecated in favor of `secretKey`) | | ||
|
||
## Examples | ||
| `authorizedParties`| `string[]` | | A list of authorized parties. For example, `['http://localhost:4003', 'https://clerk.com']` | | ||
| `clockSkewInMs?` | `number` | Specifies the allowed time difference (in milliseconds) between the Clerk server (which generates the token) and the clock of the user's application server when validating a token. Defaults to 5000 ms (5 seconds).| | ||
| `jwksCacheTtlInMs?` | `number` | Specifies the allowed time (in milliseconds) the JWKs are considered valid in cache . Defaults to 3600_000 ms (1 hour).| | ||
| `skipJwksCache?` | `boolean` | A flag to skip ignore cache and always fetch JWKs before each jwt verification.| | ||
|
||
### `authenticateRequest({ req })` | ||
## `authenticateRequest()` example | ||
|
||
Takes the token passed by the frontend as a Bearer token in the Authorization header, and performs a networkless authenication. This will verify if the user is signed into the application or not. | ||
|
||
|
||
```tsx | ||
import { clerkClient } from '@clerk/nextjs' | ||
import { clerkClient } from '@clerk/nextjs/server' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET(req: NextRequest) { | ||
const { isSignedIn } = await clerkClient.authenticateRequest({ request: req }) | ||
const { isSignedIn } = await clerkClient.authenticateRequest(req) | ||
|
||
if ( !isSignedIn ) { | ||
return NextResponse.json({ status: 401 }) | ||
} | ||
|
||
// Perform protected actions | ||
|
||
return NextResponse.json({ message: "This is a reply" }, status: { 200 }) | ||
return NextResponse.json({ message: "This is a reply" }) | ||
} | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters