How to get current user information (userid) in Sanity client?
In Sanity Studio, you can access information about the currently logged-in user, including their user ID, email address, and other details. There are two main approaches depending on your Studio version:
For Studio v3 (Current Version)
The Studio exports a built-in hook called useCurrentUser that you can import directly:
import { useCurrentUser } from "sanity"
function MyComponent() {
const currentUser = useCurrentUser()
// Access user properties
console.log(currentUser?.id)
console.log(currentUser?.email)
console.log(currentUser?.name)
return <div>Welcome, {currentUser?.name}</div>
}For Studio v2 (Legacy)
If you're still on Studio v2, you can use the older import path:
import { useCurrentUser } from "@sanity/base/hooks"Or create your own custom hook using the user store:
import userStore from 'part:@sanity/base/user'
import { useEffect, useState } from 'react'
export function useCurrentUser() {
const [user, setUser] = useState({})
useEffect(() => {
userStore.currentUser.subscribe(e => setUser(e.user))
}, [])
return user
}Use Cases
This is particularly useful when you want to:
- Gate or modify custom Studio components based on the logged-in user
- Display user-specific information in custom tools or plugins
- Implement role-based UI customizations
- Track which user made specific changes
The user object typically includes properties like id, email, name, and roles, which you can use to customize the Studio experience based on who's logged in.
You can find more details in the useCurrentUser recipe on Sanity's documentation.
Show original thread3 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.