Last active
November 11, 2020 23:57
-
-
Save blogcacanid/4b85d7a4ae3be3a261a518a90d27b515 to your computer and use it in GitHub Desktop.
App.js Authentication JWT React JS Lumen 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from "react"; | |
import { useDispatch, useSelector } from "react-redux"; | |
import { Router, Switch, Route, Link } from "react-router-dom"; | |
import "bootstrap/dist/css/bootstrap.min.css"; | |
import logo from './logo.svg'; | |
import "./App.css"; | |
import Login from "./components/Login"; | |
import Register from "./components/Register"; | |
import Home from "./components/Home"; | |
import Profile from "./components/Profile"; | |
import { logout } from "./actions/auth"; | |
import { clearMessage } from "./actions/message"; | |
import { history } from "./helpers/history"; | |
const App = () => { | |
const { user: currentUser } = useSelector((state) => state.auth); | |
const dispatch = useDispatch(); | |
useEffect(() => { | |
history.listen((location) => { | |
dispatch(clearMessage()); | |
}); | |
}, [dispatch]); | |
const logOut = () => { | |
dispatch(logout()); | |
}; | |
return ( | |
<Router history={history}> | |
<div> | |
<nav className="navbar navbar-expand navbar-dark bg-dark"> | |
<Link to={"/"} className="navbar-brand"> | |
<img src={logo} className="App-logo" alt="React logo" width="32" /> | |
REACT LOGIN JWT | |
</Link> | |
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> | |
<span className="navbar-toggler-icon"></span> | |
</button> | |
<div className="navbar-nav mr-auto"> | |
<li className="nav-item"> | |
<Link to={"/home"} className="nav-link"> | |
Home | |
</Link> | |
</li> | |
</div> | |
{currentUser ? ( | |
<div className="navbar-nav ml-auto"> | |
<li className="nav-item"> | |
<Link to={"/profile"} className="nav-link"> | |
{currentUser.username} | |
</Link> | |
</li> | |
<li className="nav-item"> | |
<a href="/login" className="nav-link" onClick={logOut}> | |
LogOut | |
</a> | |
</li> | |
</div> | |
) : ( | |
<div className="navbar-nav ml-auto"> | |
<li className="nav-item"> | |
<Link to={"/register"} className="nav-link"> | |
Register | |
</Link> | |
</li> | |
<li className="nav-item"> | |
<Link to={"/login"} className="nav-link"> | |
Login | |
</Link> | |
</li> | |
</div> | |
)} | |
</nav> | |
<div className="container mt-3"> | |
<Switch> | |
<Route exact path={["/", "/home"]} component={Home} /> | |
<Route exact path="/login" component={Login} /> | |
<Route exact path="/register" component={Register} /> | |
<Route exact path="/profile" component={Profile} /> | |
</Switch> | |
</div> | |
</div> | |
</Router> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment