Advertisement
Hen_B_S

AuthActions, AuthReducer and Store Function

Feb 3rd, 2023 (edited)
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 1.10 KB | Source Code | 0 0
  1. import { AUTH_FAILURE, AUTH_REQ, AUTH_SUCCESS, LOGIN_REQUEST, LOGOUT_REQUEST } from "types/authTypes";
  2. import { createStore} from "redux";
  3. import rootReducer from "./reducers"
  4.  
  5. //Auth Actions
  6.  
  7. export const authenticate = () => {
  8.   return {
  9.     type: AUTH_REQ,
  10.   };
  11. };
  12.  
  13. export const authSuccess = (content: { token: string }) => {
  14.   localStorage.setItem("USER_KEY", content.token);
  15.   return {
  16.     type: AUTH_SUCCESS,
  17.     payload: content,
  18.   };
  19. };
  20.  
  21. export const authFailure = (error: any) => {
  22.   return {
  23.     type: AUTH_FAILURE,
  24.     payload: error,
  25.   };
  26. };
  27.  
  28. //Auth Reducer
  29.  
  30. const initialState = {
  31.   userName: "",
  32.   isLoggedIn: "",
  33. };
  34.  
  35. export const authReducer = (state = initialState, action: any) => {
  36.   switch (action.type) {
  37.     case LOGIN_REQUEST:
  38.     case LOGOUT_REQUEST:
  39.       return {
  40.         ...state,
  41.       };
  42.     case AUTH_SUCCESS:
  43.     case AUTH_FAILURE:
  44.       return {
  45.         username: action.payload.username,
  46.         isLoggedIn: action.payload.isLoggedIn,
  47.       };
  48.     default:
  49.       return state;
  50.   }
  51. };
  52.  
  53. //Store
  54.  
  55. export const store = createStore(rootReducer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement