Skip to content

Commit 1982503

Browse files
authored
Merge pull request #121 from jes708/restrict-unauth
Redirect unauthenticated users to always go to the home screen. Authe…
2 parents 6756f24 + acc596b commit 1982503

File tree

6 files changed

+86
-33
lines changed

6 files changed

+86
-33
lines changed

browser/js/actions/AuthActions.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
import axios from 'axios'
4+
import { replace } from 'react-router-redux'
45

56
export const AUTH_LOGIN_REQUEST = 'AUTH_LOGIN_REQUEST'
67
export const AUTH_LOGIN_SUCCESS = 'AUTH_LOGIN_SUCCESS'
@@ -12,6 +13,10 @@ export const AUTH_USER_REQUEST = 'AUTH_USER_REQUEST'
1213
export const AUTH_USER_RECEIVED = 'AUTH_USER_RECEIVED'
1314
export const AUTH_NO_USER = 'AUTH_NO_USER'
1415

16+
export function userReceived (user) {
17+
return { type: AUTH_USER_RECEIVED, user }
18+
}
19+
1520
export const login = (credentials) => (dispatch) => {
1621
dispatch({ type: AUTH_LOGIN_REQUEST })
1722

@@ -51,16 +56,18 @@ export const getLoggedInUser = () => (dispatch, getState) => {
5156
return axios.get('/session')
5257
.then(res => res.data)
5358
.then(resData => dispatch(userReceived(resData.user)))
54-
.catch(() => dispatch({ type: AUTH_NO_USER }))
59+
.catch(() => {
60+
dispatch({ type: AUTH_NO_USER })
61+
dispatch(replace('/'))
62+
})
5563
}
5664
}
5765

5866
export const logout = () => (dispatch) => {
5967
return axios.get('/logout')
60-
.then(() => dispatch({ type: AUTH_LOGOUT_SUCCESS }))
68+
.then(() => {
69+
dispatch({ type: AUTH_LOGOUT_SUCCESS })
70+
dispatch(replace('/'))
71+
})
6172
.catch(err => dispatch({ type: AUTH_LOGOUT_FAILURE }))
6273
}
63-
64-
export function userReceived (user) {
65-
return { type: AUTH_USER_RECEIVED, user }
66-
}

browser/js/components/GraderAssessments.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class GraderAssessments extends Component {
3535
}
3636

3737
componentWillMount () {
38-
this.props.dispatch(getUserAssessments())
38+
const { dispatch, isFetchingUser } = this.props
39+
if (!isFetchingUser) dispatch(getUserAssessments())
3940
}
4041

4142
handleSelectAssessment (assessmentId) {
@@ -171,15 +172,19 @@ class GraderAssessments extends Component {
171172
}
172173

173174
render () {
174-
return (
175-
<div style={Object.assign({}, styles.gradingPane, styles.paperStyle)}>
176-
<div style={styles.content}>
177-
{this.renderToggleFormButton()}
178-
{this.renderSearchBar()}
179-
{this.renderForm()}
175+
const { isUserFetching } = this.props
176+
177+
if (!isUserFetching) {
178+
return (
179+
<div style={Object.assign({}, styles.gradingPane, styles.paperStyle)}>
180+
<div style={styles.content}>
181+
{this.renderToggleFormButton()}
182+
{this.renderSearchBar()}
183+
{this.renderForm()}
184+
</div>
180185
</div>
181-
</div>
182-
)
186+
)
187+
} else return (<div></div>)
183188
}
184189
}
185190

@@ -196,6 +201,7 @@ const mapStateToProps = state => {
196201
const { isFetching } = assessments
197202
const { user } = session
198203
return {
204+
isFetchingUser: session.isFetching,
199205
isFetching,
200206
assessments: getAllAssessments(assessments.byId),
201207
user

browser/js/config/routes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@ import App from '../containers/App'
66
import Home from '../containers/Home'
77
// import Grade from '../containers/Grade'
88
import {AnnotatedGrade as Grade} from '../containers/Grade'
9-
import AuthForm from '../shared/AuthForm'
109
import TestAnnotate from '../components/Annotator/test.js'
1110
import TestComponent from '../components/test/TestComponent'
11+
import NotFound from '../shared/NotFound'
1212

1313
const routes = (
1414
<Route path='/' component={App}>
1515
<IndexRoute component={Home} />
1616
<Route path='grade' component={Grade} />
1717
<Route path='/test/annotate' component={TestAnnotate} />
1818
<Route path='/test/component' component={TestComponent} />
19+
<Route path='*' component={NotFound} />
1920
</Route>
2021
);
2122

2223
export default routes;
24+

browser/js/containers/Home/index.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
import React, { Component } from 'react'
3+
import React, { Component, PropTypes } from 'react'
4+
import { connect } from 'react-redux'
45
import GitHubInvert from '../../shared/GitHubInvert'
56
import Paper from 'material-ui/Paper'
67
import { blue300, blue600 } from 'material-ui/styles/colors'
@@ -51,14 +52,32 @@ const styles = {
5152
}
5253
}
5354

54-
export default function Home () {
55-
return (
56-
<div style={styles.paperStyle}>
57-
<div style={styles.header}>CodeGenius</div>
58-
<hr style={styles.hr} />
59-
<p style={styles.p}>CodeGenius optimizes the process of evaluating student code assessments<br />so you can get all of your grading done in one sitting.</p>
60-
<GitHubInvert style={styles.button} href='/auth/github' />
61-
</div>
62-
)
55+
class Home extends Component {
56+
render () {
57+
const { isFetching } = this.props
58+
59+
if (!isFetching) {
60+
return (
61+
<div style={styles.paperStyle}>
62+
<div style={styles.header}>CodeGenius</div>
63+
<hr style={styles.hr} />
64+
<p style={styles.p}>CodeGenius optimizes the process of evaluating student code assessments<br />so you can get all of your grading done in one sitting.</p>
65+
<GitHubInvert style={styles.button} href='/auth/github' />
66+
</div>
67+
)
68+
} else {
69+
return (<div style={{textAlign: 'center'}}></div>)
70+
}
71+
}
6372
}
6473

74+
const mapStateToProps = (state) => {
75+
const { session } = state
76+
77+
return {
78+
isFetching: session.isFetching
79+
}
80+
}
81+
82+
export default connect(mapStateToProps)(Home)
83+

browser/js/shared/Navbar/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import React, { Component, PropTypes } from 'react'
44
import { connect } from 'react-redux'
5+
import { push } from 'react-router-redux'
56
import getState from 'redux'
67
import { Link } from 'react-router'
78
import FlatButton from 'material-ui/FlatButton'
@@ -31,23 +32,26 @@ const styles = {
3132
}
3233

3334
const NAV_ITEMS = [
34-
{ label: 'Home', path: '/', auth: true },
35-
{ label: 'Grade', path: 'grade', auth: true },
36-
{ label: 'Members Only', path: 'membersOnly', auth: true }
35+
{ label: 'Grade', path: '/grade', auth: true }
3736
]
3837

3938
class Navbar extends Component {
4039
componentWillMount () {
41-
this.props.dispatch(getLoggedInUser())
40+
const { user, dispatch } = this.props
41+
42+
if (!user) {
43+
dispatch(getLoggedInUser())
44+
}
4245
}
4346

4447
componentWillReceiveProps (nextProps) {
45-
if (nextProps.user) this.context.router.push('/grade')
48+
const { user, pathname, dispatch } = nextProps
49+
50+
if (user && pathname === '/') dispatch(push('/grade'))
4651
}
4752

4853
handleLogout () {
4954
this.props.dispatch(logout())
50-
this.context.router.push('/')
5155
}
5256

5357
renderNavItems () {
@@ -112,9 +116,10 @@ Navbar.contextTypes = {
112116
}
113117

114118
const mapStateToProps = state => {
115-
const { session } = state
119+
const { session, routing } = state
116120
return {
117121
user: session.user,
122+
pathname: routing.locationBeforeTransitions.pathname
118123
}
119124
}
120125

browser/js/shared/NotFound.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
import React from 'react'
4+
5+
const NotFound = (props) => {
6+
return (
7+
<div style={{textAlign: 'center', marginTop: '100px'}}>
8+
<h1>404</h1>
9+
<h3>Page Not Found</h3>
10+
</div>
11+
)
12+
}
13+
14+
export default NotFound

0 commit comments

Comments
 (0)