mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
feat: application booting.
This commit is contained in:
@@ -1,28 +1,37 @@
|
||||
import { useMutation } from 'react-query';
|
||||
import useApiRequest from '../useRequest';
|
||||
import { useAuthActions } from '../state';
|
||||
import { persistor } from 'store/createStore';
|
||||
import { setCookie } from '../../utils';
|
||||
|
||||
/**
|
||||
* Saves the response data to cookies.
|
||||
*/
|
||||
function setAuthLoginCookies(data) {
|
||||
setCookie('token', data.token);
|
||||
setCookie('authenticated_user_id', data.user.id);
|
||||
setCookie('organization_id', data.tenant.organization_id);
|
||||
setCookie('tenant_id', data.tenant.id);
|
||||
|
||||
if (data?.tenant?.metadata?.language)
|
||||
setCookie('locale', data.tenant.metadata.language);
|
||||
}
|
||||
|
||||
/**
|
||||
* Authentication login.
|
||||
*/
|
||||
export const useAuthLogin = (props) => {
|
||||
const { setLogin } = useAuthActions();
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useMutation(
|
||||
(values) => apiRequest.post('auth/login', values),
|
||||
{
|
||||
select: (res) => res.data,
|
||||
onSuccess: (data) => {
|
||||
setLogin(data.data);
|
||||
return useMutation((values) => apiRequest.post('auth/login', values), {
|
||||
select: (res) => res.data,
|
||||
onSuccess: (data) => {
|
||||
// Set authentication cookies.
|
||||
setAuthLoginCookies(data.data);
|
||||
|
||||
// Run the store persist.
|
||||
persistor.persist();
|
||||
},
|
||||
...props
|
||||
}
|
||||
);
|
||||
// Reboot the application.
|
||||
window.location.reload();
|
||||
},
|
||||
...props,
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -34,7 +43,7 @@ export const useAuthRegister = (props) => {
|
||||
return useMutation(
|
||||
(values) => apiRequest.post('auth/register', values),
|
||||
props,
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -45,9 +54,9 @@ export const useAuthSendResetPassword = (props) => {
|
||||
|
||||
return useMutation(
|
||||
(email) => apiRequest.post('auth/send_reset_password', email),
|
||||
props
|
||||
props,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Authentication reset password.
|
||||
@@ -58,5 +67,5 @@ export const useAuthResetPassword = (props) => {
|
||||
return useMutation(
|
||||
([token, values]) => apiRequest.post(`auth/reset/${token}`, values),
|
||||
props,
|
||||
)
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
9
client/src/hooks/query/base.js
Normal file
9
client/src/hooks/query/base.js
Normal file
@@ -0,0 +1,9 @@
|
||||
// Query client config.
|
||||
export const queryConfig = {
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
refetchOnWindowFocus: true,
|
||||
staleTime: 30000,
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -112,11 +112,16 @@ export function useUsers(props) {
|
||||
* Retrieve details of the given user.
|
||||
*/
|
||||
export function useUser(id, props) {
|
||||
const apiRequest = useApiRequest();
|
||||
|
||||
return useQueryTenant(
|
||||
return useRequestQuery(
|
||||
[t.USER, id],
|
||||
() => apiRequest.get(`users/${id}`).then((response) => response.data.user),
|
||||
props,
|
||||
{
|
||||
method: 'get',
|
||||
url: `users/${id}`,
|
||||
},
|
||||
{
|
||||
select: (response) => response.data.user,
|
||||
defaultData: {},
|
||||
...props,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,15 @@ import {
|
||||
setStoreReset,
|
||||
} from 'store/authentication/authentication.actions';
|
||||
import { useQueryClient } from 'react-query';
|
||||
import { removeCookie } from '../../utils';
|
||||
|
||||
function removeAuthenticationCookies() {
|
||||
removeCookie('token');
|
||||
removeCookie('organization_id');
|
||||
removeCookie('tenant_id');
|
||||
removeCookie('authenticated_user_id');
|
||||
removeCookie('locale');
|
||||
}
|
||||
|
||||
export const useAuthActions = () => {
|
||||
const dispatch = useDispatch();
|
||||
@@ -15,11 +24,15 @@ export const useAuthActions = () => {
|
||||
setLogin: useCallback((login) => dispatch(setLogin(login)), [dispatch]),
|
||||
setLogout: useCallback(() => {
|
||||
// Resets store state.
|
||||
dispatch(setStoreReset());
|
||||
// dispatch(setStoreReset());
|
||||
|
||||
// Remove all cached queries.
|
||||
queryClient.removeQueries();
|
||||
}, [dispatch, queryClient]),
|
||||
|
||||
removeAuthenticationCookies();
|
||||
|
||||
window.location.reload();
|
||||
}, [queryClient]),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -41,12 +54,12 @@ export const useAuthToken = () => {
|
||||
* Retrieve the authentication user.
|
||||
*/
|
||||
export const useAuthUser = () => {
|
||||
return useSelector((state) => state.authentication.user);
|
||||
return useSelector((state) => ({}));
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the authenticated organization id.
|
||||
*/
|
||||
export const useAuthOrganizationId = () => {
|
||||
return useSelector((state) => state.authentication.organization);
|
||||
return useSelector((state) => state.authentication.organizationId);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user