Skip to content

Commit 861f5bc

Browse files
author
Vito Chin
committed
2 parents 3e2e995 + 27405e1 commit 861f5bc

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

package-lock.json

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App/App.jsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { HomePage } from '../HomePage';
99
import { LoginPage } from '../LoginPage';
1010
import { LogoutPage } from '../LogoutPage';
1111

12+
1213
class App extends React.Component {
1314
constructor(props) {
1415
super(props);
@@ -21,7 +22,8 @@ class App extends React.Component {
2122
}
2223

2324
render() {
24-
const { alert } = this.props;
25+
const { alert, token } = this.props;
26+
2527
return (
2628
<div className="jumbotron">
2729
<div className="container">
@@ -31,7 +33,7 @@ class App extends React.Component {
3133
}
3234
<Router history={history}>
3335
<div>
34-
<PrivateRoute exact path="/" component={HomePage} />
36+
<PrivateRoute exact path="/" component={HomePage} token={token}/>
3537
<Route path="/login" component={LoginPage} />
3638
<Route path="/logout" component={LogoutPage} />
3739
<Route path='/auth' component={() => { window.location = authUrl; return null;} }/>
@@ -45,9 +47,10 @@ class App extends React.Component {
4547
}
4648

4749
function mapStateToProps(state) {
48-
const { alert } = state;
50+
const { alert, token } = state;
4951
return {
50-
alert
52+
alert,
53+
token
5154
};
5255
}
5356

src/_components/PrivateRoute.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22
import { Route, Redirect } from 'react-router-dom';
33

4-
export const PrivateRoute = ({ component: Component, ...rest }) => (
4+
export const PrivateRoute = ({ component: Component, token, ...rest }) => (
55
<Route {...rest} render={props => (
6-
localStorage.getItem('user')
6+
(token !== null)
77
? <Component {...props} />
88
: <Redirect to={'/auth'} />
99
)} />

src/_constants/user.constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export const userConstants = {
88
GETALL_REQUEST: 'USERS_GETALL_REQUEST',
99
GETALL_SUCCESS: 'USERS_GETALL_SUCCESS',
1010
GETALL_FAILURE: 'USERS_GETALL_FAILURE',
11+
12+
SET_TOKEN: 'SET_TOKEN'
1113
};

src/_reducers/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { combineReducers } from 'redux';
33
import { authentication } from './authentication.reducer';
44
import { users } from './users.reducer';
55
import { alert } from './alert.reducer';
6+
import { token } from './token.reducer';
67

78
const rootReducer = combineReducers({
89
authentication,
910
users,
10-
alert
11+
alert,
12+
token
1113
});
1214

1315
export default rootReducer;

src/_reducers/token.reducer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { userConstants } from '../_constants';
2+
3+
export function token(state = null, action) {
4+
switch (action.type) {
5+
case userConstants.SET_TOKEN:
6+
return action.token;
7+
default:
8+
return state;
9+
}
10+
}

0 commit comments

Comments
 (0)