auth()
Access minimal authentication data for managing sessions and data fetching.
The `auth()` helper is a convenient way to access the current auth state. This helper provides the minimal information needed for data-loading and helper methods to manage the current active session. Below are some examples of usage.
Retrieving userId
1import { auth } from '@clerk/nextjs';23export default function Home() {4const { userId } = auth();5return <div>User Id: {userId}</div>;6}
Data fetching
When using a Clerk integration or you need to send a JWT along to a server you can use the `getToken` function.
1import { NextResponse } from 'next/server';2import { auth } from '@clerk/nextjs';3export async function GET() {4const {userId, getToken} = auth();5if(!userId){6return new Response("Unauthorized", { status: 401 });7}8const token = await getToken({template: "supabase"});9// Fetch data from Supabase and return it.10const data = { supabaseData: 'Hello World' };11return NextResponse.json({ data });12}
Check your current users role
In some cases you need to check your users current organization role before displaying data or allowing certain actions to be performed.
1import { auth } from '@clerk/nextjs';23export default async function Home() {4const { orgRole } = auth();5return (6<>7<div>Your curren role is {orgRole}</div>8</>9)10}11