CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/admin/users/impersonate.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Alert } from "antd";
7
import { join } from "path";
8
9
import { Rendered, useEffect, useState } from "@cocalc/frontend/app-framework";
10
import { Loading } from "@cocalc/frontend/components";
11
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
12
import { webapp_client } from "@cocalc/frontend/webapp-client";
13
14
interface Props {
15
account_id: string;
16
first_name: string;
17
last_name: string;
18
}
19
20
export function Impersonate(props: Readonly<Props>) {
21
const { first_name, last_name, account_id } = props;
22
23
const [auth_token, set_auth_token] = useState<string | null>(null);
24
const [err, set_err] = useState<string | null>(null);
25
26
async function get_token(): Promise<void> {
27
try {
28
const auth_token = await webapp_client.admin_client.get_user_auth_token(
29
account_id,
30
);
31
set_auth_token(auth_token);
32
set_err(null);
33
} catch (err) {
34
set_err(err.toString());
35
set_auth_token(null);
36
}
37
}
38
39
useEffect(() => {
40
get_token();
41
}, []);
42
43
function render_link(): Rendered {
44
if (auth_token == null) {
45
return <Loading />;
46
}
47
// lang_temp: https://github.com/sagemathinc/cocalc/issues/7782
48
const link = join(appBasePath, `auth/impersonate?auth_token=${auth_token}&lang_temp=en`);
49
return (
50
<div>
51
<a href={link} target="_blank" rel="noopener noreferrer">
52
Right click and open this link in a new incognito window, where you
53
will be signed in as {first_name} {last_name}...
54
</a>
55
<br />
56
The actual link:
57
<pre style={{ fontSize: "11pt", textAlign: "center" }}>
58
<a href={link} target="_blank" rel="noopener noreferrer">
59
{link}
60
</a>
61
</pre>
62
</div>
63
);
64
}
65
66
function render_err(): Rendered {
67
if (err != null) {
68
return (
69
<div>
70
<b>ERROR</b> {err}
71
</div>
72
);
73
}
74
}
75
76
return (
77
<Alert
78
type="warning"
79
style={{ margin: "15px" }}
80
message={
81
<b>
82
Impersonate user "{first_name} {last_name}"
83
</b>
84
}
85
description={
86
<>
87
{render_err()}
88
{render_link()}
89
</>
90
}
91
/>
92
);
93
}
94
95