Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Redirect user to login page if he is not authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrohee committed Mar 30, 2017
1 parent e83457e commit 9293000
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
37 changes: 37 additions & 0 deletions client/src/components/login/EnsureIsAuthenticatedContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { PropTypes } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { browserHistory } from 'react-router'

class EnsureIsAuthenticatedContainer extends React.Component {
componentDidMount() {
const { currentURL, isUserAuthenticated } = this.props

if (!isUserAuthenticated) {
browserHistory.replace("/login")
}
}

render() {
if (this.props.isUserAuthenticated) {
return this.props.children
} else {
return null
}
}
}

function mapStateToProps(state, ownProps) {
return {
isUserAuthenticated: state.user.isAuthenticated,
currentURL: ownProps.location.pathname,
}
}

EnsureIsAuthenticatedContainer.propTypes = {
isUserAuthenticated: PropTypes.bool.isRequired,
currentURL: PropTypes.string.isRequired,
children: PropTypes.object.isRequired,
}

export default connect(mapStateToProps)(EnsureIsAuthenticatedContainer)
17 changes: 10 additions & 7 deletions client/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ import CommitteeAnswerPage from './components/pepite/Applicant/CommitteeAnswer/C
import ApplicantPage from './components/pepite/Applicant/ApplicantPage'
import CguPage from './components/cgu/CguPage'
import NotFound from './components/error/NotFound'
import EnsureIsAuthenticatedContainer from './components/login/EnsureIsAuthenticatedContainer'

export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="application/:id/print" component={PrintPage}/>
<Route path="application(/:id)" component={ApplicationPage}/>
<Route path="pepite" component={PepiteHomePage}/>
<Route path="pepite/applicant" component={ApplicantPage}/>
<Route path="pepite/committeeAnswer/:id" component={CommitteeAnswerPage}/>
<Route path="login" component={LoginPage}/>
<Route path="cgu" component={CguPage}/>
<Route path="application/:id/print" component={PrintPage} />
<Route path="application(/:id)" component={ApplicationPage} />
<Route path="login" component={LoginPage} />
<Route path="cgu" component={CguPage} />
<Route component={EnsureIsAuthenticatedContainer}>
<Route path="pepite" component={PepiteHomePage} />
<Route path="pepite/applicant" component={ApplicantPage} />
<Route path="pepite/committeeAnswer/:id" component={CommitteeAnswerPage} />
</Route>
<Route path="*" component={NotFound} />
</Route>
)

0 comments on commit 9293000

Please sign in to comment.