Authentication and user registration with Sanity - resources?

12 replies
Last updated: Jun 5, 2023
Can I do authentication with Sanity? Where users can register and login? If there are any resources on this please point me to them. I have scoured the documentation, but can't seem to get it.
AI Update

Sanity itself does not provide end-user authentication for your application's users to register and login. This is an important distinction that often confuses people when first exploring Sanity.

What Sanity Authentication Is For

Sanity's authentication system is designed for content creators and editors who need to access Sanity Studio or manage content. The authentication features you'll find in the docs (like API tokens, SAML SSO, and the deprecated third-party login) are all about managing who can access your Sanity project and Studio - not your website visitors.

For Your Application's End Users

If you need authentication for your website or app visitors (like a membership site, user profiles, or gated content), you'll need to use a separate authentication service. Here's the recommended approach:

Use a Third-Party Auth Provider

Integrate an authentication service like:

  • Auth0
  • Firebase Authentication
  • Clerk
  • Supabase Auth
  • NextAuth.js (for Next.js apps)
  • Netlify Identity

These services handle user registration, login, password resets, and session management for your end users.

Connecting Auth to Sanity Content

Once you have authentication set up, you can:

  1. Store user-specific data in Sanity: After a user authenticates with your auth provider, you can create or update documents in Sanity with their information using a server-side API call with a write token

  2. Restrict content based on user status: In your frontend, check the user's authentication status before displaying certain content fetched from Sanity

  3. User-generated content: Use Sanity Functions (serverless compute within Sanity) or serverless endpoints to accept authenticated user submissions and write them to Sanity. Functions are the modern, recommended approach for handling this kind of automation.

Example Pattern

Here's a common pattern:

// Your frontend (after user logs in with Auth0/Firebase/etc)
const userToken = await authProvider.getToken()

// Call your backend/serverless function or Sanity Function
await fetch('/api/create-user-content', {
  method: 'POST',
  headers: { 'Authorization': `Bearer ${userToken}` },
  body: JSON.stringify({ content: 'user data' })
})

// Your backend validates the user token and writes to Sanity
// using a Sanity API token with write permissions

Resources

While there isn't a specific Sanity guide for end-user authentication (because it's outside Sanity's scope), you might find these resources helpful:

The Bottom Line

Sanity is not an auth provider - it's a content platform. You'll need to pair it with a dedicated authentication service for your end users. This separation of concerns is actually a strength of the composable/headless CMS approach, as you can choose the best authentication solution for your specific needs while using Sanity to manage and deliver your content.

Show original thread
12 replies
Are you hoping to for these users to become project member or just use the Content Lake as a database?
If it’s the former, while you can have unlimited users in a project, they would all have Admin access. This wouldn’t be a great idea.

You could use Sanity as a database for your users but there’s nothing built in to manage that for you. That would be something that you would have to create.
Ooooh
I would love to use the Content Lake as database
So I thought I could also use Sanity for the users' database altogether.
Yeah, totally possible. It would require you to use the JS client to create the user documents. You’d also need to make sure you salt and hash any passwords you store and use something like Passport.js.
Okay!Great! Thanks!
But could I use Authentication providers with this too? Like GoogleAuth?
Yeah, any auth provider that you can specify the database for would work.
Awesome!
That's very helpful
Thanks
You’re welcome!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?