Make login and logout in authProvider async

Change-Id: I6bfb1c7a5a3c5a43f9fa622e87d9d487a95a0b6e
This commit is contained in:
Manuel Stahl 2024-04-16 13:16:09 +02:00
parent 33d29e01b1
commit 0b4f3a60c0
2 changed files with 10 additions and 15 deletions

View file

@ -2,7 +2,7 @@ import { fetchUtils } from "react-admin";
const authProvider = { const authProvider = {
// called when the user attempts to log in // called when the user attempts to log in
login: ({ base_url, username, password, loginToken }) => { login: async ({ 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
base_url = process.env.REACT_APP_SERVER || base_url; base_url = process.env.REACT_APP_SERVER || base_url;
@ -38,15 +38,14 @@ const authProvider = {
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 }) => { const { json } = await fetchUtils.fetchJson(login_api_url, options);
localStorage.setItem("home_server", json.home_server); localStorage.setItem("home_server", 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);
});
}, },
// called when the user clicks on the logout button // called when the user clicks on the logout button
logout: () => { logout: async () => {
console.log("logout"); console.log("logout");
const logout_api_url = const logout_api_url =
@ -62,11 +61,9 @@ const authProvider = {
}; };
if (typeof access_token === "string") { if (typeof access_token === "string") {
fetchUtils.fetchJson(logout_api_url, options).then(({ json }) => { await fetchUtils.fetchJson(logout_api_url, options);
localStorage.removeItem("access_token"); localStorage.removeItem("access_token");
});
} }
return Promise.resolve();
}, },
// called when the API returns an error // called when the API returns an error
checkError: ({ status }) => { checkError: ({ status }) => {

View file

@ -1,5 +1,3 @@
import { waitFor } from "@testing-library/react";
import authProvider from "./authProvider"; import authProvider from "./authProvider";
describe("authProvider", () => { describe("authProvider", () => {
@ -93,7 +91,7 @@ describe("authProvider", () => {
method: "POST", method: "POST",
user: { authenticated: true, token: "Bearer foo" }, user: { authenticated: true, token: "Bearer foo" },
}); });
waitFor(() => expect(localStorage.getItem("access_token")).toBeNull()); expect(localStorage.getItem("access_token")).toBeNull();
}); });
}); });