123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import React, { Component } from "react";
- import {
- BrowserRouter as Router,
- Route,
- Link,
- Redirect,
- withRouter
- } from "react-router-dom";
- ////////////////////////////////////////////////////////////
- // 1. Click the public page
- // 2. Click the protected page
- // 3. Log in
- // 4. Click the back button, note the URL each time
- function AuthExample() {
- return (
- <Router>
- <div>
- <AuthButton />
- <ul>
- <li>
- <Link to="/public">Public Page</Link>
- </li>
- <li>
- <Link to="/protected">Protected Page</Link>
- </li>
- </ul>
- <Route path="/public" component={Public} />
- <Route path="/login" component={Login} />
- <PrivateRoute path="/protected" component={Protected} />
- </div>
- </Router>
- );
- }
- const fakeAuth = {
- isAuthenticated: false,
- authenticate(cb) {
- this.isAuthenticated = true;
- setTimeout(cb, 100); // fake async
- },
- signout(cb) {
- this.isAuthenticated = false;
- setTimeout(cb, 100);
- }
- };
- const AuthButton = withRouter(
- ({ history }) =>
- fakeAuth.isAuthenticated ? (
- <p>
- Welcome!{" "}
- <button
- onClick={() => {
- fakeAuth.signout(() => history.push("/"));
- }}
- >
- Sign out
- </button>
- </p>
- ) : (
- <p>You are not logged in.</p>
- )
- );
- function PrivateRoute({ component: Component, ...rest }) {
- return (
- <Route
- {...rest}
- render={props =>
- fakeAuth.isAuthenticated ? (
- <Component {...props} />
- ) : (
- <Redirect
- to={{
- pathname: "/login",
- state: { from: props.location }
- }}
- />
- )
- }
- />
- );
- }
- function Public() {
- return <h3>Public</h3>;
- }
- function Protected() {
- return <h3>Protected</h3>;
- }
- class Login extends Component {
- state = { redirectToReferrer: false };
- login = () => {
- fakeAuth.authenticate(() => {
- this.setState({ redirectToReferrer: true });
- });
- };
- render() {
- let { from } = this.props.location.state || { from: { pathname: "/" } };
- let { redirectToReferrer } = this.state;
- if (redirectToReferrer) return <Redirect to={from} />;
- return (
- <div>
- <p>You must log in to view the page at {from.pathname}</p>
- <button onClick={this.login}>Log in</button>
- </div>
- );
- }
- }
- export default AuthExample;
|