useAuth()
import { useAuth } from '@auth/clerk'
- useAuth()
Read the current authentication state and perform common auth actions with a universal import path. Proxies to @clerk/nextjs
on web and @clerk/clerk-expo
on mobile.
Usage
AuthGate.tsx
import { useAuth } from '@auth/clerk'
export const AuthGate = ({ children }) => {
const { isLoaded, isSignedIn, userId, getToken, signOut } = useAuth()
if (!isLoaded) return null
if (!isSignedIn) return <SignInScreen />
// Optionally fetch a token for API calls
// const token = await getToken()
return (
<>
{children}
<Button onPress={() => signOut()} title="Sign out" />
</>
)
}
API
Returned values
isLoaded: boolean
— Whether the auth state is hydrated.isSignedIn: boolean
— Whether a user is currently signed in.userId?: string
— The current user’s id when signed in.sessionId?: string
— The current session id when signed in.orgId?: string
— The active organization id, if any.getToken: (opts?) => Promise<string | null>
— Retrieves a JWT for authenticated requests.signOut: (opts?) => Promise<void>
— Signs out the active session.
See official docs for more details and options: https://clerk.com/docs/nextjs/reference/hooks/use-auth