Introducing GROQ-powered Webhooks

useCurrentUser Custom React Hook

By Simeon Griggs

Get details of the currently logged in Sanity user

getCurrentUser.js

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
}

Sometimes you want to gate or modify your custom Studio components based on the current logged in user.

This Custom Hook can be dropped into any component to get the user's ID, email address, and more.

Contributor

Simeon Griggs

🇦🇺 in 🇬🇧 Solution Engineer @ Sanity

Simeon is located at Newcastle upon Tyne, UK
Visit Simeon Griggs's profile