Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions docs/_partials/auth-object-table.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The `Auth` object is available on the `request` object in server contexts. Some
| Astro | [`locals.auth()`](/docs/reference/astro/locals#locals-auth) |
| Express | [`req.auth`](/docs/reference/express/overview) |
| Fastify | [`getAuth()`](/docs/reference/fastify/get-auth) |
| Hono | [`getAuth()`](/docs/reference/hono/get-auth) |
| Nuxt | [`event.context.auth()`](/docs/reference/nuxt/overview#auth-object) |
| React Router | [`getAuth()`](/docs/reference/react-router/get-auth) |
| TanStack React Start | [`auth()`](/docs/reference/tanstack-react-start/auth) |
Expand Down
303 changes: 165 additions & 138 deletions docs/_partials/authenticate-req.mdx
Original file line number Diff line number Diff line change
@@ -1,214 +1,241 @@
<If notSdk={["nextjs", "astro", "expressjs", "fastify", "nuxt", "react-router", "tanstack-react-start"]}>
<If notSdk={["nextjs", "astro", "expressjs", "fastify", "hono", "nuxt", "react-router", "tanstack-react-start"]}>
```tsx
import { createClerkClient } from '@clerk/backend'

// Initialize clerkClient
const clerkClient = createClerkClient({
secretKey: process.env.CLERK_SECRET_KEY,
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
import { createClerkClient } from '@clerk/backend'

// Initialize clerkClient
const clerkClient = createClerkClient({
secretKey: process.env.CLERK_SECRET_KEY,
publishableKey: process.env.CLERK_PUBLISHABLE_KEY,
})

export async function GET(req: Request) {
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

export async function GET(req: Request) {
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
// Add logic to perform protected actions

// Add logic to perform protected actions

return Response.json({ message: 'This is a reply' })
}
return Response.json({ message: 'This is a reply' })
}
```
</If>

<If sdk="nextjs">
```tsx {{ filename: 'app/api/example/route.ts' }}
import { clerkClient } from '@clerk/nextjs/server'
import { clerkClient } from '@clerk/nextjs/server'

export async function GET(req: Request) {
// Initialize clerkClient
const client = await clerkClient()
export async function GET(req: Request) {
// Initialize clerkClient
const client = await clerkClient()

// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await client.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await client.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

// Add logic to perform protected actions
// Add logic to perform protected actions

return Response.json({ message: 'This is a reply' })
}
return Response.json({ message: 'This is a reply' })
}
```
</If>

<If sdk="astro">
```tsx {{ filename: 'src/api/example.ts' }}
import { clerkClient } from '@clerk/astro/server'
import type { APIRoute } from 'astro'
import { clerkClient } from '@clerk/astro/server'
import type { APIRoute } from 'astro'

export const GET: APIRoute = async (context) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(context).authenticateRequest(context.request, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})
export const GET: APIRoute = async (context) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(context).authenticateRequest(context.request, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

// Add logic to perform protected actions
// Add logic to perform protected actions

return Response.json({ message: 'This is a reply' })
}
return Response.json({ message: 'This is a reply' })
}
```
</If>

<If sdk="expressjs">
```js {{ filename: 'index.js' }}
import { clerkClient } from '@clerk/express'
import express from 'express'
import { clerkClient } from '@clerk/express'
import express from 'express'

const app = express()

app.get('/example', async (req, res) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
res.status(401).json({ error: 'User not authenticated' })
}

// Add logic to perform protected actions

const app = express()
return res.json({ message: 'This is a reply' })
})
```
</If>

app.get('/example', async (req, res) => {
<If sdk="fastify">
```ts {{ filename: 'src/routes/example.ts' }}
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { clerkClient } from '@clerk/fastify'

export const exampleRoutes = (fastify: FastifyInstance) => {
fastify.get('/example', async (req: FastifyRequest, res: FastifyReply) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
res.status(401).json({ error: 'User not authenticated' })
return res.status(401).json({ error: 'User not authenticated' })
}

// Add logic to perform protected actions

return res.json({ message: 'This is a reply' })
})
}
```
</If>

<If sdk="fastify">
```ts {{ filename: 'src/routes/example.ts' }}
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
import { clerkClient } from '@clerk/fastify'

export const exampleRoutes = (fastify: FastifyInstance) => {
fastify.get('/example', async (req: FastifyRequest, res: FastifyReply) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient.authenticateRequest(req, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return res.status(401).json({ error: 'User not authenticated' })
}

// Add logic to perform protected actions

return res.json({ message: 'This is a reply' })
})
<If sdk="hono">
```ts {{ filename: 'src/index.ts' }}
import { clerkMiddleware } from '@clerk/hono'
import { Hono } from 'hono'
import { HTTPException } from 'hono/http-exception'

const app = new Hono()

app.use('*', clerkMiddleware())

app.get('/example', async (c) => {
const clerk = c.get('clerk')

const { isAuthenticated } = await clerk.authenticateRequest(c.req.raw, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

if (!isAuthenticated) {
throw new HTTPException(401, { message: 'User not authenticated' })
}

return c.json({ message: 'This is a reply' })
})
```
</If>

<If sdk="nuxt">
```ts {{ filename: 'server/api/example.ts' }}
import { clerkClient } from '@clerk/nuxt/server'
import { clerkClient } from '@clerk/nuxt/server'

export default defineEventHandler(async (event) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(event).authenticateRequest(event.request, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})
export default defineEventHandler(async (event) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(event).authenticateRequest(event.request, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return createError({ statusCode: 401, statusMessage: 'User not authenticated' })
}
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return createError({ statusCode: 401, statusMessage: 'User not authenticated' })
}

// Add logic to perform protected actions
// Add logic to perform protected actions

return { message: 'This is a reply' }
})
return { message: 'This is a reply' }
})
```
</If>

<If sdk="react-router">
```tsx {{ filename: 'app/routes/example.tsx' }}
import { redirect } from 'react-router'
import { clerkClient } from '@clerk/react-router/server'
import type { Route } from './+types/example'
import { redirect } from 'react-router'
import { clerkClient } from '@clerk/react-router/server'
import type { Route } from './+types/example'

export async function loader(args: Route.LoaderArgs) {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(args).authenticateRequest(args.request, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})
export async function loader(args: Route.LoaderArgs) {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient(args).authenticateRequest(args.request, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return redirect('/sign-in?redirect_url=' + args.request.url)
}
// Protect the route from unauthenticated users
if (!isAuthenticated) {
return redirect('/sign-in?redirect_url=' + args.request.url)
}

// Add logic to perform protected actions
// Add logic to perform protected actions

return { message: 'This is a reply' }
}
return { message: 'This is a reply' }
}
```
</If>

<If sdk="tanstack-react-start">
```tsx {{ filename: 'app/routes/api/example.tsx' }}
import { createFileRoute } from '@tanstack/react-router'
import { clerkClient } from '@clerk/tanstack-react-start/server'

export const ServerRoute = createFileRoute('/api/example')({
server: {
handlers: {
GET: async ({ request }) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient().authenticateRequest(request, {
authorizedParties: ['https://example.com'],
+ jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

// Add logic to perform protected actions

return Response.json({ message: 'This is a reply' })
},
import { createFileRoute } from '@tanstack/react-router'
import { clerkClient } from '@clerk/tanstack-react-start/server'

export const ServerRoute = createFileRoute('/api/example')({
server: {
handlers: {
GET: async ({ request }) => {
// Initialize clerkClient
// Use the `authenticateRequest()` method to verify the token
const { isAuthenticated } = await clerkClient().authenticateRequest(request, {
authorizedParties: ['https://example.com'],
jwtKey: process.env.CLERK_JWT_KEY,
})

// Protect the route from unauthenticated users
if (!isAuthenticated) {
return Response.json({ error: 'Unauthorized' }, { status: 401 })
}

// Add logic to perform protected actions

return Response.json({ message: 'This is a reply' })
},
},
})
},
})
```
</If>
Loading
Loading