getAuth()
The getAuth()
helper retrieves authentication state from the request object.
- Name
req
- Description
The Next.js request object.
- Name
opts?
- Description
An optional object that can be used to configure the behavior of the
getAuth()
function. It accepts the following properties:secretKey?
: A string that represents the Secret Key used to sign the session token. If not provided, the Secret Key is retrieved from the environment variableCLERK_SECRET_KEY
.
Returns
getAuth()
returns the Auth
object.
Usage
Basic usage
The following example demonstrates how to use getAuth()
to retrieve authentication information in an API route.
Protect API routes
It is important to protect your API routes to ensure that only authenticated users can access them. You can do this by checking if the userId
is present in the getAuth()
response, like in the following example:
Usage with getToken()
getAuth()
returns getToken()
, which is a method that returns the current user's session token. You can also use this function to retrieve a custom JWT template, like in the following example:
Usage with clerkClient
clerkClient
is used to access the Backend SDK, which exposes Clerk's Backend API resources. You can use getAuth()
to pass authentication information that many of the Backend SDK methods require, like in the following example:
Feedback
Last updated on