+ {JSON.stringify(loaderData, null, 2)}
+
+ - User has no session, or has a pending session. They either need to sign in, or they need to - complete pending session tasks by selecting or creating an organization. -
- ) - } +export default async function Page() { + // The `Auth` object gives you access to properties like `isAuthenticated` and `userId` + // Accessing the `Auth` object differs depending on the SDK you're using + // https://clerk.com/docs/reference/backend/types/auth-object#how-to-access-the-auth-object + const { isAuthenticated, userId, orgId } = await auth() + if (!isAuthenticated) { return (- User {userId} has a valid session and {orgId} is defined + User has no session, or has a pending session. They either need to sign in, or they need to + complete pending session tasks by selecting or creating an organization.
) } - ``` - By default, users with a `pending` session are treated as signed-out, and their `userId` will not be available. However, in some cases, you may want to access the user's ID even if their session is still `pending`. In these cases, you can set `treatPendingAsSignedOut` to `false`, which will treat `pending` sessions as signed-in and allow you to access the `userId`. + return ( ++ User {userId} has a valid session and {orgId} is defined +
+ ) +} +``` - ```tsx {{ filename: 'app/api/get-teams/route.tsx' }} - import { auth } from '@clerk/nextjs/server' +##### Example: Accessing the `userId` for pending sessions - export const POST = async () => { - // `treatPendingAsSignedOut` is set to `false` to allow access to the `userId` for pending sessions - const { isAuthenticated, userId, has } = await auth({ treatPendingAsSignedOut: false }) +By default, users with a `pending` session are treated as signed-out, and their `userId` will not be available. However, in some cases, you may want to access the user's ID even if their session is still `pending`. In these cases, you can set `treatPendingAsSignedOut` to `false`, which will treat `pending` sessions as signed-in and allow you to access the `userId`. - // Check if the user is signed-out - if (!isAuthenticated) { - return Response.json({ error: 'User is signed-out' }, { status: 401 }) - } +```tsx {{ filename: 'app/api/get-teams/route.tsx' }} +import { auth } from '@clerk/nextjs/server' - // Now the pending user's `userId` can be used for your use case - // This is a basic example of creating a resource using the `userId` - try { - const newResource = await resources.create({ - userId, - }) +export const POST = async () => { + // `treatPendingAsSignedOut` is set to `false` to allow access to the `userId` for pending sessions + // Accessing the `Auth` object differs depending on the SDK you're using + // https://clerk.com/docs/reference/backend/types/auth-object#how-to-access-the-auth-object + const { isAuthenticated, userId, has } = await auth({ treatPendingAsSignedOut: false }) - return Response.json({ data: newResource }, { status: 201 }) - } catch (error) { - return Response.json({ error: 'Failed to create resource' }, { status: 500 }) - } + // Check if the user is signed-out + if (!isAuthenticated) { + return Response.json({ error: 'User is signed-out' }, { status: 401 }) } - ``` -