Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Issue with authorization token causing "Session does not match project host" error during GROQ request in Next.js build.

12 replies
Last updated: Jan 12, 2021
Hej,
I’ve made a dataset private and created a token. I worked once, the second time around i’m getting this for my requests:


    body: {
      message: 'Session does not match project host',
      statusCode: 401,
      error: 'Unauthorized'
    },
What does that mean?
Jan 12, 2021, 3:09 PM
(normal http groq request btw)
Jan 12, 2021, 3:10 PM
• creating a new token does not fix it• setting the dataset to public again does not fix it (as long as you’re still sending a token)
Jan 12, 2021, 3:17 PM
(I need auth to access non-root-path-docs )
Jan 12, 2021, 3:18 PM
Hi User. How are you sending the GROQ request? Is the authorization token being sent along?
Jan 12, 2021, 3:30 PM
Hi User,It’s during next.js build using
@sanity/client
.Pretty sure the token is being sent along (works with public and no token).

.env

STUDIO_TOKEN=skLRHxVmhBpirP0…notmyrealtoken…ggdOQdendi
client.js

export const sanityConfig = {
  projectId:
    process.env.NEXT_PUBLIC_STUDIO_PROJECT_ID ||
    process.env.SANITY_STUDIO_API_PROJECT_ID,
  dataset:
    process.env.NEXT_PUBLIC_STUDIO_DATASET ||
    process.env.SANITY_STUDIO_API_DATASET ||
    'production',
  token: process.env.STUDIO_TOKEN,
  useCdn: process.env.NODE_ENV === 'production',
};

const client = sanityClient(sanityConfig);

export default client;
Jan 12, 2021, 3:36 PM
If you open your query in your browser (e.g.,
<https://abc.api.sanity.io/v1/data/query/production?query=*>
), do you get something back (i.e., all the public paths)?
Jan 12, 2021, 3:47 PM
Hi User - have you verified that your build environment variables are correctly set up? If you host on a platform like Netlify or Vercel, these are not normally pulled from
.env
files anymore but require additional config.
On Vercel:
https://vercel.com/docs/environment-variables On Netlify:
https://docs.netlify.com/configure-builds/environment-variables/
Jan 12, 2021, 3:53 PM
Also, just to rule this out, are you sure the token you added belongs to the correct project? Sometimes that exact error message means you've used a different project's token.
Jan 12, 2021, 3:56 PM
faceplam
Jan 12, 2021, 4:17 PM
Gosh darnit 😄I feel so stupid now. You’re exactly right, i just added the token to the wrong project.

🦥

Thx
user M
and
user A
Jan 12, 2021, 4:17 PM
faceplam
Jan 12, 2021, 4:17 PM
Gosh darnit 😄I feel so stupid now. You’re exactly right, i just added the token to the wrong project.

🦥

Thx
user M
and
user A
Jan 12, 2021, 4:17 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?