Feat/logged in user data
This commit is contained in:
parent
41df7429d2
commit
595372bef0
15 changed files with 351 additions and 141 deletions
|
|
@ -1,9 +1,10 @@
|
|||
import { useCallback } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { getAuthToken, signIn, signOut } from '../redux';
|
||||
import { getAuthToken, getCurrentUser, signIn, signOut } from '../redux';
|
||||
|
||||
export function useAuth() {
|
||||
const dispatch = useDispatch();
|
||||
const currentUser = useSelector(getCurrentUser);
|
||||
const authToken = useSelector(getAuthToken);
|
||||
|
||||
const logIn = useCallback(
|
||||
|
|
@ -19,6 +20,7 @@ export function useAuth() {
|
|||
|
||||
return {
|
||||
authToken,
|
||||
currentUser,
|
||||
logIn,
|
||||
logOut,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
export { signIn, signOut, AuthActionTypes } from './actions';
|
||||
export { default as reducer } from './reducers';
|
||||
export { getAuth, getIsAuthLoading, getAuthToken } from './selectors';
|
||||
export { getAuth, getIsAuthLoading, getAuthToken, getCurrentUser } from './selectors';
|
||||
export * from './types';
|
||||
|
|
|
|||
|
|
@ -2,15 +2,24 @@ import { createApiReducer, chainReducers, INITIAL_API_STATUS } from 'src/service
|
|||
|
||||
import { AuthState } from './types';
|
||||
import { AuthActionTypes } from './actions';
|
||||
import { CurrentUser } from '../types';
|
||||
|
||||
const initialCurrentUserState: CurrentUser = {
|
||||
email: null,
|
||||
name: null,
|
||||
preferredUsername: null,
|
||||
id: null,
|
||||
};
|
||||
|
||||
const initialState: AuthState = {
|
||||
token: null,
|
||||
userInfo: initialCurrentUserState,
|
||||
_status: INITIAL_API_STATUS,
|
||||
};
|
||||
|
||||
const auth = createApiReducer(
|
||||
[AuthActionTypes.SIGN_IN_START, AuthActionTypes.SIGN_IN_SUCCESS, AuthActionTypes.SIGN_IN_FAILURE],
|
||||
(data) => ({ token: data.accessToken }),
|
||||
(data) => ({ token: data.accessToken, userInfo: data.userInfo }),
|
||||
(data) => data.error.message,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ export const getAuth = (state: State) => state.auth;
|
|||
|
||||
export const getAuthToken = (state: State) => state.auth.token;
|
||||
|
||||
export const getCurrentUser = (state: State) => state.auth.userInfo;
|
||||
|
||||
export const getIsAuthLoading = (state: State) => isLoading(getAuth(state));
|
||||
|
||||
export const getToken = (state: State) => state.auth.token;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,11 @@
|
|||
export interface Auth {
|
||||
token: string | null;
|
||||
userInfo: CurrentUser;
|
||||
}
|
||||
|
||||
export interface CurrentUser {
|
||||
email: string | null;
|
||||
name: string | null;
|
||||
preferredUsername: string | null;
|
||||
id: string | null;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,7 +70,7 @@ export const updateUserById = (user: any) => async (dispatch: Dispatch<any>) =>
|
|||
payload: transformResponseUser(data),
|
||||
});
|
||||
|
||||
showToast('User updated sucessfully.', ToastType.Success);
|
||||
showToast('User updated successfully.', ToastType.Success);
|
||||
|
||||
dispatch(fetchUsers());
|
||||
} catch (err) {
|
||||
|
|
@ -95,7 +95,7 @@ export const createUser = (user: any) => async (dispatch: Dispatch<any>) => {
|
|||
payload: transformResponseUser(data),
|
||||
});
|
||||
|
||||
showToast('User created sucessfully.', ToastType.Success);
|
||||
showToast('User created successfully.', ToastType.Success);
|
||||
|
||||
dispatch(fetchUsers());
|
||||
} catch (err: any) {
|
||||
|
|
@ -121,7 +121,7 @@ export const deleteUser = (id: string) => async (dispatch: Dispatch<any>) => {
|
|||
payload: {},
|
||||
});
|
||||
|
||||
showToast('User deleted sucessfully.', ToastType.Success);
|
||||
showToast('User deleted successfully.', ToastType.Success);
|
||||
|
||||
dispatch(fetchUsers());
|
||||
} catch (err) {
|
||||
|
|
|
|||
Reference in a new issue