identify

Associate the current session with an identified user.

This method associates the current session with an identified user on the server side, serving as the equivalent of the identify method available on the client side.

You should call this method when a user authenticates, typically at login, to start a new session associated with their profile and history.

Signature

This function has the following signature:

function identify(userId: string, route?: RouteContext): Promise<void>

Example

Here is an example of how to use this function:

Server action example
12345678910111213141516171819202122232425
'use client';
import {login} from '@/app/services';
import {identifyUser} from '@/app/actions';
export function LoginForm() {
const onSubmit = async form => {
const username = form.get('username');
const password = form.get('password');
// Your login logic
if (await login(username, password)) {
// Identify the user
await identifyUser(username);
}
};
return (
<form action={onSubmit}>
<input type="text" name="username" />
<input type="password" name="password" />
<button type="submit">Login</button>
</form>
);
};

Parameters

The following list describes the supported parameters:

userId
string

The ID that uniquely identifies the user in your application.

Although the user ID can be any string, it is recommended to use a string or other globally unique identifier.

route(optional)
object

The context of the current route.

The property names are aligned with those in Next.js for easy forwarding, as shown in the Page router example.

req
NextApiRequest|NextRequest|GetServerSidePropsRequest

The request object.

res
NextApiResponse|NextResponse|GetServerSidePropsResponse

The response object.