Feat/login
This commit is contained in:
parent
4803c4d8b6
commit
3361d4c043
7 changed files with 91 additions and 167 deletions
|
|
@ -1,38 +1,22 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { getAuth, signIn, signOut, register as apiRegister, refreshUser as apiRefreshUser } from '../redux';
|
||||
import { getAuth, signIn, signOut } from '../redux';
|
||||
|
||||
export function useAuth() {
|
||||
const dispatch = useDispatch();
|
||||
const auth = useSelector(getAuth);
|
||||
|
||||
const logIn = useCallback(
|
||||
(email: string, password: string) => {
|
||||
return dispatch(signIn(email, password));
|
||||
},
|
||||
[dispatch],
|
||||
);
|
||||
|
||||
const register = useCallback(
|
||||
(email: string, password: string, firstName: string, lastName: string) => {
|
||||
return dispatch(apiRegister(email, password, firstName, lastName));
|
||||
},
|
||||
[dispatch],
|
||||
);
|
||||
const logIn = useCallback(() => {
|
||||
return dispatch(signIn());
|
||||
}, [dispatch]);
|
||||
|
||||
const logOut = useCallback(() => {
|
||||
return dispatch(signOut());
|
||||
}, [dispatch]);
|
||||
|
||||
const refreshUser = useCallback(() => {
|
||||
return dispatch(apiRefreshUser());
|
||||
}, [dispatch]);
|
||||
|
||||
return {
|
||||
auth,
|
||||
logIn,
|
||||
logOut,
|
||||
register,
|
||||
refreshUser,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
export { useAuth } from './hooks';
|
||||
export { getAuth, reducer, signIn, signOut, register, AuthActionTypes, getIsAuthLoading } from './redux';
|
||||
export { getAuth, reducer, signIn, signOut, AuthActionTypes, getIsAuthLoading } from './redux';
|
||||
export * from './types';
|
||||
|
|
|
|||
|
|
@ -15,20 +15,10 @@ const signOutAction = (): SuccessAction => ({
|
|||
payload: null,
|
||||
});
|
||||
|
||||
export const signIn = (email: string, password: string) =>
|
||||
export const signIn = () =>
|
||||
createApiAction(
|
||||
{
|
||||
path: '/login',
|
||||
method: 'POST',
|
||||
body: { username: email, password },
|
||||
},
|
||||
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
|
||||
);
|
||||
|
||||
export const refreshUser = () =>
|
||||
createApiAction(
|
||||
{
|
||||
path: '/dashboard',
|
||||
path: '/hydra/callback',
|
||||
method: 'GET',
|
||||
},
|
||||
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
|
||||
|
|
@ -39,18 +29,3 @@ export function signOut() {
|
|||
dispatch(signOutAction());
|
||||
};
|
||||
}
|
||||
|
||||
export const register = (email: string, password: string, firstName: string, lastName: string) =>
|
||||
createApiAction(
|
||||
{
|
||||
path: '/auth/register',
|
||||
method: 'POST',
|
||||
body: {
|
||||
email,
|
||||
firstName,
|
||||
lastName,
|
||||
password,
|
||||
},
|
||||
},
|
||||
[AuthActionTypes.REGISTRATION_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.REGISTRATION_FAILURE],
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export { signIn, signOut, register, refreshUser, AuthActionTypes } from './actions';
|
||||
export { signIn, signOut, AuthActionTypes } from './actions';
|
||||
export { default as reducer } from './reducers';
|
||||
export { getAuth, getIsAuthLoading } from './selectors';
|
||||
export * from './types';
|
||||
|
|
|
|||
Reference in a new issue