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

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– 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?