New way of doing auth routes
This commit is contained in:
@@ -1,48 +1,59 @@
|
||||
/* noflow */
|
||||
/* @flow */
|
||||
|
||||
import React, { Component } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
import { BrowserRouter, Switch, Route } from "react-router-dom";
|
||||
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
|
||||
|
||||
import { ConnectedLogin, ConnectedProtectedRoutes } from "./routingApp";
|
||||
import { store } from "./routingApp/store";
|
||||
|
||||
const Home = () => <h1>Home Sweet Home</h1>;
|
||||
const Help = () => <h1>Help! SOS!</h1>;
|
||||
const Alpha = () => <h1>Alpha</h1>;
|
||||
const Bravo = () => <h1>Bravo</h1>;
|
||||
const Charlie = () => <h1>Charlie</h1>;
|
||||
const Zulu = () => <h1>Zulu</h1>;
|
||||
const Error404 = () => <h1>404 Error!</h1>;
|
||||
|
||||
class App extends Component<{}> {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<BrowserRouter>
|
||||
<Switch>
|
||||
<Route path="/login" component={ConnectedLogin} />
|
||||
<Route
|
||||
path="/about/:something"
|
||||
render={props => (
|
||||
<div>
|
||||
About... {props.match.params.something}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<Route
|
||||
path="/help"
|
||||
render={() => <div>Help!</div>}
|
||||
/>
|
||||
<ConnectedProtectedRoutes>
|
||||
<div>
|
||||
<header>
|
||||
<nav>
|
||||
<Link to="/">Home</Link>
|
||||
<Link to="/about/routing">
|
||||
About Routing
|
||||
</Link>
|
||||
<Link to="/alpha">Alpha...</Link>
|
||||
<Link to="/bravo">Bravo...</Link>
|
||||
<Link to="/charlie">Charlie...</Link>
|
||||
<Link to="/wrong">...Wrong...</Link>
|
||||
<Link to="/zulu">Zulu</Link>
|
||||
<Link to="/help">Help</Link>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<Switch>
|
||||
<Route exact path="/" component={Home} />
|
||||
<Route path="/help" component={Help} />
|
||||
<Route
|
||||
exact
|
||||
path="/"
|
||||
render={() => <div>HOME SWEET HOME</div>}
|
||||
path="/about/:something"
|
||||
render={props => (
|
||||
<div>
|
||||
<h1>About...</h1>
|
||||
{props.match.params.something}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<Route
|
||||
path="/alpha"
|
||||
render={() => <div>Alpha page</div>}
|
||||
/>
|
||||
<Route
|
||||
path="/zulu"
|
||||
render={() => <div>Zulu page</div>}
|
||||
/>
|
||||
</ConnectedProtectedRoutes>
|
||||
<Route render={() => <div>404 ERROR!</div>} />
|
||||
</Switch>
|
||||
<Route path="/alpha" component={Alpha} />
|
||||
<Route path="/bravo" component={Bravo} />
|
||||
<Route path="/charlie" component={Charlie} />
|
||||
<Route path="/zulu" component={Zulu} />
|
||||
<Route component={Error404} />
|
||||
</Switch>
|
||||
</div>
|
||||
</BrowserRouter>
|
||||
</Provider>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/* noflow */
|
||||
|
||||
/* eslint-disable */
|
||||
|
||||
import React, { Component } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
|
||||
|
||||
import {
|
||||
ConnectedLogin,
|
||||
ConnectedProtectedRoutes,
|
||||
AuthRoute
|
||||
} from "./routingApp";
|
||||
import { store } from "./routingApp/store";
|
||||
|
||||
const Home = () => <h1>Home Sweet Home</h1>;
|
||||
const Help = () => <h1>Help! SOS!</h1>;
|
||||
const Alpha = () => <h1>Alpha</h1>;
|
||||
const Bravo = () => <h1>Bravo</h1>;
|
||||
const Charlie = () => <h1>Charlie</h1>;
|
||||
const Zulu = () => <h1>Zulu</h1>;
|
||||
const Error404 = () => <h1>404 Error!</h1>;
|
||||
|
||||
class App extends Component<{}> {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<BrowserRouter>
|
||||
<div>
|
||||
<header>
|
||||
<nav>
|
||||
<Link to="/">Home</Link>
|
||||
<Link to="/login">Log in</Link>
|
||||
<Link to="/about/routing">
|
||||
About Routing
|
||||
</Link>
|
||||
<Link to="/alpha">Alpha...</Link>
|
||||
<Link to="/bravo">Bravo...</Link>
|
||||
<Link to="/charlie">Charlie...</Link>
|
||||
<Link to="/wrong">...Wrong...</Link>
|
||||
<Link to="/zulu">Zulu</Link>
|
||||
<Link to="/help">Help</Link>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<Switch>
|
||||
<Route exact path="/" component={Home} />
|
||||
<Route path="/help" component={Help} />
|
||||
<Route
|
||||
path="/about/:something"
|
||||
render={props => (
|
||||
<div>
|
||||
<h1>About...</h1>
|
||||
{props.match.params.something}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<Route
|
||||
path="/login"
|
||||
component={ConnectedLogin}
|
||||
/>
|
||||
<AuthRoute path="/alpha" component={Alpha} />
|
||||
<AuthRoute path="/bravo" component={Bravo} />
|
||||
<AuthRoute
|
||||
path="/charlie"
|
||||
component={Charlie}
|
||||
/>
|
||||
<AuthRoute path="/zulu" component={Zulu} />
|
||||
<AuthRoute component={Error404} />
|
||||
</Switch>
|
||||
</div>
|
||||
</BrowserRouter>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -0,0 +1,77 @@
|
||||
/* noflow */
|
||||
|
||||
/* eslint-disable */
|
||||
|
||||
import React, { Component } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
|
||||
|
||||
import { ConnectedLogin, ConnectedProtectedRoutes } from "./routingApp";
|
||||
import { store } from "./routingApp/store";
|
||||
|
||||
const Home = () => <h1>Home Sweet Home</h1>;
|
||||
const Help = () => <h1>Help! SOS!</h1>;
|
||||
const Alpha = () => <h1>Alpha</h1>;
|
||||
const Bravo = () => <h1>Bravo</h1>;
|
||||
//const Charlie = () => <h1>Charlie</h1>;
|
||||
const Charlie = props => ((window.XXX = props), <h1>Charlie</h1>);
|
||||
const Zulu = () => <h1>Zulu</h1>;
|
||||
const Error404 = () => <h1>404 Error!</h1>;
|
||||
|
||||
class App extends Component<{}> {
|
||||
render() {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<BrowserRouter>
|
||||
<div>
|
||||
<header>
|
||||
<nav>
|
||||
<Link to="/">Home</Link>
|
||||
<Link to="/login">Log in</Link>
|
||||
<Link to="/about/routing">
|
||||
About Routing
|
||||
</Link>
|
||||
<Link to="/alpha">Alpha...</Link>
|
||||
<Link to="/bravo">Bravo...</Link>
|
||||
<Link to="/charlie">Charlie...</Link>
|
||||
<Link to="/wrong">...Wrong...</Link>
|
||||
<Link to="/zulu">Zulu</Link>
|
||||
<Link to="/help">Help</Link>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
<Switch>
|
||||
<Route exact path="/" component={Home} />
|
||||
<Route path="/help" component={Help} />
|
||||
<Route
|
||||
path="/about/:something"
|
||||
render={props => (
|
||||
<div>
|
||||
<h1>About...</h1>
|
||||
{props.match.params.something}
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
<Route
|
||||
path="/login"
|
||||
component={ConnectedLogin}
|
||||
/>
|
||||
<ConnectedProtectedRoutes>
|
||||
<Route path="/alpha" component={Alpha} />
|
||||
<Route path="/bravo" component={Bravo} />
|
||||
<Route
|
||||
path="/charlie"
|
||||
component={Charlie}
|
||||
/>
|
||||
<Route path="/zulu" component={Zulu} />
|
||||
<Route component={Error404} />
|
||||
</ConnectedProtectedRoutes>
|
||||
</Switch>
|
||||
</div>
|
||||
</BrowserRouter>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom";
|
||||
|
||||
import App from "./App.routing";
|
||||
import App from "./App.routing.protected.auth";
|
||||
import registerServiceWorker from "./registerServiceWorker";
|
||||
|
||||
ReactDOM.render(<App />, document.getElementById("root"));
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import React from "react";
|
||||
import { Route, Redirect } from "react-router-dom";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
export class Auth extends React.Component {
|
||||
render() {
|
||||
const myProps = { ...this.props };
|
||||
if (!myProps.token) {
|
||||
delete myProps.component;
|
||||
myProps.render = () => (
|
||||
<Redirect
|
||||
to={{
|
||||
pathname: this.props.loginRoute,
|
||||
state: { from: this.props.location }
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
return <Route {...myProps} />;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
/* eslint-disable */
|
||||
|
||||
import React from "react";
|
||||
import { connect } from "react-redux";
|
||||
|
||||
import { Auth } from "./authRoute.component";
|
||||
export const AuthRoute = connect(state => ({
|
||||
token: state.token,
|
||||
loginRoute: "/login"
|
||||
}))(Auth);
|
||||
@@ -2,5 +2,6 @@
|
||||
|
||||
import { ConnectedLogin } from "./login.connected.js";
|
||||
import { ConnectedProtectedRoutes } from "./protectedRoutes.connected.js";
|
||||
import { AuthRoute } from "./authroute.connected.js";
|
||||
|
||||
export { ConnectedLogin, ConnectedProtectedRoutes };
|
||||
export { ConnectedLogin, ConnectedProtectedRoutes, AuthRoute };
|
||||
|
||||
@@ -40,11 +40,13 @@ export class Login extends React.PureComponent<{
|
||||
<div>
|
||||
<h1>Login Form</h1>
|
||||
<div>
|
||||
User:{" "}
|
||||
<input type="text" onBlur={this.onUserNameBlur} />
|
||||
User:<input
|
||||
type="text"
|
||||
onBlur={this.onUserNameBlur}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
Password:{" "}
|
||||
Password:
|
||||
<input
|
||||
type="password"
|
||||
onBlur={this.onPasswordBlur}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
/* @flow */
|
||||
|
||||
import React from "react";
|
||||
import { Redirect } from "react-router-dom";
|
||||
import { Redirect, Switch } from "react-router-dom";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
export class ProtectedRoutes extends React.Component<{
|
||||
token: string,
|
||||
children: [{}],
|
||||
children: any,
|
||||
location: object
|
||||
}> {
|
||||
static propTypes = {
|
||||
token: PropTypes.string.isRequired,
|
||||
children: PropTypes.arrayOf(PropTypes.object).isRequired,
|
||||
children: PropTypes.any,
|
||||
location: PropTypes.object.isRequired
|
||||
};
|
||||
|
||||
render() {
|
||||
return this.props.token ? (
|
||||
this.props.children
|
||||
<Switch>{this.props.children}</Switch>
|
||||
) : (
|
||||
<Redirect
|
||||
to={{
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
import { createStore, applyMiddleware } from "redux";
|
||||
import thunk from "redux-thunk";
|
||||
|
||||
import { reducer } from "./login.reducer.js";
|
||||
import { reducer } from "./login.reducer";
|
||||
|
||||
export const store = createStore(reducer, applyMiddleware(thunk));
|
||||
|
||||
Reference in New Issue
Block a user