Access the auth state inside your React components.
useAuth hook is a convenient way to access the current auth state. This hook provides the minimal information needed for data-loading and helper methods to manage the current active session.
This hook is compatible with SSR and is recommended for all authentication tasks.
This new hook should be used instead of:
useSession() , to access
getToken() or the
useUser(), to access
useClerk(), to access
In the following basic example,
useAuth is used to access the auth state and generate a new JWT for a custom Supabase integration in a NextJS application. Note that your component must be a descendant of <ClerkProvider/>.
Alternative ways to access the auth object
There are times when you may need to access the auth object during SSR, API handlers and Edge middleware. The auth object is also available in Next.js, Remix, and Gatsby API routes, even during server-side rendering.
The following snippets are quick examples for NextJS applications. For more details, look at the starter guide for your framework of choice.