fix: initialize auth in RQ client
This commit is contained in:
@@ -1,11 +1,14 @@
|
|||||||
import { ApolloProvider as BaseApolloProvider } from '@apollo/client';
|
import { ApolloProvider as BaseApolloProvider } from '@apollo/client';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
|
||||||
import { mutateAndGetClient } from 'graphql/client';
|
import { mutateAndGetClient } from 'graphql/client';
|
||||||
import useAuthentication from 'hooks/useAuthentication';
|
import useAuthentication from 'hooks/useAuthentication';
|
||||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||||
|
|
||||||
const ApolloProvider = (props) => {
|
const ApolloProvider = (props) => {
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
const authentication = useAuthentication();
|
const authentication = useAuthentication();
|
||||||
|
|
||||||
const onError = React.useCallback(
|
const onError = React.useCallback(
|
||||||
(message) => {
|
(message) => {
|
||||||
enqueueSnackbar(message, {
|
enqueueSnackbar(message, {
|
||||||
@@ -17,12 +20,15 @@ const ApolloProvider = (props) => {
|
|||||||
},
|
},
|
||||||
[enqueueSnackbar],
|
[enqueueSnackbar],
|
||||||
);
|
);
|
||||||
|
|
||||||
const client = React.useMemo(() => {
|
const client = React.useMemo(() => {
|
||||||
return mutateAndGetClient({
|
return mutateAndGetClient({
|
||||||
onError,
|
onError,
|
||||||
token: authentication.token,
|
token: authentication.token,
|
||||||
});
|
});
|
||||||
}, [onError, authentication]);
|
}, [onError, authentication]);
|
||||||
|
|
||||||
return <BaseApolloProvider client={client} {...props} />;
|
return <BaseApolloProvider client={client} {...props} />;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default ApolloProvider;
|
export default ApolloProvider;
|
||||||
|
@@ -33,11 +33,11 @@ export default function AutomatischQueryClientProvider({ children }) {
|
|||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
api.defaults.headers.Authorization = token;
|
api.defaults.headers.Authorization = token;
|
||||||
|
|
||||||
initialize();
|
|
||||||
} else {
|
} else {
|
||||||
delete api.defaults.headers.Authorization;
|
delete api.defaults.headers.Authorization;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initialize();
|
||||||
},
|
},
|
||||||
[initialize, token],
|
[initialize, token],
|
||||||
);
|
);
|
||||||
|
@@ -1,12 +1,18 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { getItem, setItem } from 'helpers/storage';
|
import { getItem, setItem } from 'helpers/storage';
|
||||||
|
|
||||||
export const AuthenticationContext = React.createContext({
|
export const AuthenticationContext = React.createContext({
|
||||||
token: null,
|
token: null,
|
||||||
updateToken: () => void 0,
|
updateToken: () => void 0,
|
||||||
|
isAuthenticated: false,
|
||||||
|
initialize: () => void 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
export const AuthenticationProvider = (props) => {
|
export const AuthenticationProvider = (props) => {
|
||||||
const { children } = props;
|
const { children } = props;
|
||||||
|
const [isInitialized, setInitialized] = React.useState(false);
|
||||||
const [token, setToken] = React.useState(() => getItem('token'));
|
const [token, setToken] = React.useState(() => getItem('token'));
|
||||||
|
|
||||||
const value = React.useMemo(() => {
|
const value = React.useMemo(() => {
|
||||||
return {
|
return {
|
||||||
token,
|
token,
|
||||||
@@ -14,8 +20,13 @@ export const AuthenticationProvider = (props) => {
|
|||||||
setToken(newToken);
|
setToken(newToken);
|
||||||
setItem('token', newToken);
|
setItem('token', newToken);
|
||||||
},
|
},
|
||||||
|
isAuthenticated: Boolean(token) && isInitialized,
|
||||||
|
initialize: () => {
|
||||||
|
setInitialized(true);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}, [token]);
|
}, [token, isInitialized]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthenticationContext.Provider value={value}>
|
<AuthenticationContext.Provider value={value}>
|
||||||
{children}
|
{children}
|
||||||
|
@@ -4,9 +4,5 @@ import { AuthenticationContext } from 'contexts/Authentication';
|
|||||||
export default function useAuthentication() {
|
export default function useAuthentication() {
|
||||||
const authenticationContext = React.useContext(AuthenticationContext);
|
const authenticationContext = React.useContext(AuthenticationContext);
|
||||||
|
|
||||||
return {
|
return authenticationContext;
|
||||||
token: authenticationContext.token,
|
|
||||||
updateToken: authenticationContext.updateToken,
|
|
||||||
isAuthenticated: Boolean(authenticationContext.token),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user