anonymize
Learn how to anonymize an identified user using a standalone function.
This method dissociates the current session from the identified user, serving as the equivalent of the anonymize method available on the client side.
You should call this method when a user logs out to start a new anonymous session.
To keep the Croct token in sync with your auth system between requests, pair this with the userIdResolver option in the middleware.
Signature
This function has the following signature:
function anonymize(route?: RouteContext): Promise<void>Example
Here is an example of how to use this function:
'use client';
import {logout} from '@/app/services';import {anonymizeUser} from '@/app/actions';
export function LogoutButton() { const onClick = async () => { // Your logout logic await logout(); // Anonymize the user await anonymizeUser(); };
return (<button onClick={onClick}>Logout</button>);}Parameters
The following list describes the supported parameters:
- 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.