Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev

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

1
import { auth } from '@clerk/nextjs';
2
3
export default function Home() {
4
const { userId } = auth();
5
return <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.

1
import { NextResponse } from 'next/server';
2
import { auth } from '@clerk/nextjs';
3
export async function GET() {
4
const {userId, getToken} = auth();
5
if(!userId){
6
return new Response("Unauthorized", { status: 401 });
7
}
8
const token = await getToken({template: "supabase"});
9
// Fetch data from Supabase and return it.
10
const data = { supabaseData: 'Hello World' };
11
return 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.

1
import { auth } from '@clerk/nextjs';
2
3
export default async function Home() {
4
const { orgRole } = auth();
5
return (
6
<>
7
<div>Your curren role is {orgRole}</div>
8
</>
9
)
10
}
11

Available variables

Was this helpful?

Clerk © 2023