Last active
February 18, 2025 15:36
-
-
Save sineto/52d6a4f634cb51c2a6e6013dc64be47b to your computer and use it in GitHub Desktop.
Revisions
-
Sinésio Neto revised this gist
Jul 23, 2020 . 3 changed files with 34 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,24 +1,18 @@ import React from 'react'; import { BrowserRouter } from 'react-router-dom'; import { UserProvider } from './services/UserContext'; import Routes from './Routes' function App() { return ( <BrowserRouter> <UserProvider> <Routes /> </UserProvider> </BrowserRouter> ); } export default App; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,11 @@ import React from 'react'; import Header from '../Header'; const Layout = ({ children }) => { return ( <> <Header /> { children } </> ); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ import React from 'react'; import { Switch, Route } from 'react-router-dom'; import Home from './pages/Home'; import Login from './components/Login'; import Dashboard from './components/Dashboard'; import Users from './components/Users'; import Products from './components/Products'; const Routes = () => { return ( <Switch> <Route path='/' exact component={Home} /> <Route path='/login' exact component={Login} /> <Route path='/admin' component={Dashboard} /> <Route path='/users' component={Users} /> <Route path='/products' component={Products} /> </Switch> ); }; export default Routes; -
Sinésio Neto revised this gist
Jul 23, 2020 . 3 changed files with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,14 +8,14 @@ export const login = async (username, password) => { const token = response.data.token; if (token) { localStorage.setItem('user', JSON.stringify(response.data)); } return response.data; }; export const isAuthenticated = () => { const user = localStorage.getItem('user'); if (!user) { return {} } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,7 @@ const Header = () => { const history = useHistory(); const handleLogOut = () => { localStorage.removeItem('user'); setCurrentUser({}); history.push('/'); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ export const UserProvider = ({ children }) => { const checkLoggedIn = async () => { let cuser = isAuthenticated(); if (cuser === null) { localStorage.setItem('user', ''); cuser = ''; } -
Sinésio Neto revised this gist
Jul 23, 2020 . No changes.There are no files selected for viewing
-
Sinésio Neto revised this gist
Jul 23, 2020 . No changes.There are no files selected for viewing
-
Sinésio Neto created this gist
Jul 23, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ import React from 'react'; import { Switch, Route } from 'react-router-dom'; import Home from './pages/Home'; import Login from './components/Login'; import Dashboard from './components/Dashboard'; import Users from './components/Users'; import Products from './components/Products'; function App() { return ( <> <Switch> <Route path='/' exact component={Home} /> <Route path='/login' exact component={Login} /> <Route path='/admin' component={Dashboard} /> <Route path='/users' component={Users} /> <Route path='/products' component={Products} /> </Switch> </> ); } export default App; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ import api from './api'; export const login = async (username, password) => { const response = await api.post('login', { username, password }); const token = response.data.token; if (token) { localStorage.setItem('valentiaUser', JSON.stringify(response.data)); } return response.data; }; export const isAuthenticated = () => { const user = localStorage.getItem('valentiaUser'); if (!user) { return {} } return JSON.parse(user); }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import React from 'react'; import Layout from '../../components/Layout'; const Dashboard = () => { return ( <> <Layout> <h1>Dashboard</h1> </Layout> </> ); }; export default Dashboard; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ import React, { useContext } from 'react'; import { Link, useHistory } from 'react-router-dom'; import UserContext from '../../services/UserContext'; const Header = () => { const [ currentUser, setCurrentUser ] = useContext(UserContext); const history = useHistory(); const handleLogOut = () => { localStorage.removeItem('valentiaUser'); setCurrentUser({}); history.push('/'); }; console.log('header', currentUser); return ( <> <header> <nav> <ul> <li> <Link to='/'>Home</Link> </li> <li> <Link to='/products'>Products</Link> </li> <li> <Link to='/users'>Users</Link> </li> </ul> <button type='button' onClick={handleLogOut} > Log out </button> </nav> </header> </> ); }; export default Header; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ import React from 'react'; import Header from '../Header'; import { UserProvider } from '../../services/UserContext'; const Layout = ({ children }) => { return ( <> <UserProvider> <Header /> { children } </UserProvider> </> ); }; export default Layout; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ import React, { useState } from 'react'; import { useHistory } from 'react-router-dom'; import { login } from '../../services/AuthService'; const Login = () => { const [ username, setUsername ] = useState(''); const [ password, setPassword ] = useState(''); const history = useHistory(); const onChangeUsername = (e) => { setUsername(e.target.value); }; const onChangePassword = (e) => { setPassword(e.target.value); }; const onSubmit = async (e) => { e.preventDefault(); try { await login(username, password); history.push('/admin'); } catch (error) { console.error('error', error); } }; return ( <> <form onSubmit={onSubmit}> <label htmlFor='username'>Username</label> <input type='text' name='username' value={username} onChange={onChangeUsername} /> <label htmlFor='password'>Password</label> <input type='password' name='password' value={password} onChange={onChangePassword} /> <button type='submit'>Login</button> </form> </> ); }; export default Login; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import React from 'react'; import Layout from '../../components/Layout'; const Products = () => { return ( <> <Layout> <h1>Products</h1> </Layout> </> ); }; export default Products; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import React, { useState, useEffect, createContext } from 'react'; import { Redirect } from 'react-router-dom'; import { isAuthenticated } from './AuthService'; import Login from '../components/Login'; const UserContext = createContext(); export const UserProvider = ({ children }) => { const [ currentUser, setCurrentUser ] = useState(undefined); useEffect(() => { const checkLoggedIn = async () => { let cuser = isAuthenticated(); if (cuser === null) { localStorage.setItem('valentiaUser', ''); cuser = ''; } setCurrentUser(cuser); }; checkLoggedIn(); }, []); console.log('usercontext', currentUser); return ( <UserContext.Provider value={[currentUser, setCurrentUser]}> { currentUser?.token ? children : <Login />} </UserContext.Provider> ); }; export default UserContext; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ import React from 'react'; import Layout from '../../components/Layout'; const Users = () => { return ( <> <Layout> <h1>Users</h1> </Layout> </> ); }; export default Users;