AuthIntermediateSSR-safehooks/auth/useUser.ts

useUser

Current user object.

Import

TypeScript
import { useUser } from '@/hooks';

API

Signature

useUser() → user | null

When to use

Use useUser instead of re-implementing the same pattern in multiple components.

SSR / Next.js

Provider can wrap client layouts; seed isLoading during session hydrate.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useUser
Editor
function Avatar() {
  const user = useUser();
  return <p>{user?.name ?? 'Guest'}</p>;
}
Preview

Implementation source

Full library source for useUser — copy into your project when you are not installing a package yet.

hooks/auth/useUser.ts
import { useAuth } from './useAuth';
import type { AuthUser } from './AuthContext';

/** Current authenticated user (or null). */
export function useUser<TUser extends AuthUser = AuthUser>(): TUser | null {
  return useAuth<TUser>().user;
}

Copy-paste example

useUser.example.tsx
'use client';

import { useUser } from '@/hooks';

export function Avatar() {
  const user = useUser();
  return <p>{user?.name ?? 'Guest'}</p>;
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: Wrap the tree with AuthProvider and pass your session shape — keep it generic.
  • Source of truth: open hooks/auth/useUser.ts for implementation details