Unlock seamless workflows and faster delivery with our latest releases- get the details

How to reference a Sanity account user as an "author" for a document.

9 replies
Last updated: Jan 27, 2022
Hi all! I'm looking to reference a Sanity account user as an "author" for a given document. What's the recommended approach for this? Thanks!
Jan 25, 2022, 5:49 PM
Thanks, Rachel, so you first need to manually create a person document for each user you want available, correct?
Jan 25, 2022, 5:56 PM
Correct! I'm putting together a script for you. Hold tight!
Jan 25, 2022, 5:57 PM
Thank you šŸ™‚
Jan 25, 2022, 6:27 PM
Thank you šŸ™‚
Jan 25, 2022, 6:27 PM
Ok, I created the following script. You'll need to adjust the
doc
object to match whatever schema you have set up for your author types, but the rest of it should work for you.
import { studioClient } from "./studioClient"
import cq from 'concurrent-queue'
import userStore from 'part:@sanity/base/user'

// Create a queue to limit the rate at which you write changes to Sanity
let queue = cq().limit({ concurrency: 2 }).process(function (task) {
  return new Promise(function (resolve, reject) {
      setTimeout(resolve.bind(undefined, task), 1000)
  })
})

const createUserDocuments = async () => {
  const allUsers = await studioClient
    .fetch(`*[_id in path('_.groups.*')].members[@ != 'everyone']`)
    .then(res => userStore.getUsers(res))
    .then(users => users.filter(user => user.isCurrentUser))

    for (const user of allUsers){
      const doc = {
        _type: 'person',
        _id: user.id,
        name: user.displayName,
        //other fields you have in your schema
      }
      queue(doc).then(async () => {
        //create the doc via the client
        studioClient.create(doc)
          .then(updatedDoc => {
            console.log(`Hurray, the doc is updated! New document:${updatedDoc._id}`)
          })
          .catch((err) => {
            console.error('Oh no, the update failed: ', err.message)
          })
      })
    }
 
}



createUserDocuments()

// execute this script by running 
// $ sanity exec ./path-to-your-script --withUserToken --mock-browser-env
Jan 25, 2022, 7:02 PM
Oh, I have a reusable
studioClient
configured in my Studio that I import in that script. You can just replace it with:
import client from "part:@sanity/base/client"

const studioClient = client.withConfig({apiVersion: '2021-03-25'})
Jan 25, 2022, 7:03 PM
Awesome! Thank you. I'm trying it out now
Jan 25, 2022, 7:20 PM
Thanks again, Racheal. This was just what I needed. I had to remove the line with
.then(users => users.filter(user => user.isCurrentUser))
to have it load in all the existing users instead of just the current user but it worked great. I even modified it to load in the profile images. I appreciate your help on this!
Jan 27, 2022, 3:53 PM
Awesome, glad it worked for you!
Jan 27, 2022, 5:20 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.

Was this answer helpful?