private routes and redirecting

This commit is contained in:
Delucse
2020-12-03 12:09:01 +01:00
parent 5d9bfa97af
commit 80fd112658
7 changed files with 90 additions and 31 deletions
+41
View File
@@ -0,0 +1,41 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Route, Redirect, withRouter } from 'react-router-dom';
class PrivateRoute extends Component {
render() {
return (
<Route
{...this.props.exact}
render={({ location }) =>
this.props.isAuthenticated ? (
this.props.children
) : (()=>{
return (
<Redirect
to={{
pathname: "/user/login",
state: { from: location }
}}
/>
)
})()
}
/>
);
}
}
PrivateRoute.propTypes = {
isAuthenticated: PropTypes.bool
};
const mapStateToProps = state => ({
isAuthenticated: state.auth.isAuthenticated
});
export default connect(mapStateToProps, null)(withRouter(PrivateRoute));
+70
View File
@@ -0,0 +1,70 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { visitPage } from '../../actions/generalActions';
import { Route, Switch, withRouter } from 'react-router-dom';
import PrivateRoute from './PrivateRoute';
import Home from '../Home';
import Tutorial from '../Tutorial/Tutorial';
import TutorialHome from '../Tutorial/TutorialHome';
import Builder from '../Tutorial/Builder/Builder';
import NotFound from '../NotFound';
import ProjectHome from '../Project/ProjectHome';
import Project from '../Project/Project';
import Settings from '../Settings/Settings';
import Impressum from '../Impressum';
import Privacy from '../Privacy';
import Login from '../User/Login';
class Routes extends Component {
componentDidUpdate() {
this.props.visitPage();
}
render() {
return (
<div style={{ margin: '0 22px' }}>
<Switch>
<Route path="/" exact component={Home} />
// Tutorials
<Route path="/tutorial" exact component={TutorialHome} />
<PrivateRoute path="/tutorial/builder" exact>
<Builder/>
</PrivateRoute>
<Route path="/tutorial/:tutorialId" exact component={Tutorial} />
// Sharing
<Route path="/share/:shareId" exact component={Project} />
// Gallery-Projects
<Route path="/gallery" exact component={ProjectHome} />
<Route path="/gallery/:galleryId" exact component={Project} />
// User-Projects
<PrivateRoute path="/project" exact>
<ProjectHome/>
</PrivateRoute>
<PrivateRoute path="/project/:projectId" exact>
<Project/>
</PrivateRoute>
// User
<Route path="/user/login" exact component={Login} />
// settings
<Route path="/settings" exact component={Settings} />
// privacy
<Route path="/impressum" exact component={Impressum} />
<Route path="/privacy" exact component={Privacy} />
// Not Found
<Route component={NotFound} />
</Switch>
</div>
);
}
}
Home.propTypes = {
visitPage: PropTypes.func.isRequired
};
export default connect(null, { visitPage })(withRouter(Routes));