feat: protected routes
This commit is contained in:
18
src/App.js
18
src/App.js
@@ -14,6 +14,7 @@ import Year from "./components/screens/Year"
|
||||
import User from "./components/screens/User"
|
||||
import Login from "./components/screens/Login"
|
||||
import Register from "./components/screens/Register"
|
||||
import PrivateRoute from "./components/PrivateRoute"
|
||||
|
||||
import { withAuthentication } from "./components/session"
|
||||
import { withFirebase } from "./components/firebase"
|
||||
@@ -63,7 +64,9 @@ class App extends Component {
|
||||
}
|
||||
|
||||
render() {
|
||||
const { selectedTheme } = this.state
|
||||
const { selectedTheme, authUser } = this.state
|
||||
const { authUser: propAuthUser } = this.props
|
||||
const authed = !!propAuthUser || !!authUser
|
||||
|
||||
const currentTheme = theme[selectedTheme]
|
||||
return (
|
||||
@@ -71,13 +74,20 @@ class App extends Component {
|
||||
<Router>
|
||||
<Navbar toggleTheme={this.onChangeTheme} />
|
||||
<RouteLayout>
|
||||
<Route path="/:year(\d+)" component={Year} exact />
|
||||
<Route
|
||||
<PrivateRoute
|
||||
authed={authed}
|
||||
path="/:year(\d+)"
|
||||
component={Year}
|
||||
exact
|
||||
/>
|
||||
<PrivateRoute
|
||||
authed={authed}
|
||||
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)"
|
||||
component={Month}
|
||||
exact
|
||||
/>
|
||||
<Route
|
||||
<PrivateRoute
|
||||
authed={authed}
|
||||
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)/:day(\d+)"
|
||||
component={Day}
|
||||
exact
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import React from "react"
|
||||
import styled from "@emotion/styled"
|
||||
import {
|
||||
ArrowRightCircle,
|
||||
ArrowRight,
|
||||
Book,
|
||||
Calendar,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
Circle,
|
||||
Edit2,
|
||||
LogIn,
|
||||
Moon,
|
||||
Sun,
|
||||
User,
|
||||
@@ -33,14 +36,22 @@ const IconBase = styled.div`
|
||||
}
|
||||
`
|
||||
|
||||
const Icon = ({ name, tabindex, ...rest }) => (
|
||||
const Icon = ({ name, tabindex, label, ...rest }) => (
|
||||
<IconBase tabIndex={tabindex} {...rest}>
|
||||
{label && (
|
||||
<span style={{ margin: "3px -3px 3px 3px", fontWeight: 700 }}>
|
||||
{label}
|
||||
</span>
|
||||
)}
|
||||
{name === "ArrowRightCircle" && <ArrowRightCircle />}
|
||||
{name === "ArrowRight" && <ArrowRight />}
|
||||
{name === "Book" && <Book />}
|
||||
{name === "Calendar" && <Calendar />}
|
||||
{name === "ChevronLeft" && <ChevronLeft />}
|
||||
{name === "ChevronRight" && <ChevronRight />}
|
||||
{name === "Circle" && <Circle />}
|
||||
{name === "Edit2" && <Edit2 />}
|
||||
{name === "LogIn" && <LogIn />}
|
||||
{name === "Moon" && <Moon />}
|
||||
{name === "Sun" && <Sun />}
|
||||
{name === "User" && <User />}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import React from "react"
|
||||
import { StyledLink as Link } from "../elements"
|
||||
import styled from "@emotion/styled"
|
||||
/** @jsx jsx */
|
||||
import { jsx, css } from "@emotion/core"
|
||||
import { compose } from "recompose"
|
||||
import { withTheme } from "emotion-theming"
|
||||
|
||||
@@ -82,9 +84,11 @@ const Navbar = ({ authUser, theme, toggleTheme }) => (
|
||||
</React.Fragment>
|
||||
) : (
|
||||
<React.Fragment>
|
||||
<Link to={"/"}>Landing</Link>
|
||||
<Link to={"/login"}>Login</Link>
|
||||
<Link to={"/register"}>Register</Link>
|
||||
{/* <Link to={"/"}>Landing</Link> */}
|
||||
<Link to={"/login"}>
|
||||
<Icon name="LogIn" label="Get Started" />
|
||||
</Link>
|
||||
{/* <Link to={"/register"}>Register</Link> */}
|
||||
</React.Fragment>
|
||||
)}
|
||||
</NavIcons>
|
||||
|
||||
21
src/components/PrivateRoute/PrivateRoute.js
Normal file
21
src/components/PrivateRoute/PrivateRoute.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import React, { Component } from "react"
|
||||
import { Route, Redirect } from "react-router-dom"
|
||||
|
||||
const PrivateRoute = ({ component: Component, authed, ...rest }) => {
|
||||
return (
|
||||
<Route
|
||||
{...rest}
|
||||
render={props =>
|
||||
authed === true ? (
|
||||
<Component {...props} />
|
||||
) : (
|
||||
<Redirect
|
||||
to={{ pathname: "/login", state: { from: props.location } }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
export default PrivateRoute
|
||||
2
src/components/PrivateRoute/index.js
Normal file
2
src/components/PrivateRoute/index.js
Normal file
@@ -0,0 +1,2 @@
|
||||
import PrivateRoute from "./PrivateRoute"
|
||||
export default PrivateRoute
|
||||
Reference in New Issue
Block a user