@archon-inc/sdk
Version:
Integrate easily to our government platform using this SDK. More info on https://archon.inc/sdk
28 lines (27 loc) • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = requestPrivilegedRoleSwitch;
const Archon_js_1 = require("../Archon.js");
/**
* Used to request a role switch to a privileged role. Requires a user to authenticate the request.
* @param user The user to request the role switch for.
* @param newRole The new role to switch to.
* @param callbackUrl The URL to redirect the user to after the request is processed.
* @param reason An optional (but highly recommended) reason for the role switch request.
* Shows to the user when they authenticate the request.
* @returns The session change request with an authentication URL for the user to visit.
*/
async function requestPrivilegedRoleSwitch(user, newRole, callbackUrl, reason) {
const response = await Archon_js_1.Archon.request(`/users/roleSwitchRequest/${typeof user === "string" ? user : user.id}`, "POST", {
roleName: newRole.name,
requestUri: callbackUrl,
reason: reason
});
if (response.status !== 200) {
throw new Error(`Failed to request role switch: ${response.data}`);
}
return {
...response.data,
authUrl: `${Archon_js_1.Archon.getPublicUrl()}/.archon/authenticate/switchRole?nonce=${response.data.nonce}`
};
}