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:
'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 logicif (await login(username, password)) {// Identify the userawait 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:
- userIdstring
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.
Conditional requirementThis option is only needed for Page router or API routes, as the current request scope is only accessible through the App router and Server actions.
The property names are aligned with those in Next.js for easy forwarding, as shown in the Page router example.