Add utility method mxLoginWithAccessToken to login with existing access token (#7261)

This commit is contained in:
Michael Telatynski 2021-12-02 13:46:44 +00:00 committed by GitHub
parent b69ad0cd0b
commit aa7cae08aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 3 deletions

View file

@ -100,6 +100,7 @@ declare global {
mxOnRecaptchaLoaded?: () => void; mxOnRecaptchaLoaded?: () => void;
electron?: Electron; electron?: Electron;
mxSendSentryReport: (userText: string, issueUrl: string, error: Error) => Promise<void>; mxSendSentryReport: (userText: string, issueUrl: string, error: Error) => Promise<void>;
mxLoginWithAccessToken: (hsUrl: string, accessToken: string) => Promise<void>;
} }
interface DesktopCapturerSource { interface DesktopCapturerSource {

View file

@ -538,8 +538,8 @@ export function hydrateSession(credentials: IMatrixClientCreds): Promise<MatrixC
* fires on_logging_in, optionally clears localstorage, persists new credentials * fires on_logging_in, optionally clears localstorage, persists new credentials
* to localstorage, starts the new client. * to localstorage, starts the new client.
* *
* @param {MatrixClientCreds} credentials * @param {IMatrixClientCreds} credentials
* @param {Boolean} clearStorage * @param {Boolean} clearStorageEnabled
* *
* @returns {Promise} promise which resolves to the new MatrixClient once it has been started * @returns {Promise} promise which resolves to the new MatrixClient once it has been started
*/ */
@ -918,3 +918,17 @@ export function stopMatrixClient(unsetClient = true): void {
} }
} }
} }
// Utility method to perform a login with an existing access_token
window.mxLoginWithAccessToken = async (hsUrl: string, accessToken: string): Promise<void> => {
const tempClient = createClient({
baseUrl: hsUrl,
accessToken,
});
const { user_id: userId } = await tempClient.whoami();
await doSetLoggedIn({
homeserverUrl: hsUrl,
accessToken,
userId,
}, true);
};

View file

@ -40,7 +40,7 @@ import { logger } from "matrix-js-sdk/src/logger";
export interface IMatrixClientCreds { export interface IMatrixClientCreds {
homeserverUrl: string; homeserverUrl: string;
identityServerUrl: string; identityServerUrl?: string;
userId: string; userId: string;
deviceId?: string; deviceId?: string;
accessToken: string; accessToken: string;