PHP 7.4.33
Preview: AuthContext.js Size: 840 B
/var/www/web-koncept/client/src/context/AuthContext.js
import { createContext, useReducer } from "react";

export const AuthContext = createContext();

export const authReducer = (state, action) => {
  switch (action.type) {
    case "LOGIN":
      return { user: action.payload, access_token: action.token };
    case "LOGOUT":
      localStorage.removeItem("user");
      localStorage.removeItem("token");
      return { user: null, access_token: null };
    default:
      return state;
  }
};

export const AuthContextProvider = ({ children }) => {
  const [state, dispatch] = useReducer(authReducer, {
    user: localStorage.getItem("user")
      ? JSON.parse(localStorage.getItem("user"))
      : null,
    access_token: localStorage.getItem("token") || null,
  });

  return (
    <AuthContext.Provider value={{ ...state, dispatch }}>
      {children}
    </AuthContext.Provider>
  );
};

Directory Contents

Dirs: 0 × Files: 2
Name Size Perms Modified Actions
840 B lrw-r--r-- 2024-11-04 12:03:48
Edit Download
1.68 KB lrw-r--r-- 2024-11-06 05:16:11
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).