AuthIntermediateSSR-safehooks/auth/useRole.ts

useRole

Role string list.

Import

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

API

Signature

useRole() → string[]

When to use

Use useRole 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 · useRole
Editor
function Roles() {
  const roles = useRole();
  return <pre>{roles.join(', ')}</pre>;
}
Preview

Implementation source

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

hooks/auth/useRole.ts
import { useAuth } from './useAuth';

/** Role list for the current user. */
export function useRole(): string[] {
  return useAuth().roles;
}

Copy-paste example

useRole.example.tsx
'use client';

import { useRole } from '@/hooks';

export function Roles() {
  const roles = useRole();
  return <pre>{roles.join(', ')}</pre>;
}

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/useRole.ts for implementation details