webui: Add logout functionality

This commit is contained in:
Kevin Kandlbinder 2022-03-04 01:24:52 +01:00
parent b4aa9caff4
commit 45ca353b3f
2 changed files with 13 additions and 8 deletions

View file

@ -6,7 +6,7 @@ import RegisterView from "./components/auth/RegisterView";
import RequireAuth from "./features/auth/RequireAuth"; import RequireAuth from "./features/auth/RequireAuth";
import {useAppDispatch} from "./app/hooks"; import {useAppDispatch} from "./app/hooks";
import broadcastChannel from "./app/broadcastChannel"; import broadcastChannel from "./app/broadcastChannel";
import {receiveAuthUpdate} from "./features/auth/authSlice"; import {logOut, receiveAuthUpdate} from "./features/auth/authSlice";
function App() { function App() {
const dispatch = useAppDispatch() const dispatch = useAppDispatch()
@ -23,7 +23,10 @@ function App() {
<Route path={"login"} element={<LoginView/>} /> <Route path={"login"} element={<LoginView/>} />
<Route path={"register"} element={<RegisterView/>} /> <Route path={"register"} element={<RegisterView/>} />
</Route> </Route>
<Route path={"/"} element={<RequireAuth><h1>hi</h1></RequireAuth>}/> <Route path={"/"} element={<RequireAuth><h1>hi</h1> <button onClick={() => {
dispatch(logOut())
}
}>Log out</button></RequireAuth>}/>
</Routes> </Routes>
); );
} }

View file

@ -1,4 +1,4 @@
import React from "react"; import React, {useEffect} from "react";
import styles from "./AuthLayout.module.scss"; import styles from "./AuthLayout.module.scss";
import {Link, useLocation, useNavigate, useOutlet} from "react-router-dom"; import {Link, useLocation, useNavigate, useOutlet} from "react-router-dom";
@ -23,12 +23,14 @@ const AuthLayout = () => {
const navigate = useNavigate() const navigate = useNavigate()
useEffect(() => {
if(authState.status == "logged_in") { if(authState.status == "logged_in") {
if(locationState && locationState.location) { if(locationState && locationState.location) {
navigate(locationState.location, {replace: true}) navigate(locationState.location, {replace: true})
} }
navigate("/", {replace: true}) navigate("/", {replace: true})
} }
}, [authState])
return <div className={styles.auth}> return <div className={styles.auth}>
<div className={styles.background}/> <div className={styles.background}/>