Skip to main content
Docs

Fullstack SDK

A fullstack SDK combines the frontend-only SDKNext.js Icon and backend-only SDKNext.js Icon into one. A fullstack SDK is necessary for frameworks that support multiple rendering strategies (SSR, SSG, etc.), middleware, data fetching, and more. Examples of such frameworks would be Next.js or Rails.

Expected features

Optional features

  • User should be able to enforce authentication on individual routes (e.g. with a requireAuthNext.js Icon helper)
  • Use singleton pattern to only create a pre-configured instance of Clerk backend client

Implementation

See the respective frontend-only SDKNext.js Icon and backend-only SDKNext.js Icon implementation instructions.

In addition to these instructions, you'll need to go through the following steps to support all required features.

Note

If you're looking for a real-world example, have a look at @clerk/nextjs.

Add handshake support

Inside your Clerk middleware, add checks for the headers on the requestState. Apply these headers to the Response and handle any existing location headers (e.g. redirects).

clerk-middleware.ts
import { clerkClient as defaultClerkClient } from './client.ts'

const clerkMiddleware = (options) => {
  return async (context, next) => {
    const clerkClient = options.clerkClient || defaultClerkClient

    const requestState = await clerkClient.authenticateRequest(context.req, {
      authorizedParties: ['https://example.com'],
    })

    if (requestState.headers) {
      // This adds observability headers to the res
      requestState.headers.forEach((value, key) => context.res.headers.append(key, value))

      const locationHeader = requestState.headers.get('location')

      if (locationHeader) {
        return context.redirect(locationHeader, 307)
      } else if (requestState.status === 'handshake') {
        throw new Error('Clerk: unexpected handshake without redirect')
      }
    }

    context.set('clerkAuth', requestState.toAuth())
    context.set('clerk', clerkClient)

    await next()
  }
}

Feedback

What did you think of this content?

Last updated on