getAuth()
The getAuth()
helper retrieves the current user's authentication state from the request object.
Parameters
- Name
request
- Description
The 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
Server data loading
The following example demonstrates how to use getAuth()
to protect a profile page route and load user data.
If the user is authenticated, their userId
is passed to the Backend SDK's getUser()
method to retrieve the user's information.
Server action
Unlike the previous example that loads data when the page loads, the following example uses getAuth()
to only fetch user data after submitting the form. The helper runs on form submission, authenticates the user, and processes the form data.
Feedback
Last updated on