Last active
February 6, 2022 20:11
-
-
Save dialguiba/1545c7bc4c78f8375366ab60811d14ee to your computer and use it in GitHub Desktop.
routes
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 characters
import { useEffect, useState } from "react"; | |
import { BrowserRouter as Router, Switch, Redirect } from "react-router-dom"; | |
import { useDispatch } from "react-redux"; | |
import { JournalScreen } from "../components/journal/JournalScreen"; | |
import { AuthRouter } from "./AuthRouter"; | |
import { firebase } from "../firebase/firebase-config"; | |
import { login } from "../actions/Auth"; | |
import { PrivateRoute } from "./PrivateRoute"; | |
import { PublicRoute } from "./PublicRoute"; | |
import { startLoadingNotes } from "../actions/notes"; | |
export const AppRouter = () => { | |
const dispatch = useDispatch(); | |
const [checking, setChecking] = useState(true); | |
const [isLoggedIn, setIsLoggedIn] = useState(false); | |
useEffect(() => { | |
firebase.auth().onAuthStateChanged(async (user) => { | |
if (user?.uid) { | |
dispatch(login(user.uid, user.displayName)); | |
setIsLoggedIn(true); | |
dispatch(startLoadingNotes(user.uid)); | |
} else { | |
setIsLoggedIn(false); | |
} | |
setChecking(false); | |
}); | |
}, [dispatch, checking]); | |
if (checking) { | |
return <h1>Espere...</h1>; | |
} | |
return ( | |
<Router> | |
<div> | |
<Switch> | |
<PublicRoute path="/auth" component={AuthRouter} isAuthenticated={isLoggedIn} /> | |
<PrivateRoute path="/" component={JournalScreen} exact isAuthenticated={isLoggedIn} /> | |
<Redirect to="/auth/login" /> | |
</Switch> | |
</div> | |
</Router> | |
); | |
}; |
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 characters
import { Redirect, Route, Switch } from "react-router-dom"; | |
import { LoginScreen } from "../components/auth/LoginScreen"; | |
import { RegisterScreen } from "../components/auth/RegisterScreen"; | |
export const AuthRouter = () => { | |
return ( | |
<div className="auth__main"> | |
<div className="auth__box-container"> | |
<Switch> | |
<Route exact path="/auth/login" component={LoginScreen} /> | |
<Route exact path="/auth/register" component={RegisterScreen} /> | |
<Redirect exact to="/auth/login" /> | |
</Switch> | |
</div> | |
</div> | |
); | |
}; |
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 characters
import React from "react"; | |
import PropTypes from "prop-types"; | |
import { Route, Redirect } from "react-router-dom"; | |
export const PrivateRoute = ({ isAuthenticated, component: Component, ...rest }) => { | |
localStorage.setItem("lastPath", rest.location.pathname); | |
return <Route {...rest} component={(props) => (isAuthenticated ? <Component {...props} /> : <Redirect to="/login" />)} />; | |
}; | |
PrivateRoute.propTypes = { | |
isAuthenticated: PropTypes.bool.isRequired, | |
component: PropTypes.func.isRequired, | |
}; |
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 characters
import React from "react"; | |
import PropTypes from "prop-types"; | |
import { Route, Redirect } from "react-router-dom"; | |
export const PublicRoute = ({ isAuthenticated, component: Component, ...rest }) => { | |
return <Route {...rest} component={(props) => (!isAuthenticated ? <Component {...props} /> : <Redirect to="/" />)} />; | |
}; | |
PublicRoute.propTypes = { | |
isAuthenticated: PropTypes.bool.isRequired, | |
component: PropTypes.func.isRequired, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment