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