Use proper homeserver URL for API requests
This commit is contained in:
parent
475aa11f06
commit
40b5031550
@ -70,7 +70,7 @@ const LoginPage = ({ theme }) => {
|
||||
var locale = useLocale();
|
||||
const setLocale = useSetLocale();
|
||||
const translate = useTranslate();
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
|
||||
const renderInput = ({
|
||||
meta: { touched, error } = {},
|
||||
|
@ -1,5 +1,20 @@
|
||||
import { fetchUtils } from "react-admin";
|
||||
|
||||
const ensureHttpsForUrl = url => {
|
||||
if (/^https:\/\//i.test(url)) {
|
||||
return url;
|
||||
}
|
||||
const domain = url.replace(/http.?:\/\//g, "");
|
||||
return "https://" + domain;
|
||||
};
|
||||
|
||||
const stripTrailingSlash = str => {
|
||||
if (!str) {
|
||||
return;
|
||||
}
|
||||
return str.endsWith("/") ? str.slice(0, -1) : str;
|
||||
};
|
||||
|
||||
const authProvider = {
|
||||
// called when the user attempts to log in
|
||||
login: ({ homeserver, username, password }) => {
|
||||
@ -13,16 +28,17 @@ const authProvider = {
|
||||
}),
|
||||
};
|
||||
|
||||
// add 'https://' to homeserver url if its missing
|
||||
let newUrl = window.decodeURIComponent(homeserver);
|
||||
newUrl = newUrl.trim().replace(/\s/g, "");
|
||||
if (!/^https?:\/\//i.test(newUrl)) {
|
||||
homeserver = `https://${newUrl}`;
|
||||
}
|
||||
const url = window.decodeURIComponent(homeserver);
|
||||
const trimmed_url = url.trim().replace(/\s/g, "");
|
||||
const login_api_url =
|
||||
ensureHttpsForUrl(trimmed_url) + "/_matrix/client/r0/login";
|
||||
|
||||
const url = homeserver + "/_matrix/client/r0/login";
|
||||
return fetchUtils.fetchJson(url, options).then(({ json }) => {
|
||||
localStorage.setItem("home_server", json.home_server);
|
||||
return fetchUtils.fetchJson(login_api_url, options).then(({ json }) => {
|
||||
const normalized_base_url = stripTrailingSlash(
|
||||
json.well_known["m.homeserver"].base_url
|
||||
);
|
||||
localStorage.setItem("base_url", normalized_base_url);
|
||||
localStorage.setItem("home_server_url", json.home_server);
|
||||
localStorage.setItem("user_id", json.user_id);
|
||||
localStorage.setItem("access_token", json.access_token);
|
||||
localStorage.setItem("device_id", json.device_id);
|
||||
|
@ -63,11 +63,12 @@ const dataProvider = {
|
||||
user_id: user_id,
|
||||
guests: guests,
|
||||
};
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
const url = `${homeserver_url}?${stringify(query)}`;
|
||||
|
||||
return jsonClient(url).then(({ json }) => ({
|
||||
@ -78,11 +79,12 @@ const dataProvider = {
|
||||
|
||||
getOne: (resource, params) => {
|
||||
console.log("getOne " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return jsonClient(`${homeserver_url}/${params.id}`).then(({ json }) => ({
|
||||
data: res.map(json),
|
||||
}));
|
||||
@ -90,11 +92,12 @@ const dataProvider = {
|
||||
|
||||
getMany: (resource, params) => {
|
||||
console.log("getMany " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return Promise.all(
|
||||
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`))
|
||||
).then(responses => ({
|
||||
@ -116,11 +119,12 @@ const dataProvider = {
|
||||
}),
|
||||
};
|
||||
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
const url = `${homeserver_url}?${stringify(query)}`;
|
||||
|
||||
return jsonClient(url).then(({ headers, json }) => ({
|
||||
@ -137,11 +141,12 @@ const dataProvider = {
|
||||
|
||||
update: (resource, params) => {
|
||||
console.log("update " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(params.data, filterNullValues),
|
||||
@ -152,11 +157,12 @@ const dataProvider = {
|
||||
|
||||
updateMany: (resource, params) => {
|
||||
console.log("updateMany " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return Promise.all(
|
||||
params.ids.map(id => jsonClient(`${homeserver_url}/${id}`), {
|
||||
method: "PUT",
|
||||
@ -169,11 +175,12 @@ const dataProvider = {
|
||||
|
||||
create: (resource, params) => {
|
||||
console.log("create " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return jsonClient(`${homeserver_url}/${params.data.id}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(params.data, filterNullValues),
|
||||
@ -184,11 +191,12 @@ const dataProvider = {
|
||||
|
||||
delete: (resource, params) => {
|
||||
console.log("delete " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return jsonClient(`${homeserver_url}/${params.id}`, {
|
||||
method: "DELETE",
|
||||
}).then(({ json }) => ({
|
||||
@ -198,11 +206,12 @@ const dataProvider = {
|
||||
|
||||
deleteMany: (resource, params) => {
|
||||
console.log("deleteMany " + resource);
|
||||
const homeserver = localStorage.getItem("home_server");
|
||||
const homeserver = localStorage.getItem("base_url");
|
||||
if (!homeserver || !(resource in resourceMap)) return Promise.reject();
|
||||
|
||||
const res = resourceMap[resource];
|
||||
const homeserver_url = "https://" + homeserver + res.path;
|
||||
|
||||
const homeserver_url = homeserver + res.path;
|
||||
return Promise.all(
|
||||
params.ids.map(id =>
|
||||
jsonClient(`${homeserver_url}/${id}`, {
|
||||
|
Loading…
Reference in New Issue
Block a user