From 922c9fb4cef9d09d771c2319c2bdb42595ca1e36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chy=C5=82a?= Date: Fri, 28 Jul 2023 09:33:22 +0200 Subject: [PATCH] Fix crash chrome on logout (#4027) --- .changeset/stale-kings-compare.md | 5 +++++ src/auth/AuthProvider.test.tsx | 20 ++++++++++++++++++++ src/auth/hooks/useAuthProvider.ts | 6 +++++- src/utils/credentialsManagement.ts | 10 ++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 .changeset/stale-kings-compare.md diff --git a/.changeset/stale-kings-compare.md b/.changeset/stale-kings-compare.md new file mode 100644 index 000000000..2ef690927 --- /dev/null +++ b/.changeset/stale-kings-compare.md @@ -0,0 +1,5 @@ +--- +"saleor-dashboard": patch +--- + +Fix Chrome browser crash when user click logout button diff --git a/src/auth/AuthProvider.test.tsx b/src/auth/AuthProvider.test.tsx index f7c047ffe..d5c96db86 100644 --- a/src/auth/AuthProvider.test.tsx +++ b/src/auth/AuthProvider.test.tsx @@ -8,6 +8,8 @@ import { useIntl } from "react-intl"; import { useAuthProvider } from "./hooks/useAuthProvider"; +const originalWindowNavigator = window.navigator; + const adminCredentials = { email: "admin@example.com", password: "admin", @@ -22,6 +24,24 @@ const nonStaffUserCredentials = { beforeEach(() => { localStorage.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", () => ({ diff --git a/src/auth/hooks/useAuthProvider.ts b/src/auth/hooks/useAuthProvider.ts index fd442bdb7..83755242f 100644 --- a/src/auth/hooks/useAuthProvider.ts +++ b/src/auth/hooks/useAuthProvider.ts @@ -7,6 +7,7 @@ import useLocalStorage from "@dashboard/hooks/useLocalStorage"; import useNavigator from "@dashboard/hooks/useNavigator"; import { commonMessages } from "@dashboard/intl"; import { + checkIfCredentialsExist, isSupported as isCredentialsManagementAPISupported, login as loginWithCredentialsManagementAPI, saveCredentials, @@ -109,7 +110,10 @@ export function useAuthProvider({ } 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(); } diff --git a/src/utils/credentialsManagement.ts b/src/utils/credentialsManagement.ts index 88151fe8c..7d00d79ed 100644 --- a/src/utils/credentialsManagement.ts +++ b/src/utils/credentialsManagement.ts @@ -20,6 +20,16 @@ export async function login( return result!; } +export async function checkIfCredentialsExist() { + const credential = await navigator.credentials.get({ password: true }); + + if (credential === null) { + return false; + } + + return true; +} + export function saveCredentials( user: UserFragment | UserDetailsFragment, password: string,