Difference between revisions of "Impersonation"
From SocialStack
(Created page with "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...") |
|||
| (5 intermediate revisions by one other user not shown) | |||
| Line 4: | Line 4: | ||
= Starting impersonation = | = Starting impersonation = | ||
Send a get request to /v1/user/x/impersonate | 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: | ||
<syntaxhighlight lang="js"> | |||
import webRequest from 'UI/Functions/WebRequest'; | |||
function impersonate(userId){ | |||
return webRequest('user/' + userId + '/impersonate').then(response => { | |||
window.location = '/'; | |||
}); | |||
} | |||
</syntaxhighlight> | |||
= Ending impersonation = | = Ending impersonation = | ||
Send a get request to /v1/user/unpersonate | Send a get request to /v1/user/unpersonate | ||
= Identifying if someone is impersonating = | |||
The frontend can identify if someone is currently impersonating via a session value called realUser: | |||
<syntaxhighlight lang="js"> | |||
const { session, setSession } = useSession(); | |||
var { realUser, user } = session; | |||
// If realUser is set and different to user, they are impersonating | |||
var isImpersonating = realUser && (user.id != realUser.id); | |||
</syntaxhighlight> | |||
Latest revision as of 23:09, 11 January 2023
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';
function impersonate(userId){
return webRequest('user/' + userId + '/impersonate').then(response => {
window.location = '/';
});
}
Ending impersonation
Send a get request to /v1/user/unpersonate
Identifying if someone is impersonating
The frontend can identify if someone is currently impersonating via a session value called realUser:
const { session, setSession } = useSession();
var { realUser, user } = session;
// If realUser is set and different to user, they are impersonating
var isImpersonating = realUser && (user.id != realUser.id);