Clerk Astro SDK
The Clerk Astro SDK gives you access to prebuilt components, stores, and helpers to make user authentication easier. Refer to the quickstart guide
Integration
To configure Clerk with Astro, you must pass the clerk() integrationintegrations array in your astro.config.mjs file. See the quickstart
updateClerkOptions()
The updateClerkOptions() function is used to update Clerk's options at runtime. It can be called at any time after Clerk has been initialized
Client-side helpers
The Astro SDK provides stores that give you access to the Clerk
-
$authStore
Astro Icon -
$clerkStore
Astro Icon -
$userStore
Astro Icon -
$signInStore
Astro Icon -
$signUpStore
Astro Icon -
$sessionStore
Astro Icon -
$sessionListStore
Astro Icon -
$organizationStore
Astro Icon
Server-side helpers
The following references show how to integrate Clerk features into your Astro app on the server-side.
Locals
The Astro SDK provides access to Clerk's authentication data through Astro's locals object. The following references show how to access authentication data in server-side code:
clerkMiddleware()
The clerkMiddleware() helper integrates Clerk authentication and authorization into your Astro application through middleware. You can learn more here
clerkClient()
Clerk's JS Backend SDKusers.getUserList() method from the JS Backend SDK instead of manually making a fetch request to the https://api.clerk.com/v1/users endpoint.
To access a resource, you must first instantiate a clerkClient instance. See the reference documentation
Example: Use clerkClient to get a user's information
The following example uses clerkClient to get information about the currently signed-in user. If the user is authenticated, their userId is passed to clerkClient.users.getUser()
import { clerkClient } from '@clerk/astro/server'
export async function GET(context) {
const { isAuthenticated, userId, redirectToSignIn } = context.locals.auth()
if (!isAuthenticated) {
return redirectToSignIn()
}
const user = await clerkClient(context).users.getUser(userId)
return new Response(JSON.stringify({ user }))
}Feedback
Last updated on