Structured Content 2022: Join our conference to explore fresh perspectives on content and digital experiences β†’

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

Other schemas by author