Sanity Library Reference Docs
    Preparing search index...
    • A React hook that exposes the stored OAuth token state along with refresh and revoke actions.

      Returns UseOAuthTokensResult

      The current UseOAuthTokensResult

      The token view is a synchronous read over core's token state source, so the hook re-renders whenever tokens change — including changes made in another tab, which core propagates via storage events.

      function TokenStatus() {
      const {tokens, isExpired, refresh, revoke} = useOAuthTokens()

      if (!tokens) return <div>Not signed in</div>

      const handleRefresh = async () => {
      if (!isExpired()) return
      try {
      await refresh()
      } catch {
      // Transient failure (tokens unchanged, retry later) or the refresh
      // token was rejected (tokens now null, user is logged out).
      }
      }

      return (
      <div>
      <p>Expires at {tokens.expiresAt.toLocaleTimeString()}</p>
      <button onClick={handleRefresh}>Refresh if expired</button>
      <button onClick={() => revoke()}>Revoke tokens</button>
      </div>
      )
      }