identify

Learn how to identify the current visitor on the server.

This function identifies the current visitor by re-issuing the user token for the given customer.

Call it from a server action with the action’s load context, typically after your own authentication succeeds. The updated token is persisted when the response cookies are written.

Signature

This function has the following signature:

function identify(userId: string, scope: CroctContext): Promise<void>;

Example

Identify a customer in a login action, after your own authentication succeeds:

app/routes/login.tsx
import {redirect} from 'react-router';import {identify} from '@croct/plug-hydrogen/server';import type {Route} from './+types/login';
export async function action({request, context}: Route.ActionArgs) {  const form = await request.formData();
  await identify(String(form.get('customerId')), context);
  return redirect('/account');}

Parameters

The following list describes the supported parameters:

userId
string

The ID of the customer to identify.

scope
CroctContext

The Hydrogen load context for the current request.