forked from bharvi-bissa/project-manager-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added security, protected routes | react
- Loading branch information
1 parent
f3da89f
commit fcc65d2
Showing
17 changed files
with
220 additions
and
4,032 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import axios from "axios"; | ||
|
||
const setAuthToken = token => { | ||
if (token) axios.defaults.headers.common["Authorization"] = "Bearer " + token; | ||
else delete axios.defaults.headers.common["Authorization"]; | ||
}; | ||
|
||
export default setAuthToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const isEmpty = value => | ||
value === undefined || | ||
value === null || | ||
(typeof value === "object" && Object.keys(value).length === 0) || | ||
(typeof value === "string" && value.trim().length === 0); | ||
|
||
export default isEmpty; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
project-manager-react-client/src/components/Commons/PrivateRoute.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { Route, Redirect } from "react-router-dom"; | ||
import { connect } from "react-redux"; | ||
import PropTypes from "prop-types"; | ||
|
||
const PrivateRoute = ({ component: Component, auth, ...rest }) => ( | ||
<Route | ||
{...rest} | ||
render={props => | ||
auth.isAuthenticated === true ? ( | ||
<Component {...props} /> | ||
) : ( | ||
<Redirect to="auth/login" /> | ||
) | ||
} | ||
/> | ||
); | ||
|
||
PrivateRoute.propTypes = { | ||
auth: PropTypes.object.isRequired | ||
}; | ||
|
||
const mapStateToProps = state => ({ | ||
auth: state.auth | ||
}); | ||
|
||
export default connect(mapStateToProps)(PrivateRoute); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import isEmpty from "../Validation/is-empty"; | ||
|
||
import { SET_CURRENT_USER } from "../actions/types"; | ||
|
||
const initialState = { | ||
isAuthenticated: false, | ||
user: {} | ||
}; | ||
|
||
export default function(state = initialState, action) { | ||
switch (action.type) { | ||
case SET_CURRENT_USER: | ||
return { | ||
...state, | ||
isAuthenticated: !isEmpty(action.payload), | ||
user: action.payload | ||
}; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.