Files
cinny/src/client/action/auth.js
T
Ajay Bura 5a8b82a12e feat: URL navigation in auth (#1603)
* bump to react 18 and install react-router-dom

* Upgrade to react 18 root

* update vite

* add cs api's

* convert state/auth to ts

* add client config context

* add auto discovery context

* add spec version context

* add auth flow context

* add background dot pattern css

* add promise utils

* init url based routing

* update auth route server path as effect

* add auth server hook

* always use server from discovery info in context

* login - WIP

* upgrade jotai to v2

* add atom with localStorage util

* add multi account sessions atom

* add default IGNORE res to auto discovery

* add error type in async callback hook

* handle password login error

* fix async callback hook

* allow password login

* Show custom server not allowed error in mxId login

* add sso login component

* add token login

* fix hardcoded m.login.password in login func

* update server input on url change

* Improve sso login labels

* update folds

* fix async callback batching state update in safari

* wrap async callback set state in queueMicrotask

* wip

* wip - register

* arrange auth file structure

* add error codes

* extract filed error component form password login

* add register util function

* handle register flow - WIP

* update unsupported auth flow method reasons

* improve password input styles

* Improve UIA flow next stage calculation
complete stages can have any order so we will look for first stage which is not in completed

* process register UIA flow stages

* Extract register UIA stages component

* improve register error messages

* add focus trap & step count in UIA stages

* add reset password path and path utils

* add path with origin hook

* fix sso redirect url

* rename register token query param to token

* restyle auth screen header

* add reset password component - WIP

* add reset password form

* add netlify rewrites

* fix netlify file indentation

* test netlify redirect

* fix vite to include netlify toml

* add more netlify redirects

* add splat to public and assets path

* fix vite base name

* add option to use hash router in config and remove appVersion

* add splash screen component

* add client config loading and error screen

* fix server picker bug

* fix reset password email input type

* make auth page small screen responsive

* fix typo in reset password screen
2024-01-21 18:20:56 +05:30

105 lines
3.2 KiB
JavaScript

import * as sdk from 'matrix-js-sdk';
import cons from '../state/cons';
function updateLocalStore(accessToken, deviceId, userId, baseUrl) {
localStorage.setItem(cons.secretKey.ACCESS_TOKEN, accessToken);
localStorage.setItem(cons.secretKey.DEVICE_ID, deviceId);
localStorage.setItem(cons.secretKey.USER_ID, userId);
localStorage.setItem(cons.secretKey.BASE_URL, baseUrl);
}
function createTemporaryClient(baseUrl) {
return sdk.createClient({ baseUrl });
}
async function startSsoLogin(baseUrl, type, idpId) {
const client = createTemporaryClient(baseUrl);
localStorage.setItem(cons.secretKey.BASE_URL, client.baseUrl);
window.location.href = client.getSsoLoginUrl(window.location.href, type, idpId);
}
async function login(baseUrl, username, email, password) {
const identifier = {};
if (username) {
identifier.type = 'm.id.user';
identifier.user = username;
} else if (email) {
identifier.type = 'm.id.thirdparty';
identifier.medium = 'email';
identifier.address = email;
} else throw new Error('Bad Input');
const client = createTemporaryClient(baseUrl);
const res = await client.login('m.login.password', {
identifier,
password,
initial_device_display_name: cons.DEVICE_DISPLAY_NAME,
});
const myBaseUrl = res?.well_known?.['m.homeserver']?.base_url || client.baseUrl;
updateLocalStore(res.access_token, res.device_id, res.user_id, myBaseUrl);
}
async function loginWithToken(baseUrl, token) {
const client = createTemporaryClient(baseUrl);
const res = await client.login('m.login.token', {
token,
initial_device_display_name: cons.DEVICE_DISPLAY_NAME,
});
const myBaseUrl = res?.well_known?.['m.homeserver']?.base_url || client.baseUrl;
updateLocalStore(res.access_token, res.device_id, res.user_id, myBaseUrl);
}
// eslint-disable-next-line camelcase
async function verifyEmail(baseUrl, email, client_secret, send_attempt, next_link) {
const res = await fetch(`${baseUrl}/_matrix/client/r0/register/email/requestToken`, {
method: 'POST',
body: JSON.stringify({
email, client_secret, send_attempt, next_link,
}),
headers: {
'Content-Type': 'application/json; charset=utf-8',
},
credentials: 'same-origin',
});
const data = await res.json();
return data;
}
async function completeRegisterStage(
baseUrl, username, password, auth,
) {
const tempClient = createTemporaryClient(baseUrl);
try {
const result = await tempClient.registerRequest({
username,
password,
auth,
initial_device_display_name: cons.DEVICE_DISPLAY_NAME,
});
const data = { completed: result.completed || [] };
if (result.access_token) {
data.done = true;
updateLocalStore(result.access_token, result.device_id, result.user_id, baseUrl);
}
return data;
} catch (e) {
const result = e.data;
const data = { completed: result.completed || [] };
if (result.access_token) {
data.done = true;
updateLocalStore(result.access_token, result.device_id, result.user_id, baseUrl);
}
return data;
}
}
export {
updateLocalStore, createTemporaryClient, login, verifyEmail,
loginWithToken, startSsoLogin,
completeRegisterStage,
};