authentication state react context

Solutions on MaxInterview for authentication state react context by the best coders in the world

showing results for - "authentication state react context"
Eudora
17 Oct 2020
1//AuthState.js
2
3import { useReducer } from "react";
4import { AuthContext } from "./authContext";
5import { AuthReducer } from "./authReducer";
6import {
7  REGISTER_SUCCESS,
8  REGISTER_FAIL,
9  AUTH_ERROR,
10  USER_LOADED,
11  LOGIN_SUCCESS,
12  LOGIN_FAIL,
13  LOGOUT,
14  CLEAR_ERRORS,
15} from "../types";
16
17export const AuthState = (props) => {
18  const initialState = {
19    user: null,
20    token: localStorage.getItem("token"),
21    isAuthenticated: null,
22    loading: true,
23    error: null,
24  };
25
26  const [state, dispatch] = useReducer(AuthReducer, initialState);
27
28  /// actions here...
29
30  // Load users
31
32  // Register user
33
34  // Login user
35
36  // Logout
37
38  // Clear Errors
39
40  return (
41    <AuthContext.Provider
42      value={{
43        token: state.token,
44        isAuthenticated: state.isAuthenticated,
45        loading: state.loading,
46        user: state.user,
47        error: state.error,
48      }}>
49      {props.children}
50    </AuthContext.Provider>
51  );
52};
53