mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 21:00:31 +00:00
refactoring: authentication with react-query.
This commit is contained in:
41
client/src/hooks/state/authentication.js
Normal file
41
client/src/hooks/state/authentication.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { useCallback } from 'react';
|
||||
import { isAuthenticated } from 'store/authentication/authentication.reducer';
|
||||
import { setLogin, setLogout } from 'store/authentication/authentication.actions';
|
||||
|
||||
export const useAuthActions = () => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
return {
|
||||
setLogin: useCallback((login) => dispatch(setLogin(login)), [dispatch]),
|
||||
setLogout: useCallback(() => dispatch(setLogout()), [dispatch]),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve whether the user is authenticated.
|
||||
*/
|
||||
export const useIsAuthenticated = () => {
|
||||
return useSelector(isAuthenticated);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the authentication token.
|
||||
*/
|
||||
export const useAuthToken = () => {
|
||||
return useSelector((state) => state.authentication.token);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the authentication user.
|
||||
*/
|
||||
export const useAuthUser = () => {
|
||||
return useSelector((state) => state.authentication.user);
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieve the authenticated organization id.
|
||||
*/
|
||||
export const useAuthOrganizationId = () => {
|
||||
return useSelector((state) => state.authentication.organization);
|
||||
};
|
||||
17
client/src/hooks/state/globalErrors.js
Normal file
17
client/src/hooks/state/globalErrors.js
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useSelector, useDispatch } from "react-redux";
|
||||
import { setGlobalErrors } from 'store/globalErrors/globalErrors.actions';
|
||||
|
||||
export const useSetGlobalErrors = () => {
|
||||
const dispatch = useDispatch();
|
||||
|
||||
return useCallback((errors) => {
|
||||
dispatch(setGlobalErrors(errors));
|
||||
}, [dispatch]);
|
||||
};
|
||||
|
||||
export const useGlobalErrors = () => {
|
||||
const globalErrors = useSelector(state => state.globalErrors.data);
|
||||
|
||||
return { globalErrors };
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
export * from './dashboard';
|
||||
export * from './dashboard';
|
||||
export * from './authentication';
|
||||
export * from './globalErrors';
|
||||
Reference in New Issue
Block a user