]> git.localhorst.tv Git - alttp.git/blobdiff - resources/js/components/App.js
add discord auth
[alttp.git] / resources / js / components / App.js
index a4d02b12aa40a8f4ce85b39e8fb3278849d0a22a..74cec3d7d9b7703f9a88e7663fe6564026710d71 100644 (file)
@@ -1,18 +1,50 @@
 import axios from 'axios';
-import React, { useEffect } from 'react';
+import React, { useEffect, useState } from 'react';
 import { BrowserRouter, Route, Routes } from 'react-router-dom';
 
+import Header from './common/Header';
 import Front from './pages/Front';
+import UserContext from '../helpers/UserContext';
 
 const App = () => {
+       const [user, setUser] = useState(null);
+
+       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(() => {
-               axios.get('/sanctum/csrf-cookie');
+               let timer = null;
+               axios
+                       .get('/sanctum/csrf-cookie')
+                       .then(() => {
+                               checkAuth();
+                               timer = setInterval(checkAuth, 15 * 60 * 1000);
+                       });
+               return () => {
+                       if (timer) clearInterval(timer);
+               };
        }, []);
 
        return <BrowserRouter>
-               <Routes>
-                       <Route path="*" element={<Front />} />
-               </Routes>
+               <UserContext.Provider value={user}>
+                       <Header doLogout={doLogout} />
+                       {user ?
+                               <Routes>
+                                       <Route path="*" element={<Front />} />
+                               </Routes>
+                       : <Front />}
+               </UserContext.Provider>
        </BrowserRouter>;
 };