👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Can You Get the Current Login Users Authentication Token Directly?

2 replies
Last updated: Aug 18, 2021
Hi, simple question 💡 Inside a Sanity Studio plugin, can you get the current logged in users Authentication token directly, or do you need a robot Read/Write token to fetch data from the Studio?
I have a plugin which gets content from the studio, and it works with a robot R/W token, but I’d like to not have a robot token for this action, and instead use the signed-in User token
Aug 18, 2021, 8:58 AM
Solution:
Add
credentials: 'include'
in the fetch request of your plugin. That will use the signed in session cookie.
Aug 18, 2021, 9:19 AM
You can also use the built-in client that the studio uses, which is set up to use the authentication:
import client from 'part:@sanity/base/client'

client
  .create({_type: "customDoc", foo: 'bar' })
  .then(doc =>
 console.log(doc))
  .catch(err => console.error(err))
If you’re outside the studio, you can also configure the client to use auth if the user is logged in, that is, use the session token that set on the sanity API:

client.withConfig({ withCredentials: true })
Aug 18, 2021, 9:57 AM

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?