Add user profile to the top menu (#80)

* Add user profile to the top menu

* update readme
This commit is contained in:
Aine 2024-10-22 12:18:55 +03:00 committed by GitHub
parent ca71038874
commit 4f2cd38344
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 49 additions and 6 deletions

View file

@ -69,6 +69,7 @@ The following changes are already implemented:
* [Login with access token](https://github.com/etkecc/synapse-admin/pull/58)
* [Fix footer causing vertical scrollbar](https://github.com/etkecc/synapse-admin/pull/60)
* [Custom Menu Items](https://github.com/etkecc/synapse-admin/pull/79)
* [Add user profile to the top menu](https://github.com/etkecc/synapse-admin/pull/80)
_the list will be updated as new changes are added_

View file

@ -52,7 +52,11 @@ const AdminMenu = (props) => {
useEffect(() => {
const menuConfig = localStorage.getItem('menu');
if (menuConfig) {
setMenu(JSON.parse(menuConfig));
try {
setMenu(JSON.parse(menuConfig));
} catch (e) {
console.error('Error parsing menu configuration', e);
}
}
}, []);

View file

@ -30,7 +30,7 @@ describe("authProvider", () => {
});
expect(ret).toEqual({redirectTo: "/"});
expect(fetch).toHaveBeenCalledWith("http://example.com/_matrix/client/r0/login", {
expect(fetch).toHaveBeenCalledWith("http://example.com/_matrix/client/v3/login", {
body: '{"device_id":null,"initial_device_display_name":"Synapse Admin","type":"m.login.password","identifier":{"type":"m.id.user","user":"@user:example.com"},"password":"secret"}',
headers: new Headers({
Accept: "application/json",
@ -61,7 +61,7 @@ describe("authProvider", () => {
});
expect(ret).toEqual({redirectTo: "/"});
expect(fetch).toHaveBeenCalledWith("https://example.com/_matrix/client/r0/login", {
expect(fetch).toHaveBeenCalledWith("https://example.com/_matrix/client/v3/login", {
body: '{"device_id":null,"initial_device_display_name":"Synapse Admin","type":"m.login.token","token":"login_token"}',
headers: new Headers({
Accept: "application/json",
@ -83,7 +83,7 @@ describe("authProvider", () => {
await authProvider.logout(null);
expect(fetch).toHaveBeenCalledWith("example.com/_matrix/client/r0/logout", {
expect(fetch).toHaveBeenCalledWith("example.com/_matrix/client/v3/logout", {
headers: new Headers({
Accept: "application/json",
Authorization: "Bearer foo",

View file

@ -2,6 +2,7 @@ import { AuthProvider, HttpError, Options, fetchUtils } from "react-admin";
import storage from "../storage";
import { MatrixError, displayError } from "../components/error";
import { fetchAuthenticatedMedia } from "../utils/fetchMedia";
const authProvider: AuthProvider = {
// called when the user attempts to log in
@ -57,7 +58,7 @@ const authProvider: AuthProvider = {
storage.setItem("base_url", base_url);
const decoded_base_url = window.decodeURIComponent(base_url);
let login_api_url = decoded_base_url + (accessToken ? "/_matrix/client/v3/account/whoami" : "/_matrix/client/r0/login");
let login_api_url = decoded_base_url + (accessToken ? "/_matrix/client/v3/account/whoami" : "/_matrix/client/v3/login");
let response;
@ -95,11 +96,48 @@ const authProvider: AuthProvider = {
);
}
},
getIdentity: async () => {
const access_token = storage.getItem("access_token");
const user_id = storage.getItem("user_id");
const base_url = storage.getItem("base_url");
if (typeof access_token !== "string" || typeof user_id !== "string" || typeof base_url !== "string") {
return Promise.reject();
}
const options: Options = {
headers: new Headers({
Accept: "application/json",
Authorization: `Bearer ${access_token}`,
}),
};
const whoami_api_url = base_url + `/_matrix/client/v3/profile/${user_id}`;
try {
let avatar_url = "";
const response = await fetchUtils.fetchJson(whoami_api_url, options);
if (response.json.avatar_url) {
const mediaresp = await fetchAuthenticatedMedia(response.json.avatar_url, "thumbnail");
const blob = await mediaresp.blob();
avatar_url = URL.createObjectURL(blob);
}
return Promise.resolve({
id: user_id,
fullName: response.json.displayname,
avatar: avatar_url,
});
} catch (err) {
console.log("Error getting identity", err);
return Promise.reject();
}
},
// called when the user clicks on the logout button
logout: async () => {
console.log("logout");
const logout_api_url = storage.getItem("base_url") + "/_matrix/client/r0/logout";
const logout_api_url = storage.getItem("base_url") + "/_matrix/client/v3/logout";
const access_token = storage.getItem("access_token");
const options: Options = {