SimpleAuth
ReactComponents

SignedIn

Render children only when a session exists.

Overview

SignedIn renders children only when useAuth reports a signed-in user. While the session is loading it renders nothing (no built-in skeleton). For loading UI or email-verified branches, use Show.

Preview

Interactive preview requires JavaScript.

Install

npx @simpleauthjs/react add session

Props

NameTypeRequiredDescription
childrenReactNodeYesShown when the user has an active session.

Usage

"use client"

import { SignedIn } from "@/components/simpleauth"

export function UserMenu() {
  return (
    <SignedIn>
      <a href="/account">Account</a>
    </SignedIn>
  )
}

Source

SignedIn is generated with the session feature. The preview below lists the session-related files the CLI writes (including signed-in.tsx, use-auth.ts, and use-current-user.ts).

components/simpleauth/hooks/use-current-user.ts
"use client"

import { useCallback, useEffect, useState } from "react"
import { SimpleAuthError, type MeResponse } from "@simpleauthjs/core"
import { useSimpleAuthClient } from "../provider"

type UseCurrentUserOptions = {
  autoLoad?: boolean
}

function getErrorMessage(error: unknown) {
  if (error instanceof SimpleAuthError) {
    return error.message
  }

  if (error instanceof Error) {
    return error.message
  }

  return "Unable to fetch current user."
}

export function useCurrentUser(options: UseCurrentUserOptions = {}) {
  const { autoLoad = true } = options
  const auth = useSimpleAuthClient()
  const [isLoading, setIsLoading] = useState(Boolean(autoLoad))
  const [error, setError] = useState<string | null>(null)
  const [data, setData] = useState<MeResponse | null>(null)

  const refresh = useCallback(async () => {
    setIsLoading(true)
    setError(null)

    try {
      const response = await auth.me()
      setData(response)
      return response
    } catch (requestError) {
      setData(null)
      setError(getErrorMessage(requestError))
      throw requestError
    } finally {
      setIsLoading(false)
    }
  }, [auth])

  useEffect(() => {
    if (!autoLoad) {
      return
    }

    refresh().catch(() => {})
  }, [autoLoad, refresh])

  return {
    refresh,
    data,
    error,
    isError: Boolean(error),
    isLoading,
  }
}

On this page