Difference between revisions of "Impersonation"

From SocialStack
Line 12: Line 12:


function impersonate(userId){
function impersonate(userId){
     // Note: useSession is a React hook. It only works from a React render function.
     // Note: useSession is a React hook. It only works from a React function component.
     // You can instead pass in session and setSession into a separate function.
     // You can instead pass in session and setSession into a separate function.
     var {session, setSession} = useSession();
     var {session, setSession} = useSession();

Revision as of 16:22, 12 November 2021

If you need to switch account, often to debug something, there are two useful endpoints on the API for doing just that. The functionality is only available to admin users and will also refuse any elevation attempts, such as attempting to impersonate a developer account from a regular admin one.


Starting impersonation

Send a get request to /v1/user/x/impersonate where x is the user's ID. This means you can just navigate to it with your browser, or use webRequest like so:

import webRequest from 'UI/Functions/WebRequest';
import {useSession} from 'UI/Session';

function impersonate(userId){
    // Note: useSession is a React hook. It only works from a React function component.
    // You can instead pass in session and setSession into a separate function.
    var {session, setSession} = useSession();
    
    return webRequest('user/' + userId + '/impersonate').then(response => {
        setSession({...session, ...response.json});
    });
}

Ending impersonation

Send a get request to /v1/user/unpersonate