Advertisement
Shell_Casing

auth context

Jul 29th, 2020
1,628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useReducer, createContext } from 'react';
  2. import axios from "axios";
  3. import { Reducer } from "./reducer";
  4. import { config } from "../../config/config";
  5. import { SET_LOADING, LOAD_USER, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL, CLEAR_ERRORS } from "../../store/actions/types";
  6.  
  7. const { AUTH_URL, httpHeaders, setAuthHeaderToken } = config;
  8.  
  9. const initialState = {
  10.     loading: false,
  11.     sreToken: localStorage.getItem('sreToken'),
  12.     isAuthenticated: false,
  13.     user: null,
  14.     error: null
  15. };
  16.  
  17. export const AuthContext = createContext(initialState);
  18.  
  19. export const AuthState = ({ children }) => {
  20.  
  21.     const [state, dispatch] = useReducer(Reducer, initialState);
  22.  
  23.     const setLoading = () => dispatch({ type: SET_LOADING });
  24.  
  25.     // load user: hits the auth endpoint to check the token (if available) and return the user info
  26.     const loadUser = async () => {
  27.         if(localStorage.sre) {
  28.             setAuthHeaderToken(localStorage.sre);
  29.         }
  30.         try {
  31.             const { data } = await axios.get(AUTH_URL.loadUser);
  32.             console.log(data);
  33.             return dispatch({ type: LOAD_USER, payload: data.user });
  34.         } catch (e) {
  35.             console.log(e);
  36.             dispatch({ type: AUTH_ERROR, payload: e });
  37.             localStorage.removeItem('sre');
  38.         }
  39.     };
  40.  
  41.     // login: logs in the user and sets up the token
  42.     const login = async userCredentials => {
  43.         try {
  44.             console.log(userCredentials);
  45.             setLoading();
  46.             const { data } = await axios.post(AUTH_URL.login, userCredentials, httpHeaders);
  47.             await dispatch({ type: LOGIN_SUCCESS, payload: data.token });
  48.             await loadUser();
  49.         } catch (e) {
  50.             console.log(e);
  51.             localStorage.removeItem('sre');
  52.             dispatch({ type: LOGIN_FAIL, payload: `Invalid credentials, ${e}` });
  53.         }
  54.  
  55.     };
  56.  
  57.     const clearErrors = () => dispatch({ type: CLEAR_ERRORS });
  58.  
  59.     return (
  60.         <AuthContext.Provider value={{
  61.             loading: state.loading,
  62.             isAuthenticated: state.isAuthenticated,
  63.             user: state.user,
  64.             error: state.error,
  65.             setLoading,
  66.             loadUser,
  67.             login,
  68.             clearErrors
  69.         }}>
  70.             { children }
  71.         </AuthContext.Provider>
  72.     )
  73.  
  74. };
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement