Accessing currentUser in V3 studio. #3548
-
Inspired by this article. What is the equivalent of this in the v3 Studio? import userStore from "part:@sanity/base/user";
userStore.getCurrentUser().then((user) => {
console.log(`[currentUser]:`, user );
}) Bonus points for a Typescript answer (if that is any different). |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
For Structure Builder specifically, you can do something like this in v3: import {ConfigContext, createConfig} from 'sanity'
import {deskTool, StructureBuilder} from 'sanity/desk'
import {schemaTypes} from './schemas'
export default createConfig({
name: 'default',
title: 'sanity-v3-structure-builder',
projectId: 'your-projectId',
dataset: 'your-dataset',
plugins: [
deskTool({
structure: (S: StructureBuilder, context: ConfigContext) => {
const {projectId, dataset, schema, currentUser, client} = context
const {id, name, email, profileImage, provider, roles} = currentUser
if(!currentUser) {
return S.list().title('User missing')
}
// your logic
return S.defaults()
},
}),
],
schema: {
types: schemaTypes,
},
}) Let us know if this solves your problem or not! |
Beta Was this translation helpful? Give feedback.
-
In part, yes. But we also get the currentUser in a deploy button... const DeployButton = () => {
const [currentUser, setCurrentUser] = useState([]);
useEffect(() => {
async function getCurrentSanityUser() {
try {
const currentUser = await userStore.getCurrentUser()
const roleNames = currentUser.roles.map(role => role.name)
let hasDeloyPermissions = (roleNames.includes('administrator') || roleNames.includes('deploy')) ? true : false
setCurrentUser({
name: currentUser.name,
email: currentUser.email,
hasDeloyPermissions: hasDeloyPermissions,
permissions: roleNames
});
} catch (err) {
console.log(err);
}
}
getCurrentSanityUser();
}, []);
return (
// do stuff with currentUser
)
} |
Beta Was this translation helpful? Give feedback.
-
I understand. This is exactly what I need. // ./structure.ts
import {StructureBuilder} from 'sanity/desk'
import {ConfigContext} from 'sanity'
export const structure = (S: StructureBuilder, context: ConfigContext) => {
const {projectId, dataset, schema, currentUser, client} = context
const {id, name, email, profileImage, provider, roles} = currentUser
console.log(`[currentUser]:`, currentUser);
return S.list().title(currentUser.name).items(S.documentTypeListItems())
}
|
Beta Was this translation helpful? Give feedback.
For Structure Builder specifically, you can do something like this in v3: