Fix crash chrome on logout (#4027)

This commit is contained in:
Paweł Chyła 2023-07-28 09:33:22 +02:00 committed by GitHub
parent 80ef369f8a
commit 922c9fb4ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
"saleor-dashboard": patch
---
Fix Chrome browser crash when user click logout button

View file

@ -8,6 +8,8 @@ import { useIntl } from "react-intl";
import { useAuthProvider } from "./hooks/useAuthProvider"; import { useAuthProvider } from "./hooks/useAuthProvider";
const originalWindowNavigator = window.navigator;
const adminCredentials = { const adminCredentials = {
email: "admin@example.com", email: "admin@example.com",
password: "admin", password: "admin",
@ -22,6 +24,24 @@ const nonStaffUserCredentials = {
beforeEach(() => { beforeEach(() => {
localStorage.clear(); localStorage.clear();
sessionStorage.clear(); sessionStorage.clear();
Object.defineProperty(window, "navigator", {
configurable: true,
enumerable: true,
value: {
credentials: {
get: jest.fn(),
},
},
});
});
afterAll(() => {
Object.defineProperty(window, "navigator", {
configurable: true,
enumerable: true,
value: originalWindowNavigator,
});
}); });
jest.mock("react-intl", () => ({ jest.mock("react-intl", () => ({

View file

@ -7,6 +7,7 @@ import useLocalStorage from "@dashboard/hooks/useLocalStorage";
import useNavigator from "@dashboard/hooks/useNavigator"; import useNavigator from "@dashboard/hooks/useNavigator";
import { commonMessages } from "@dashboard/intl"; import { commonMessages } from "@dashboard/intl";
import { import {
checkIfCredentialsExist,
isSupported as isCredentialsManagementAPISupported, isSupported as isCredentialsManagementAPISupported,
login as loginWithCredentialsManagementAPI, login as loginWithCredentialsManagementAPI,
saveCredentials, saveCredentials,
@ -109,7 +110,10 @@ export function useAuthProvider({
} as RequestExternalLogoutInput), } as RequestExternalLogoutInput),
}); });
if (isCredentialsManagementAPISupported) { // Clear credentials from browser's credential manager only when exist.
// Chrome 115 crash when calling preventSilentAccess() when no credentials exist.
const hasCredentials = await checkIfCredentialsExist();
if (isCredentialsManagementAPISupported && !!hasCredentials) {
navigator.credentials.preventSilentAccess(); navigator.credentials.preventSilentAccess();
} }

View file

@ -20,6 +20,16 @@ export async function login<T>(
return result!; return result!;
} }
export async function checkIfCredentialsExist() {
const credential = await navigator.credentials.get({ password: true });
if (credential === null) {
return false;
}
return true;
}
export function saveCredentials( export function saveCredentials(
user: UserFragment | UserDetailsFragment, user: UserFragment | UserDetailsFragment,
password: string, password: string,