From 48bbe613fef5e2bad4525748721b78d98602590f Mon Sep 17 00:00:00 2001 From: Timo K Date: Sun, 24 Sep 2023 19:25:05 +0200 Subject: [PATCH] add url param token and skip login if provided Signed-off-by: Timo K --- src/ClientContext.tsx | 33 +++++++++++++++++++++++++++------ src/UrlParams.ts | 5 +++++ 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ClientContext.tsx b/src/ClientContext.tsx index f7ceea9f..5b9e57e9 100644 --- a/src/ClientContext.tsx +++ b/src/ClientContext.tsx @@ -44,6 +44,7 @@ import { import { translatedError } from "./TranslatedError"; import { useEventTarget } from "./useEvents"; import { Config } from "./config/Config"; +import { useUrlParams } from "./UrlParams"; declare global { interface Window { @@ -152,7 +153,7 @@ interface Props { export const ClientProvider: FC = ({ children }) => { const history = useHistory(); - + const { token, userId, deviceId } = useUrlParams(); // null = signed out, undefined = loading const [initClientState, setInitClientState] = useState< InitResult | null | undefined @@ -165,12 +166,20 @@ export const ClientProvider: FC = ({ children }) => { // the client. if (initializing.current) return; initializing.current = true; + const tokenLogin = + !token && !userId + ? undefined + : ({ + token, + userId, + deviceId, + } as { token: string; userId: string }); - loadClient() + loadClient(tokenLogin) .then(setInitClientState) .catch((err) => logger.error(err)) .finally(() => (initializing.current = false)); - }, []); + }, [token, userId, deviceId]); const changePassword = useCallback( async (password: string) => { @@ -339,7 +348,11 @@ type InitResult = { passwordlessUser: boolean; }; -async function loadClient(): Promise { +async function loadClient(tokenLogin?: { + token: string; + userId: string; + deviceId: string; +}): Promise { if (widget) { // We're inside a widget, so let's engage *matryoshka mode* logger.log("Using a matryoshka client"); @@ -351,7 +364,15 @@ async function loadClient(): Promise { } else { // We're running as a standalone application try { - const session = loadSession(); + let session = loadSession(); + if (tokenLogin) { + session = { + user_id: tokenLogin.userId, + device_id: tokenLogin.deviceId, //"TOKEN_DEVICE", + access_token: tokenLogin.token, + passwordlessUser: false, + }; + } if (!session) { logger.log("No session stored; continuing without a client"); return null; @@ -371,7 +392,7 @@ async function loadClient(): Promise { }; try { - const client = await initClient(initClientParams, true); + const client = await initClient(initClientParams, !tokenLogin); return { client, passwordlessUser, diff --git a/src/UrlParams.ts b/src/UrlParams.ts index 90713c3b..d155fbcf 100644 --- a/src/UrlParams.ts +++ b/src/UrlParams.ts @@ -108,6 +108,10 @@ interface UrlParams { * E2EE password */ password: string | null; + /** + * Token for a user for instance login. This is used for bridge gosts. + */ + token: string | null; } // This is here as a stopgap, but what would be far nicer is a function that @@ -200,6 +204,7 @@ export const getUrlParams = ( fontScale: Number.isNaN(fontScale) ? null : fontScale, analyticsID: parser.getParam("analyticsID"), allowIceFallback: parser.getFlagParam("allowIceFallback"), + token: parser.getParam("token"), }; };