Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Using Next Auth to Approve Sanity Comments in Next.js

6 replies
Last updated: Jun 23, 2021
Is Next Auth the right tool to be able to approve Sanity comments in Next.js on the front-end as a specific user? I would like to make it possible to approve or reject comments as the Sanity studio owner from the front-end side.
Jun 23, 2021, 6:27 AM
I think it would be easier to use
next-sanity
and the
createCurrentUserHook
 to check if someone is logged into the project, and then you can set
withCredentials: true
 in the client to have it include the logged in cookie authentication.
Jun 23, 2021, 6:32 AM
user Y

Like so to get the current specific user if logged in, add a logged in cookie.

Where should the
withCredentials: true
go inside the
SanityClient
?
So, to check if user is logged in change the withCredentials to true with useEffect and check if user logged in and withCredentials are true correct?


import userStore from 'part:@sanity/base/user'
import { useEffect, useState } from 'react'

export function useCurrentUser() {
  const [user, setUser] = useState()

  useEffect(() => {
    userStore.currentUser.subscribe(e => setUser(e.user))
  }, [])

  if (!user) {
    return {}
  }

  return user
}
Jun 23, 2021, 6:35 AM
I don't think you can use
userStore
 in the Next app since that's a studio thing.


const config = {
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  apiVersion: '2021-03-25',
  useCdn: process.env.NODE_ENV === 'production',
}

const authConfig = {
  ...,
  withCredentials: true,
  useCdn: false
}

const useCurrentUser = createCurrentUserHook(config)

const handleApprove = async commentId => {
  const result = await client.patch(commentId).set({approved: true})
  return result
}
if (!useCurrentUser.id) {
   return null
}

return (<button onClick={handleApprove}>Approve</button>)
  

Jun 23, 2021, 6:43 AM
I don't think you can use
userStore
 in the Next app since that's a studio thing.


const config = {
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  apiVersion: '2021-03-25',
  useCdn: process.env.NODE_ENV === 'production',
}

const authConfig = {
  ...config,
  withCredentials: true,
  useCdn: false
}

const useCurrentUser = createCurrentUserHook(config)

export function ApproveButton(commentId) {

  const handleApprove = async commentId => {
    const result = await client.config(authConfig).patch(commentId).set({approved: true})
    return result
  }
  if (!useCurrentUser.id) {
     return null
  }

  return (<button onClick={handleApprove}>Approve</button>)
}
Not tested, but something like this
Jun 23, 2021, 6:45 AM
user Y
Thank you will try and test!
Jun 23, 2021, 8:54 AM

Sanity.io – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Categorized in

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing