The auth() local returns the AuthClerk Icon object of the currently active user.
The currentUser() local returns the Backend UserClerk Icon object of the currently active user. This is helpful if you want to render user information, like their first and last name, directly from the server. Under the hood, currentUser() uses the clerkClientClerk Icon wrapper to make a call to the Backend API. This does count towards the Backend API request rate limit. This also uses fetch() so it is automatically deduped per request.
The following example demonstrates how to protect a page from unauthenticated users and access the current user's information.
.astro component
API Route
src/pages/me.astro
---// Get the userId from auth() -- if null, the user is not signed inconst { userId } =Astro.locals.auth()// Protect the route by checking if the user is signed inif (!userId) {returnAstro.redirect('/login')}// Get the Backend User object when you need access to the user's informationconstuser=awaitAstro.locals.currentUser()---<!-- Use `user` to render user details or create UI elements --><div>Welcome, {user.firstName}!</div>
src/api/me.ts
exportasyncfunctionGET({ locals }) {// Get the userId from auth() -- if null, the user is not signed inconst { userId } =locals.auth()// Protect the route by checking if the user is signed inif (!userId) {returnnewResponse('Unauthorized', { status:401 }) }// Get the Backend User object when you need access to the user's informationconstuser=awaitlocals.currentUser()// Add your Route Handler's logic with the returned `user` objectreturnnewResponse(JSON.stringify(user))}
Clerk provides integrations with a number of popular databases.
To retrieve a token from a JWT template and fetch data from an external source, use the getToken()Clerk Icon method from the auth() local.
src/pages/api/route.ts
exportasyncfunctionGET({ locals }) {// Get the userId and getToken() from auth()const { userId,getToken } =locals.auth()// Protect the route by checking if the user is signed inif (!userId) {returnnewResponse('Unauthorized', { status:401 }) }// Use `getToken()` to get a token from the JWT templateconsttoken=awaitgetToken({ template:'supabase' })// Fetch data from Supabase and return itconstdata= { supabaseData:'Hello World' }returnnewResponse(JSON.stringify(data))}
The following example demonstrates how to use the $authStoreAstro Icon to access the current auth state. It uses userId to detect if the user is signed in.
React
Vue
Svelte
components/external-data.tsx
import { useStore } from'@nanostores/react'import { $authStore } from'@clerk/astro/client'exportdefaultfunctionExternalData() {const { userId } =useStore($authStore)if (userId ===undefined) {// Handle loading state however you likereturn <div>Loading...</div> }if (userId ===null) {// Handle signed out state however you likereturn <div>Sign in to view this page</div> }return <div>...</div>}
components/external-data.vue
<scriptsetup>import { useStore } from'@nanostores/vue'import { $authStore } from'@clerk/astro/client'constauth=useStore($authStore)</script><template> <divv-if="auth.userId === undefined">Loading...</div> <divv-else-if="auth.userId === null">Sign in to view this page</div> <divv-else>...</div></template>
components/external-data.svelte
<script>// The $ prefix is reserved in Svelte for its own reactivity system.// Alias the imports to avoid conflicts.import { $authStore as auth } from'@clerk/astro/client'</script>{#if $auth.userId ===undefined} <div>Loading...</div>{:else if $auth.userId ===null} <div>Sign in to view this page</div>{:else} <div>...</div>{/if}
The following example demonstrates how to use the $userStoreAstro Icon to access the User object. It returns undefined while Clerk is still loading and null if the user is not signed in.
import { useStore } from'@nanostores/react'import { $userStore } from'@clerk/astro/client'exportdefaultfunctionUser() {constuser=useStore($userStore)if (user ===undefined) {// Handle loading state however you likereturnnull }if (user ===null) {return <div>Not signed in</div> }return <div>Hello {user.fullName}!</div>}
user.vue
<scriptsetup>import { useStore } from'@nanostores/vue'import { $userStore } from'@clerk/astro/client'constuser=useStore($userStore)</script><template> <divv-if="user === undefined"><!-- Handle loading state however you like --> </div> <divv-else-if="user === null">Not signed in</div> <divv-else>Hello {{ user.fullName }}!</div></template>
user.svelte
<script>// The $ prefix is reserved in Svelte for its own reactivity system.// Alias the imports to avoid conflicts.import { $userStore as user } from'@clerk/astro/client'</script>{#if $user ===undefined}<!-- Handle loading state however you like -->{:else if $user ===null} <div>Not signed in</div>{:else} <div>Hello {$user.fullName}!</div>{/if}