Skip to content

Commit

Permalink
frontend/admin: set the user UI lang temporarily to the one of the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldschilly committed Jan 7, 2025
1 parent 483c13c commit d4fcb1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/packages/frontend/admin/users/impersonate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import { Alert, Card } from "antd";
import { join } from "path";

import { Rendered, useEffect, useState } from "@cocalc/frontend/app-framework";
import { Icon, Loading } from "@cocalc/frontend/components";
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
import { webapp_client } from "@cocalc/frontend/webapp-client";
import { CopyToClipBoard } from "@cocalc/frontend/components";
import { useLocalizationCtx } from "@cocalc/frontend/app/localize";

interface Props {
account_id: string;
Expand All @@ -21,11 +23,13 @@ export function Impersonate({ first_name, last_name, account_id }: Props) {
const [auth_token, set_auth_token] = useState<string | null>(null);
const [err, set_err] = useState<string | null>(null);
const [extraWarning, setExtraWarning] = useState<boolean>(false);
const { locale } = useLocalizationCtx();

async function get_token(): Promise<void> {
try {
const auth_token =
await webapp_client.admin_client.get_user_auth_token(account_id);
const auth_token = await webapp_client.admin_client.get_user_auth_token(
account_id,
);
set_auth_token(auth_token);
set_err(null);
} catch (err) {
Expand All @@ -43,9 +47,10 @@ export function Impersonate({ first_name, last_name, account_id }: Props) {
return <Loading />;
}

// The lang_temp temporarily sets the interface language of the user to impersonate to the one of the admin
const link = join(
appBasePath,
`auth/impersonate?auth_token=${auth_token}&lang_temp=en`,
`auth/impersonate?auth_token=${auth_token}&lang_temp=${locale}`,
);

const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
Expand Down
19 changes: 13 additions & 6 deletions src/packages/server/auth/impersonate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* Sign in using an impersonation auth_token. */

import getPool from "@cocalc/database/pool";
import { createRememberMeCookie } from "@cocalc/server/auth/remember-me";
import Cookies from "cookies";

import { REMEMBER_ME_COOKIE_NAME } from "@cocalc/backend/auth/cookie-names";
import clientSideRedirect from "@cocalc/server/auth/client-side-redirect";
import { getServerSettings } from "@cocalc/database/settings/server-settings";
import base_path from "@cocalc/backend/base-path";
import getPool from "@cocalc/database/pool";
import { getServerSettings } from "@cocalc/database/settings/server-settings";
import clientSideRedirect from "@cocalc/server/auth/client-side-redirect";
import { createRememberMeCookie } from "@cocalc/server/auth/remember-me";

export async function signInUsingImpersonateToken({ req, res }) {
try {
Expand All @@ -17,7 +18,7 @@ export async function signInUsingImpersonateToken({ req, res }) {
}

async function doIt({ req, res }) {
const { auth_token } = req.query;
const { auth_token, lang_temp } = req.query;
if (!auth_token) {
throw Error("invalid empty token");
}
Expand All @@ -40,7 +41,13 @@ async function doIt({ req, res }) {
});

const { dns } = await getServerSettings();
const target = `https://${dns}${base_path}`;
let target = `https://${dns}${base_path}/app`;

// if lang_temp is a string, then append it as a query parameter
// This is usally "en" to help admins understanding the UI without changing the users language preferences
if (typeof lang_temp === "string" && lang_temp.trim().length > 0) {
target += `?lang_temp=${encodeURIComponent(lang_temp)}`;
}

clientSideRedirect({ res, target });
}

0 comments on commit d4fcb1e

Please sign in to comment.