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