Getting user data in a Sanity query and using a person document type for authors
10 replies
Last updated: Dec 2, 2020
B
Can I somehow get which user has created the document from a query?
Dec 2, 2020, 8:51 AM
M
Yes, you can use below code for the reference through which you can get email id (User object) & you can do whatever stuff you would like to do with it.
import userStore from 'part:@sanity/base/user' // remember to add rxjs/operators to your dependencies with npm or yarn import { map } from 'rxjs/operators' export default () => userStore.currentUser.pipe( map(({user}) => { const {email} = user console.log("Email:", email); }) )
Dec 2, 2020, 9:07 AM
B
I was thinking if I can have that for each document when I'm calling the api on the front-end
Dec 2, 2020, 9:18 AM
M
Okay! So, you are going to use that on front-end. You can add hidden input field or something else in document form & then you can store the user in it with custom publish action or something else. Then, you will get that entire user object on front-end with respect to particular document that you are trying to fetch
Dec 2, 2020, 9:25 AM
M
You have to add the value of last edited by user in that hidden input field
Dec 2, 2020, 9:27 AM
B
Okay, will try that. It would've be nice though if we had that data available in the result from the query. Thanks.
Dec 2, 2020, 12:10 PM
K
Usually people make a
persondocument type and an
authorsfield as an array of references to
person(or just a reference field if you know it's always restricted to one author). That way your content isn't tied to user accounts that forces you to keep users and so on even if they leave the project. Check out the content models for our blogs starters and so for how to set that up.
Dec 2, 2020, 12:33 PM
K
You can add a userId field on a person document to e.g. create some logic to have the author field automatically be populated by the current logged in user
Dec 2, 2020, 12:34 PM
J
I tried that, but in the result of the query I don't see the author field, any idea why?
Dec 2, 2020, 6:52 PM
K
I mean, actually defining schemas like this:•
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/documents/post.js#L42-L51 •
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/objects/authorReference.js •
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/documents/author.js (just add an
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/documents/post.js#L42-L51 •
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/objects/authorReference.js •
https://github.com/sanity-io/sanity-template-gatsby-blog/blob/master/template/studio/schemas/documents/author.js (just add an
idor
author(or
person) document:
import client from 'part:@sanity/base/client' import userStore from 'part:@sanity/base/user' const getAuthorId = async () => { const {id = '', email = '', name = ''} = await userStore.getUser('me') const _id = await client.fetch(`*[ _type == "author" && ( sanityID == $id || email == $email || name match $name ) ][0]._id`, {id, email, name}).catch(error => console.error(error) || null) return _id } export default { title: 'Post', name: 'post', type: 'document', initialValue: async () => { const authorId = await getAuthorId() if (!authorId) { return {} } return { authors: [ { _type: 'reference', _ref: authorId } ] } }, fields: [ /* the post fields */] }
Dec 2, 2020, 7:09 PM
J
Okay, I already have person document but for some of the editors there will not be any data in that entity, and since I only need the email (to send notification for successfully created promotion campaign in AWS Pinpoint) I just need to somehow attach that to the document. Don't want to get too much into how it is implemented 'cos don't see a point why that would metter, I just need the email of the person that has created the document and why the custom input field doesn't take the initial value?
Dec 2, 2020, 7:33 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.