Path: blob/master/src/packages/frontend/admin/users/impersonate.tsx
1496 views
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Alert, Card } from "antd";6import { join } from "path";78import { Rendered, useEffect, useState } from "@cocalc/frontend/app-framework";9import { Icon, Loading } from "@cocalc/frontend/components";10import { appBasePath } from "@cocalc/frontend/customize/app-base-path";11import { webapp_client } from "@cocalc/frontend/webapp-client";12import { CopyToClipBoard } from "@cocalc/frontend/components";13import { useLocalizationCtx } from "@cocalc/frontend/app/localize";1415interface Props {16account_id: string;17first_name: string;18last_name: string;19}2021export function Impersonate({ first_name, last_name, account_id }: Props) {22const [auth_token, set_auth_token] = useState<string | null>(null);23const [err, set_err] = useState<string | null>(null);24const [extraWarning, setExtraWarning] = useState<boolean>(false);25const { locale } = useLocalizationCtx();2627async function get_token(): Promise<void> {28try {29const auth_token = await webapp_client.admin_client.get_user_auth_token(30account_id,31);32set_auth_token(auth_token);33set_err(null);34} catch (err) {35set_err(err.toString());36set_auth_token(null);37}38}3940useEffect(() => {41get_token();42}, []);4344function render_link(): Rendered {45if (auth_token == null) {46return <Loading />;47}4849// The lang_temp temporarily sets the interface language of the user to impersonate to the one of the admin50const link = join(51appBasePath,52`auth/impersonate?auth_token=${auth_token}&lang_temp=${locale}`,53);5455const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {56event.preventDefault(); // Prevent left click from opening the link57setExtraWarning(true);58};5960return (61<div>62<div style={{ fontSize: "13pt", textAlign: "center" }}>63<a64href={link}65onClick={handleClick}66target="_blank"67rel="noopener noreferrer"68>69<Icon name="external-link" /> Right click and open this link in a70new <b>Incognito Window</b>, where you will be signed in as "71{first_name} {last_name}"...72</a>73<br />74<br />75or copy the following link and paste it in a different browser:76<br />77<br />78<CopyToClipBoard79before80inputWidth="500px"81value={`${location.origin}${link}`}82/>83</div>84{extraWarning && (85<Alert86showIcon87style={{ margin: "30px auto", maxWidth: "800px" }}88type="warning"89message="Open this link in a new Incognito Window!"90description="Otherwise your current browser session will get overwritten, and potentially sensitive information could leak."91/>92)}93</div>94);95}9697function render_err(): Rendered {98if (err != null) {99return (100<div>101<b>ERROR</b> {err}102</div>103);104}105}106107return (108<Card109title={110<>111Impersonate user "{first_name} {last_name}"112</>113}114>115{render_err()}116{render_link()}117</Card>118);119}120121122