add Logout

This commit is contained in:
Jan Naahs
2020-06-13 17:03:14 +02:00
parent 3fb85fccfa
commit 98ce8db942
5 changed files with 51 additions and 17 deletions

View File

@@ -2,7 +2,7 @@ import React, {useCallback, useState} from 'react';
import user from "../api/resources/user"; import user from "../api/resources/user";
import Login from "./views/Login"; import Login from "./views/Login";
import {Redirect, Route, Switch} from "react-router"; import {Redirect, Route, Switch, useHistory} from "react-router";
import Controls from "./views/Controls"; import Controls from "./views/Controls";
import {BrowserRouter} from "react-router-dom"; import {BrowserRouter} from "react-router-dom";
import Logs from "./views/Logs"; import Logs from "./views/Logs";
@@ -12,12 +12,21 @@ import Layout from "./components/Layout";
const App = () => { const App = () => {
const [isAuthenticated, setIsAuthenticated] = useState(false); const [isAuthenticated, setIsAuthenticated] = useState(false);
const history = useHistory();
const handleAuthenticationStatus = async () => { const handleAuthenticationStatus = async () => {
const status = await user.status(); const status = await user.status();
setIsAuthenticated(status.success); setIsAuthenticated(status.success);
}; };
const handleLogout = async () => {
const loggedOut = await user.logout();
if (loggedOut.success) {
setIsAuthenticated(false);
history.push('/login');
}
}
const ProtectedRoute = useCallback(({component: Component, ...rest}) => ( const ProtectedRoute = useCallback(({component: Component, ...rest}) => (
<Route {...rest} render={(props) => ( <Route {...rest} render={(props) => (
isAuthenticated isAuthenticated
@@ -34,7 +43,7 @@ const App = () => {
<Switch> <Switch>
<Route path="/login" render={() => (<Login handleLogin={handleAuthenticationStatus}/>)}/> <Route path="/login" render={() => (<Login handleLogin={handleAuthenticationStatus}/>)}/>
<Layout> <Layout handleLogout={handleLogout}>
<ProtectedRoute exact path="/" component={Controls}/> <ProtectedRoute exact path="/" component={Controls}/>
<ProtectedRoute path="/saves" component={Saves}/> <ProtectedRoute path="/saves" component={Saves}/>
<ProtectedRoute path="/mods" component={Controls}/> <ProtectedRoute path="/mods" component={Controls}/>

View File

@@ -1,10 +1,12 @@
import React, {useEffect, useRef, useState} from "react"; import React, {useEffect, useState} from "react";
import server from "../../api/resources/server"; import server from "../../api/resources/server";
import {NavLink} from "react-router-dom"; import {NavLink, useHistory} from "react-router-dom";
import Button from "../elements/Button";
const Layout = (props) => { const Layout = ({children, handleLogout}) => {
const [serverStatus, setServerStatus] = useState(null); const [serverStatus, setServerStatus] = useState(null);
const history = useHistory();
useEffect(() => { useEffect(() => {
(async () => { (async () => {
@@ -15,17 +17,16 @@ const Layout = (props) => {
})(); })();
}, []); }, []);
const Status = (props) => { const Status = ({info}) => {
let text = 'Unknown'; let text = 'Unknown';
let color = 'gray-light'; let color = 'gray-light';
if (props.info && props.info.success) { if (info && info.success) {
console.log(props.info); if (info.data.status === 'running') {
if (props.info.data.status === 'running') {
text = 'Running'; text = 'Running';
color = 'green'; color = 'green';
} else if (props.info.data.status === 'stopped') { } else if (info.data.status === 'stopped') {
text = 'Stopped'; text = 'Stopped';
color = 'red'; color = 'red';
} }
@@ -52,7 +53,7 @@ const Layout = (props) => {
{/*Main*/} {/*Main*/}
<div className="w-full md:w-5/6 bg-gray-100 bg-banner bg-fixed min-h-screen"> <div className="w-full md:w-5/6 bg-gray-100 bg-banner bg-fixed min-h-screen">
<div className="container mx-auto bg-gray-100 pt-16 px-6"> <div className="container mx-auto bg-gray-100 pt-16 px-6">
{props.children} {children}
</div> </div>
</div> </div>
@@ -84,7 +85,13 @@ const Layout = (props) => {
<div className="py-4 px-2 border-b-2 border-black"> <div className="py-4 px-2 border-b-2 border-black">
<h1 className="text-dirty-white text-lg mb-2 mx-4">Administration</h1> <h1 className="text-dirty-white text-lg mb-2 mx-4">Administration</h1>
<div className="text-white text-center rounded-sm bg-black shadow-inner mx-4 p-1"> <div className="text-white text-center rounded-sm bg-black shadow-inner mx-4 p-1">
<Link to="/user-management" last={true}>User Management</Link> <Link to="/user-management">User Management</Link>
<Link to="/help" last={true}>Help</Link>
</div>
</div>
<div className="py-4 px-2 border-b-2 border-black">
<div className="text-white text-center rounded-sm bg-black shadow-inner mx-4 p-1">
<Button type="danger" onClick={handleLogout}>Logout</Button>
</div> </div>
</div> </div>
</div> </div>

View File

@@ -1,10 +1,24 @@
import React from "react"; import React from "react";
const Button = () => { const Button = ({ children, type, onClick, isSubmit }) => {
let color = '';
switch (type) {
case 'success':
color = 'bg-green hover:bg-green-light';
break;
case 'danger':
color = 'bg-red hover:bg-red-light';
break;
default:
color = 'bg-gray-light hover:bg-orange'
}
return ( return (
<button className="bg-green hover:bg-green-light text-black font-bold py-2 px-4 w-full" <button onClick={onClick} className={color + ' text-black font-bold py-2 px-4 w-full'}
type="submit"> type={isSubmit ? 'submit' : 'button'}>
Sign In {children}
</button> </button>
); );
} }

View File

@@ -57,7 +57,7 @@ const Login = ({handleLogin}) => {
{errors.password && <span className="block text-red">Password is required</span>} {errors.password && <span className="block text-red">Password is required</span>}
</div> </div>
<div className="text-center"> <div className="text-center">
<Button/> <Button type="success" isSubmit={true}>Sign In</Button>
</div> </div>
</div> </div>
</form> </form>

View File

@@ -8,5 +8,9 @@ export default {
login: async data => { login: async data => {
const response = await client.post('/api/login', JSON.stringify(data)); const response = await client.post('/api/login', JSON.stringify(data));
return response.data; return response.data;
},
logout: async () => {
const response = await client.get('/api/logout');
return response.data;
} }
} }