]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/app/index.js
improved user context
[alttp.git] / resources / js / app / index.js
index fcc5311ec6404deaba12e4ed1c16c7f58b75b6f9..6a6b6c6bbda6f0184451cadefbe1ff74d7aa8528 100644 (file)
@@ -1,47 +1,17 @@
-import axios from 'axios';
-import React, { useEffect, useState } from 'react';
+import React from 'react';
 import { Helmet } from 'react-helmet';
 import { useTranslation } from 'react-i18next';
 import { BrowserRouter } from 'react-router-dom';
 
 import Routes from './Routes';
 import AlttpBaseRomProvider from '../helpers/AlttpBaseRomContext';
-import UserContext from '../helpers/UserContext';
+import { UserProvider } from '../hooks/user';
 import i18n from '../i18n';
 
 const App = () => {
-       const [user, setUser] = useState(null);
-
        const { t } = useTranslation();
 
-       const checkAuth = async () => {
-               try {
-                       const response = await axios.get('/api/user');
-                       setUser(response.data);
-               } catch (e) {
-                       setUser(null);
-               }
-       };
-
-       const doLogout = async () => {
-               await axios.post('/logout');
-               await checkAuth();
-       };
-
-       useEffect(() => {
-               let timer = null;
-               axios
-                       .get('/sanctum/csrf-cookie')
-                       .then(() => {
-                               checkAuth();
-                               timer = setInterval(checkAuth, 15 * 60 * 1000);
-                       });
-               return () => {
-                       if (timer) clearInterval(timer);
-               };
-       }, []);
-
-       useEffect(() => {
+       React.useEffect(() => {
                window.Echo.channel('App.Control')
                        .listen('PleaseRefresh', () => {
                                location.reload();
@@ -53,14 +23,14 @@ const App = () => {
 
        return <BrowserRouter>
                <AlttpBaseRomProvider>
-                       <UserContext.Provider value={user}>
+                       <UserProvider>
                                <Helmet>
                                        <html lang={i18n.language} />
                                        <title>{t('general.appName')}</title>
                                        <meta name="description" content={t('general.appDescription')} />
                                </Helmet>
-                               <Routes doLogout={doLogout} />
-                       </UserContext.Provider>
+                               <Routes />
+                       </UserProvider>
                </AlttpBaseRomProvider>
        </BrowserRouter>;
 };