Merge pull request #1788 from automatisch/AUT-867
fix: introduce fix for token management
This commit is contained in:
@@ -15,7 +15,7 @@ function AccountDropdownMenu(props) {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { open, onClose, anchorEl, id } = props;
|
const { open, onClose, anchorEl, id } = props;
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
authentication.updateToken('');
|
authentication.removeToken();
|
||||||
await apolloClient.clearStore();
|
await apolloClient.clearStore();
|
||||||
onClose();
|
onClose();
|
||||||
navigate(URLS.LOGIN);
|
navigate(URLS.LOGIN);
|
||||||
|
@@ -22,7 +22,7 @@ function DeleteAccountDialog(props) {
|
|||||||
|
|
||||||
const handleConfirm = React.useCallback(async () => {
|
const handleConfirm = React.useCallback(async () => {
|
||||||
await deleteCurrentUser();
|
await deleteCurrentUser();
|
||||||
authentication.updateToken('');
|
authentication.removeToken();
|
||||||
await apolloClient.clearStore();
|
await apolloClient.clearStore();
|
||||||
navigate(URLS.LOGIN);
|
navigate(URLS.LOGIN);
|
||||||
}, [deleteCurrentUser, currentUser]);
|
}, [deleteCurrentUser, currentUser]);
|
||||||
|
@@ -12,17 +12,20 @@ import { LOGIN } from 'graphql/mutations/login';
|
|||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
import TextField from 'components/TextField';
|
import TextField from 'components/TextField';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
|
||||||
function LoginForm() {
|
function LoginForm() {
|
||||||
const isCloud = useCloud();
|
const isCloud = useCloud();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const authentication = useAuthentication();
|
const authentication = useAuthentication();
|
||||||
const [login, { loading }] = useMutation(LOGIN);
|
const [login, { loading }] = useMutation(LOGIN);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (authentication.isAuthenticated) {
|
if (authentication.isAuthenticated) {
|
||||||
navigate(URLS.DASHBOARD);
|
navigate(URLS.DASHBOARD);
|
||||||
}
|
}
|
||||||
}, [authentication.isAuthenticated]);
|
}, [authentication.isAuthenticated]);
|
||||||
|
|
||||||
const handleSubmit = async (values) => {
|
const handleSubmit = async (values) => {
|
||||||
const { data } = await login({
|
const { data } = await login({
|
||||||
variables: {
|
variables: {
|
||||||
@@ -32,6 +35,7 @@ function LoginForm() {
|
|||||||
const { token } = data.login;
|
const { token } = data.login;
|
||||||
authentication.updateToken(token);
|
authentication.updateToken(token);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Paper sx={{ px: 2, py: 4 }}>
|
<Paper sx={{ px: 2, py: 4 }}>
|
||||||
<Typography
|
<Typography
|
||||||
@@ -107,4 +111,5 @@ function LoginForm() {
|
|||||||
</Paper>
|
</Paper>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LoginForm;
|
export default LoginForm;
|
||||||
|
@@ -25,27 +25,9 @@ const queryClient = new QueryClient({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export default function AutomatischQueryClientProvider({ children }) {
|
export default function AutomatischQueryClientProvider({ children }) {
|
||||||
const { token, initialize } = useAuthentication();
|
|
||||||
|
|
||||||
React.useEffect(
|
|
||||||
function updateTokenInHttpClient() {
|
|
||||||
if (!initialize) return;
|
|
||||||
|
|
||||||
if (token) {
|
|
||||||
api.defaults.headers.Authorization = token;
|
|
||||||
} else {
|
|
||||||
delete api.defaults.headers.Authorization;
|
|
||||||
}
|
|
||||||
|
|
||||||
initialize();
|
|
||||||
},
|
|
||||||
[initialize, token],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
{children}
|
{children}
|
||||||
|
|
||||||
<ReactQueryDevtools />
|
<ReactQueryDevtools />
|
||||||
</QueryClientProvider>
|
</QueryClientProvider>
|
||||||
);
|
);
|
||||||
|
@@ -1,31 +1,34 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { getItem, setItem } from 'helpers/storage';
|
import { getItem, removeItem, setItem } from 'helpers/storage';
|
||||||
|
import api from 'helpers/api.js';
|
||||||
|
|
||||||
export const AuthenticationContext = React.createContext({
|
export const AuthenticationContext = React.createContext({
|
||||||
token: null,
|
token: null,
|
||||||
updateToken: () => void 0,
|
updateToken: () => {},
|
||||||
|
removeToken: () => {},
|
||||||
isAuthenticated: false,
|
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,
|
||||||
updateToken: (newToken) => {
|
updateToken: (newToken) => {
|
||||||
|
api.defaults.headers.Authorization = newToken;
|
||||||
setToken(newToken);
|
setToken(newToken);
|
||||||
setItem('token', newToken);
|
setItem('token', newToken);
|
||||||
},
|
},
|
||||||
isAuthenticated: Boolean(token) && isInitialized,
|
removeToken: () => {
|
||||||
initialize: () => {
|
delete api.defaults.headers.Authorization;
|
||||||
setInitialized(true);
|
setToken(null);
|
||||||
|
removeItem('token');
|
||||||
},
|
},
|
||||||
|
isAuthenticated: Boolean(token),
|
||||||
};
|
};
|
||||||
}, [token, isInitialized]);
|
}, [token]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthenticationContext.Provider value={value}>
|
<AuthenticationContext.Provider value={value}>
|
||||||
|
@@ -24,7 +24,6 @@ root.render(
|
|||||||
<ThemeProvider>
|
<ThemeProvider>
|
||||||
<MetadataProvider>
|
<MetadataProvider>
|
||||||
{routes}
|
{routes}
|
||||||
|
|
||||||
<LiveChat />
|
<LiveChat />
|
||||||
</MetadataProvider>
|
</MetadataProvider>
|
||||||
</ThemeProvider>
|
</ThemeProvider>
|
||||||
|
Reference in New Issue
Block a user