import React, { useContext, createContext, useState } from "react";import {BrowserRouter as Router, Switch, Route, Link, Redirect, useHistory, useLocation} from "react-router-dom";export default function AuthExample() { return (
  • Public Page
  • Protected Page
);}const fakeAuth = { isAuthenticated: false, signin(cb) { fakeAuth.isAuthenticated = true; setTimeout(cb, 100); /* fake asyn */ }, signout(cb) {fakeAuth.isAuthenticated = false; setTimeout(cb, 100);}};const authContext = createContext();function ProvideAuth({ children }) { const auth = useProvideAuth(); return ({children});}function useAuth() { return useContext(authContext);}function useProvideAuth() { const [user, setUser] = useState(null); const signin = cb => { return fakeAuth.signin(() => { setUser("user"); cb(); }); }; const signout = cb => { return fakeAuth.signout(() => { setUser(null); cb(); }); }; return {user, signin, signout};}function AuthButton() { let history = useHistory(); let auth = useAuth(); return auth.user ? (

Welcome!{" "}

) : (

You are not logged in.

);}function PrivateRoute({ children, ...rest }) { let auth = useAuth(); return ( auth.user ? ( children ) : ()} />);}function PublicPage() {return

Public

;}function ProtectedPage() {return

Protected

;}function LoginPage() { let history = useHistory(); let location = useLocation(); let auth = useAuth(); let { from } = location.state || { from: { pathname: "/" } }; let login = () => { auth.signin(() => { history.replace(from); }); }; return (

You must log in to view the page at {from.pathname}

);}