Fix switch account login and credentials API wrong login loop issues (#1729)
* Fix switch account login and credentials API wrong login loop issues * Update auth provider hook
This commit is contained in:
parent
0e6ac66fb9
commit
c2ea65dea7
2 changed files with 18 additions and 5 deletions
|
@ -14,7 +14,7 @@ import {
|
||||||
saveCredentials
|
saveCredentials
|
||||||
} from "@saleor/utils/credentialsManagement";
|
} from "@saleor/utils/credentialsManagement";
|
||||||
import ApolloClient from "apollo-client";
|
import ApolloClient from "apollo-client";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { useQuery } from "react-apollo";
|
import { useQuery } from "react-apollo";
|
||||||
import { IntlShape } from "react-intl";
|
import { IntlShape } from "react-intl";
|
||||||
import urlJoin from "url-join";
|
import urlJoin from "url-join";
|
||||||
|
@ -50,6 +50,7 @@ export function useAuthProvider({
|
||||||
const navigate = useNavigator();
|
const navigate = useNavigator();
|
||||||
const { authenticated, authenticating, user } = useAuthState();
|
const { authenticated, authenticating, user } = useAuthState();
|
||||||
const [error, setError] = useState<UserContextError>();
|
const [error, setError] = useState<UserContextError>();
|
||||||
|
const permitCredentialsAPI = useRef(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (authenticating && error) {
|
if (authenticating && error) {
|
||||||
|
@ -58,14 +59,22 @@ export function useAuthProvider({
|
||||||
}, [authenticating]);
|
}, [authenticating]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!authenticated && !authenticating) {
|
if (authenticated) {
|
||||||
|
permitCredentialsAPI.current = true;
|
||||||
|
}
|
||||||
|
}, [authenticated]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!authenticated && !authenticating && permitCredentialsAPI.current) {
|
||||||
|
permitCredentialsAPI.current = false;
|
||||||
loginWithCredentialsManagementAPI(handleLogin);
|
loginWithCredentialsManagementAPI(handleLogin);
|
||||||
}
|
}
|
||||||
}, [authenticated, authenticating]);
|
}, [authenticated, authenticating]);
|
||||||
|
|
||||||
const userDetails = useQuery<UserDetails>(userDetailsQuery, {
|
const userDetails = useQuery<UserDetails>(userDetailsQuery, {
|
||||||
client: apolloClient,
|
client: apolloClient,
|
||||||
skip: !authenticated
|
skip: !authenticated,
|
||||||
|
fetchPolicy: "network-only"
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
|
@ -82,6 +91,10 @@ export function useAuthProvider({
|
||||||
navigator.credentials.preventSilentAccess();
|
navigator.credentials.preventSilentAccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forget last logged in user data.
|
||||||
|
// On next login, user details query will be refetched due to cache-and-network fetch policy.
|
||||||
|
apolloClient.clearStore();
|
||||||
|
|
||||||
const errors = result?.errors || [];
|
const errors = result?.errors || [];
|
||||||
|
|
||||||
const externalLogoutUrl = result
|
const externalLogoutUrl = result
|
||||||
|
|
|
@ -5,14 +5,14 @@ export const isSupported = !!(
|
||||||
);
|
);
|
||||||
|
|
||||||
export async function login<T>(
|
export async function login<T>(
|
||||||
loginFn: (id: string, password: string) => T
|
loginFn: (id: string, password: string) => Promise<T>
|
||||||
): Promise<T | null> {
|
): Promise<T | null> {
|
||||||
let result: T;
|
let result: T;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const credential = await navigator.credentials.get({ password: true });
|
const credential = await navigator.credentials.get({ password: true });
|
||||||
if (credential instanceof PasswordCredential) {
|
if (credential instanceof PasswordCredential) {
|
||||||
result = loginFn(credential.id, credential.password);
|
result = await loginFn(credential.id, credential.password);
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
result = null;
|
result = null;
|
||||||
|
|
Loading…
Reference in a new issue