mirror of
https://github.com/Mintplex-Labs/anything-llm.git
synced 2024-11-15 10:50:31 +01:00
f48e6b1a3e
* chore: add @ as alias for frontend root * fix: remove bad tag
33 lines
1.0 KiB
JavaScript
33 lines
1.0 KiB
JavaScript
import React, { useState, createContext } from "react";
|
|
import { AUTH_TIMESTAMP, AUTH_TOKEN, AUTH_USER } from "@/utils/constants";
|
|
|
|
export const AuthContext = createContext(null);
|
|
export function ContextWrapper(props) {
|
|
const localUser = localStorage.getItem(AUTH_USER);
|
|
const localAuthToken = localStorage.getItem(AUTH_TOKEN);
|
|
const [store, setStore] = useState({
|
|
user: localUser ? JSON.parse(localUser) : null,
|
|
authToken: localAuthToken ? localAuthToken : null,
|
|
});
|
|
|
|
const [actions] = useState({
|
|
updateUser: (user, authToken = "") => {
|
|
localStorage.setItem(AUTH_USER, JSON.stringify(user));
|
|
localStorage.setItem(AUTH_TOKEN, authToken);
|
|
setStore({ user, authToken });
|
|
},
|
|
unsetUser: () => {
|
|
localStorage.removeItem(AUTH_USER);
|
|
localStorage.removeItem(AUTH_TOKEN);
|
|
localStorage.removeItem(AUTH_TIMESTAMP);
|
|
setStore({ user: null, authToken: null });
|
|
},
|
|
});
|
|
|
|
return (
|
|
<AuthContext.Provider value={{ store, actions }}>
|
|
{props.children}
|
|
</AuthContext.Provider>
|
|
);
|
|
}
|