Docs

You are viewing an archived version of the docs.Go to latest version

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.

const authStatus = await clerkClient.authenticateRequest();
  • Name
    request
    Type
    Request
    Description

    req object

  • Name
    secretKey?
    Type
    string
    Description

    The Clerk secret key from the API Keys page in the Clerk Dashboard.

  • Name
    publishableKey?
    Type
    string
    Description

    The Clerk publishable key from the API Keys page in the Clerk Dashboard.

  • Name
    domain?
    Type
    string
    Description

    The domain for the application. For development, you can pass the localhost your application is running on. For example: localhost:3001

  • Name
    isSatellite?
    Type
    boolean
    Description

    Set to true if the instance is a satellite domain in a multi-domain setup.

  • Name
    proxyUrl?
    Type
    string
    Description

    The proxy URL from a multi-domain setup.

  • Name
    signInUrl?
    Type
    string
    Description

    The sign-in URL from a multi-domain setup.

  • Name
    jwtKey?
    Type
    string
    Description

    The PEM public key from the API Keys page -> Advanced -> JWT public key section of the Clerk Dashboard.

  • Name
    audience?
    Type
    string | string[]
    Description

    A string or list of audiences.

  • Name
    frontendApi?
    Type
    string
    Description

    The Clerk publishable key (deprecated in favor of publishableKey)

  • Name
    apiKey?
    Type
    string
    Description

    The Clerk API key (deprecated in favor of secretKey)

Examples

authenticateRequest({ req })

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.

import { clerkClient } from '@clerk/nextjs'
import { NextRequest, NextResponse } from 'next/server'

export async function GET(req: NextRequest) {
  const { isSignedIn } = await clerkClient.authenticateRequest({ request: req })

  if ( !isSignedIn ) {
    return NextResponse.json({ status: 401 })
  }
  
  // Perform protected actions

  return NextResponse.json({ message: "This is a reply" }, status: { 200 })

Feedback

What did you think of this content?

Last updated on