https://blog.devops.dev/user-impersonation-with-ke...
# support-questions-legacy
r
hey @derptacious you can do this. There are a few ways: - You can create a new session for a userId using the createNewSession function from our session recipe. This session will overwrite any current session - so if the user wants to stop the impersonation, then they will have to logout. - Another way is to add some sort of an impersonation ID in the access token payload of the session, and when you fetch the user ID in our APIs (from the session), you can do something like
Copy code
ts
let userId = session.getAccessTokenPayload().impersonationId;
if (userId === undefined) {
   userId = session.getUserId();
}
And then when you want to start an impersonation, you can update the current session like this:
Copy code
ts
await session.mergeIntoAccessTokenPayload({
   impersonationId: "..."
});
And then to stop an impersonation, you can do this:
Copy code
ts
await session.mergeIntoAccessTokenPayload({
   impersonationId: null // this will remove this key from the access token payload
});
2 Views