Instruction file imported from oakfordanalytica/futbolya (
.github/instructions/AppRouter/currentUser.instructions.md). Copyright stays with the author.
[!WARNING] For optimal performance and to avoid rate limiting, it's recommended to use the <SDKLink href="/docs/:sdk:/reference/hooks/use-user" sdks={["chrome-extension","expo","nextjs","react","react-router","tanstack-react-start"]} code={true}>useUser() hook on the client-side when possible. Only use
currentUser()when you specifically need user data in a server context.
The currentUser() helper returns the <SDKLink href="/docs/reference/backend/types/backend-user" sdks={["js-backend"]} code={true}>Backend User object of the currently active user. It can be used in Server Components, Route Handlers, and Server Actions.
Under the hood, this helper:
- calls
fetch(), so it is automatically deduped per request. - uses the
GET /v1/users/{user_id}{{ target: '_blank' }} endpoint. - counts towards the Backend API request rate limit.
[!WARNING] The <SDKLink href="/docs/reference/backend/types/backend-user" sdks={["js-backend"]} code={true}>Backend User object includes a
privateMetadatafield that should not be exposed to the frontend. Avoid passing the full user object returned bycurrentUser()to the frontend. Instead, pass only the specified fields you need.
import { currentUser } from '@clerk/nextjs/server'
export default async function Page() {
const user = await currentUser()
if (!user) return <div>Not signed in</div>
return <div>Hello {user?.firstName}</div>
}