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 User from "./components/screens/User"
|
||||||
import Login from "./components/screens/Login"
|
import Login from "./components/screens/Login"
|
||||||
import Register from "./components/screens/Register"
|
import Register from "./components/screens/Register"
|
||||||
|
import PrivateRoute from "./components/PrivateRoute"
|
||||||
|
|
||||||
import { withAuthentication } from "./components/session"
|
import { withAuthentication } from "./components/session"
|
||||||
import { withFirebase } from "./components/firebase"
|
import { withFirebase } from "./components/firebase"
|
||||||
@@ -63,7 +64,9 @@ class App extends Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { selectedTheme } = this.state
|
const { selectedTheme, authUser } = this.state
|
||||||
|
const { authUser: propAuthUser } = this.props
|
||||||
|
const authed = !!propAuthUser || !!authUser
|
||||||
|
|
||||||
const currentTheme = theme[selectedTheme]
|
const currentTheme = theme[selectedTheme]
|
||||||
return (
|
return (
|
||||||
@@ -71,13 +74,20 @@ class App extends Component {
|
|||||||
<Router>
|
<Router>
|
||||||
<Navbar toggleTheme={this.onChangeTheme} />
|
<Navbar toggleTheme={this.onChangeTheme} />
|
||||||
<RouteLayout>
|
<RouteLayout>
|
||||||
<Route path="/:year(\d+)" component={Year} exact />
|
<PrivateRoute
|
||||||
<Route
|
authed={authed}
|
||||||
|
path="/:year(\d+)"
|
||||||
|
component={Year}
|
||||||
|
exact
|
||||||
|
/>
|
||||||
|
<PrivateRoute
|
||||||
|
authed={authed}
|
||||||
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)"
|
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)"
|
||||||
component={Month}
|
component={Month}
|
||||||
exact
|
exact
|
||||||
/>
|
/>
|
||||||
<Route
|
<PrivateRoute
|
||||||
|
authed={authed}
|
||||||
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)/:day(\d+)"
|
path="/:year(\d+)/:month(0[1-9]|1[0-2]+)/:day(\d+)"
|
||||||
component={Day}
|
component={Day}
|
||||||
exact
|
exact
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import styled from "@emotion/styled"
|
import styled from "@emotion/styled"
|
||||||
import {
|
import {
|
||||||
|
ArrowRightCircle,
|
||||||
|
ArrowRight,
|
||||||
Book,
|
Book,
|
||||||
Calendar,
|
Calendar,
|
||||||
ChevronLeft,
|
ChevronLeft,
|
||||||
ChevronRight,
|
ChevronRight,
|
||||||
Circle,
|
Circle,
|
||||||
Edit2,
|
Edit2,
|
||||||
|
LogIn,
|
||||||
Moon,
|
Moon,
|
||||||
Sun,
|
Sun,
|
||||||
User,
|
User,
|
||||||
@@ -33,14 +36,22 @@ const IconBase = styled.div`
|
|||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
const Icon = ({ name, tabindex, ...rest }) => (
|
const Icon = ({ name, tabindex, label, ...rest }) => (
|
||||||
<IconBase tabIndex={tabindex} {...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 === "Book" && <Book />}
|
||||||
{name === "Calendar" && <Calendar />}
|
{name === "Calendar" && <Calendar />}
|
||||||
{name === "ChevronLeft" && <ChevronLeft />}
|
{name === "ChevronLeft" && <ChevronLeft />}
|
||||||
{name === "ChevronRight" && <ChevronRight />}
|
{name === "ChevronRight" && <ChevronRight />}
|
||||||
{name === "Circle" && <Circle />}
|
{name === "Circle" && <Circle />}
|
||||||
{name === "Edit2" && <Edit2 />}
|
{name === "Edit2" && <Edit2 />}
|
||||||
|
{name === "LogIn" && <LogIn />}
|
||||||
{name === "Moon" && <Moon />}
|
{name === "Moon" && <Moon />}
|
||||||
{name === "Sun" && <Sun />}
|
{name === "Sun" && <Sun />}
|
||||||
{name === "User" && <User />}
|
{name === "User" && <User />}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
import { StyledLink as Link } from "../elements"
|
import { StyledLink as Link } from "../elements"
|
||||||
import styled from "@emotion/styled"
|
import styled from "@emotion/styled"
|
||||||
|
/** @jsx jsx */
|
||||||
|
import { jsx, css } from "@emotion/core"
|
||||||
import { compose } from "recompose"
|
import { compose } from "recompose"
|
||||||
import { withTheme } from "emotion-theming"
|
import { withTheme } from "emotion-theming"
|
||||||
|
|
||||||
@@ -82,9 +84,11 @@ const Navbar = ({ authUser, theme, toggleTheme }) => (
|
|||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
) : (
|
) : (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<Link to={"/"}>Landing</Link>
|
{/* <Link to={"/"}>Landing</Link> */}
|
||||||
<Link to={"/login"}>Login</Link>
|
<Link to={"/login"}>
|
||||||
<Link to={"/register"}>Register</Link>
|
<Icon name="LogIn" label="Get Started" />
|
||||||
|
</Link>
|
||||||
|
{/* <Link to={"/register"}>Register</Link> */}
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
)}
|
)}
|
||||||
</NavIcons>
|
</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