Use store from react-admin instead of localStorage

By default this is the same, but it can be changed.

Change-Id: Id1b11872b5f7bc83c18f8d74b03ba6401c277da0
This commit is contained in:
Manuel Stahl 2024-02-06 09:19:27 +01:00
parent 8c1546cd5a
commit 83c9704633
2 changed files with 13 additions and 11 deletions

View File

@ -7,6 +7,7 @@ import {
useLogin, useLogin,
useNotify, useNotify,
useLocaleState, useLocaleState,
useStoreContext,
useTranslate, useTranslate,
PasswordInput, PasswordInput,
TextInput, TextInput,
@ -84,11 +85,12 @@ const FormBox = styled(Box)(({ theme }) => ({
const LoginPage = ({ cfg_base_url }) => { const LoginPage = ({ cfg_base_url }) => {
const login = useLogin(); const login = useLogin();
const notify = useNotify(); const notify = useNotify();
const store = useStoreContext();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [supportPassAuth, setSupportPassAuth] = useState(true); const [supportPassAuth, setSupportPassAuth] = useState(true);
const [locale, setLocale] = useLocaleState(); const [locale, setLocale] = useLocaleState();
const translate = useTranslate(); const translate = useTranslate();
const base_url = localStorage.getItem("base_url"); const base_url = store.getItem("base_url");
const [ssoBaseUrl, setSSOBaseUrl] = useState(""); const [ssoBaseUrl, setSSOBaseUrl] = useState("");
const loginToken = /\?loginToken=([a-zA-Z0-9_-]+)/.exec(window.location.href); const loginToken = /\?loginToken=([a-zA-Z0-9_-]+)/.exec(window.location.href);
@ -101,8 +103,8 @@ const LoginPage = ({ cfg_base_url }) => {
"", "",
window.location.href.replace(loginToken[0], "#").split("#")[0] window.location.href.replace(loginToken[0], "#").split("#")[0]
); );
const baseUrl = localStorage.getItem("sso_base_url"); const baseUrl = store.getItem("sso_base_url");
localStorage.removeItem("sso_base_url"); store.removeItem("sso_base_url");
if (baseUrl) { if (baseUrl) {
const auth = { const auth = {
base_url: baseUrl, base_url: baseUrl,
@ -168,7 +170,7 @@ const LoginPage = ({ cfg_base_url }) => {
}; };
const handleSSO = () => { const handleSSO = () => {
localStorage.setItem("sso_base_url", ssoBaseUrl); store.setItem("sso_base_url", ssoBaseUrl);
const ssoFullUrl = `${ssoBaseUrl}/_matrix/client/r0/login/sso/redirect?redirectUrl=${encodeURIComponent( const ssoFullUrl = `${ssoBaseUrl}/_matrix/client/r0/login/sso/redirect?redirectUrl=${encodeURIComponent(
window.location.href window.location.href
)}`; )}`;

View File

@ -1,6 +1,6 @@
import { fetchUtils } from "react-admin"; import { fetchUtils } from "react-admin";
const authProvider = fixed_base_url => ({ const authProvider = (fixed_base_url, store) => ({
// called when the user attempts to log in // called when the user attempts to log in
login: ({ base_url, username, password, loginToken }) => { login: ({ base_url, username, password, loginToken }) => {
// force homeserver for protection in case the form is manipulated // force homeserver for protection in case the form is manipulated
@ -12,7 +12,7 @@ const authProvider = fixed_base_url => ({
body: JSON.stringify( body: JSON.stringify(
Object.assign( Object.assign(
{ {
device_id: localStorage.getItem("device_id"), device_id: store.getItem("device_id"),
initial_device_display_name: "Synapse Admin", initial_device_display_name: "Synapse Admin",
}, },
loginToken loginToken
@ -33,16 +33,16 @@ const authProvider = fixed_base_url => ({
// server, since the admin might want to access the admin API via some // server, since the admin might want to access the admin API via some
// private address // private address
base_url = base_url.replace(/\/+$/g, ""); base_url = base_url.replace(/\/+$/g, "");
localStorage.setItem("base_url", base_url); store.setItem("base_url", base_url);
const decoded_base_url = window.decodeURIComponent(base_url); const decoded_base_url = window.decodeURIComponent(base_url);
const login_api_url = decoded_base_url + "/_matrix/client/r0/login"; const login_api_url = decoded_base_url + "/_matrix/client/r0/login";
return fetchUtils.fetchJson(login_api_url, options).then(({ json }) => { return fetchUtils.fetchJson(login_api_url, options).then(({ json }) => {
localStorage.setItem("home_server", json.home_server); store.setItem("home_server", json.home_server);
localStorage.setItem("user_id", json.user_id); store.setItem("user_id", json.user_id);
localStorage.setItem("access_token", json.access_token); store.setItem("access_token", json.access_token);
localStorage.setItem("device_id", json.device_id); store.setItem("device_id", json.device_id);
}); });
}, },
// called when the user clicks on the logout button // called when the user clicks on the logout button