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. See the Auth
reference for more information.
Usage
Protect API routes
The following example demonstrates how to protect an API route by checking if the userId
is present in the getAuth()
response.
Usage with getToken()
getAuth()
returns getToken()
, which is a method that returns the current user's session token or a custom JWT template.
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 the user's ID.
Feedback
Last updated on