Skip to content

Commit

Permalink
handling auth redirect with cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
mldangelo committed Apr 3, 2017
1 parent bc4dd87 commit b5efd73
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 56 deletions.
30 changes: 18 additions & 12 deletions app/components/Routes/AdminRoute.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import React, { PropTypes } from 'react';
import cookie from 'react-cookie';
import { Route, Redirect } from 'react-router-dom';

const AdminRoute = ({ component, ...rest }) => (
<Route
{...rest} render={props => (
window.admin ? (
React.createElement(component, props)
) : (
<Redirect
to={{
pathname: window.id ? '/unauthorized' : '/login',
state: { from: props.location },
}}
/>
)
)}
{...rest} render={(props) => {
if (!window.id) { // checks for non authenticated accounts
cookie.save('target', props.location.pathname, { path: '/' });
}
return (
window.admin ? (
React.createElement(component, props)
) : (
<Redirect
to={{
pathname: window.id ? '/unauthorized' : '/login',
state: { from: props.location },
}}
/>
)
);
}}
/>
);

Expand Down
30 changes: 18 additions & 12 deletions app/components/Routes/PrivateRoute.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import React, { PropTypes } from 'react';
import cookie from 'react-cookie';
import { Route, Redirect } from 'react-router-dom';

const PrivateRoute = ({ component, ...rest }) => (
<Route
{...rest} render={props => (
window.id ? (
React.createElement(component, props)
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location },
}}
/>
)
)}
{...rest} render={(props) => {
if (!window.id) { // checks for non authenticated accounts
cookie.save('target', props.location.pathname, { path: '/' });
}
return (
window.id ? (
React.createElement(component, props)
) : (
<Redirect
to={{
pathname: '/login',
state: { from: props.location },
}}
/>
)
);
}}
/>
);

Expand Down
2 changes: 1 addition & 1 deletion app/data/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const data = [
{ /* find . | grep ".js" | grep -vE ".min.js|node_modules|.git|.json" |
xargs -I file cat file | wc -l */
label: 'Lines of Javascript powering this website',
value: '2531',
value: '2543',
link: 'https://github.com/mldangelo/mldangelo/graphs/contributors',
},
];
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"express": "^4.15.2",
"express-session": "^1.15.2",
"extract-text-webpack-plugin": "^2.1.0",
"file-loader": "^0.11.0",
"file-loader": "^0.11.1",
"forever": "^0.15.3",
"github": "9.2.0",
"html-minify-loader": "^1.1.0",
"html-webpack-plugin": "^2.28.0",
"last.fm.api": "^0.1.3",
"moment": "^2.18.1",
"mongoose": "^4.9.2",
"mongoose": "^4.9.3",
"morgan": "^1.8.1",
"node-sass": "^4.5.2",
"nodemon": "^1.11.0",
Expand All @@ -54,6 +54,7 @@
"raw-loader": "^0.5.1",
"react": "^15.4.2",
"react-burger-menu": "^1.10.14",
"react-cookie": "^1.0.5",
"react-dom": "^15.4.2",
"react-ga": "^2.1.2",
"react-helmet": "^5.0.2",
Expand All @@ -69,7 +70,7 @@
"url-loader": "^0.5.8",
"webpack": "^2.3.2",
"webpack-dev-middleware": "^1.10.1",
"webpack-hot-middleware": "^2.17.1"
"webpack-hot-middleware": "^2.18.0"
},
"devDependencies": {
"babel-eslint": "^7.2.1",
Expand Down
7 changes: 3 additions & 4 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ const routes = (app) => {
app.get('/login/google/return', passport.authenticate('google', {
failureRedirect: '/login',
}), (req, res) => {
if (req.user && req.user.isAdmin) {
return res.redirect('/admin'); // redirect to admin dash for admin accounts
}
return res.redirect('/resume'); // the only other protected page. this works for now
const target = req.cookies.target || '/';
res.clearCookie('target', { path: '/' });
return res.redirect(target);
});

app.get('/logout', require('./views/logout'));
Expand Down
49 changes: 25 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ aws4@^1.2.1:
version "1.6.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e"

axios@^0.15.3:
version "0.15.3"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.15.3.tgz#2c9d638b2e191a08ea1d6cc988eadd6ba5bdc053"
axios@^0.16.0:
version "0.16.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.16.0.tgz#6ed9771d815f429e7510f2838262957c4953d3b6"
dependencies:
follow-redirects "1.0.0"

Expand Down Expand Up @@ -1530,7 +1530,7 @@ [email protected]:
version "1.0.6"
resolved "https://registry.yarnpkg.com/cookie-signature/-/cookie-signature-1.0.6.tgz#e303a882b342cc3ee8ca513a79999734dab3ae2c"

[email protected]:
[email protected], cookie@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.3.1.tgz#e7e0a1f9ef43b4c8ba925c5c5a96e806d16873bb"

Expand Down Expand Up @@ -2275,9 +2275,9 @@ eslint-plugin-react@^6.10.3:
jsx-ast-utils "^1.3.4"
object.assign "^4.0.4"

eslint@^3.18.0:
version "3.18.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.18.0.tgz#647e985c4ae71502d20ac62c109f66d5104c8a4b"
eslint@^3.19.0:
version "3.19.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.19.0.tgz#c8fc6201c7f40dd08941b87c085767386a679acc"
dependencies:
babel-code-frame "^6.16.0"
chalk "^1.1.3"
Expand Down Expand Up @@ -2326,10 +2326,6 @@ esprima@^2.6.0:
version "2.7.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"

esprima@^3.1.1:
version "3.1.3"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"

esprima@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-1.0.4.tgz#9f557e08fc3b4d26ece9dd34f8fbf476b62585ad"
Expand Down Expand Up @@ -2557,9 +2553,9 @@ file-entry-cache@^2.0.0:
flat-cache "^1.2.1"
object-assign "^4.0.1"

file-loader@^0.10.1:
version "0.10.1"
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-0.10.1.tgz#815034119891fc6441fb5a64c11bc93c22ddd842"
file-loader@^0.11.0:
version "0.11.1"
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-0.11.1.tgz#6b328ee1234a729e4e47d36375dd6d35c0e1db84"
dependencies:
loader-utils "^1.0.2"

Expand Down Expand Up @@ -3283,6 +3279,10 @@ is-my-json-valid@^2.10.0:
jsonpointer "^4.0.0"
xtend "^4.0.0"

is-node@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-node/-/is-node-1.0.2.tgz#d7d002745ef7debbb7477e988956ab0a4fccb653"

is-npm@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4"
Expand Down Expand Up @@ -3416,14 +3416,7 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"

js-yaml@^3.5.1:
version "3.8.2"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.2.tgz#02d3e2c0f6beab20248d412c352203827d786721"
dependencies:
argparse "^1.0.7"
esprima "^3.1.1"

js-yaml@~3.7.0:
js-yaml@^3.5.1, js-yaml@~3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80"
dependencies:
Expand Down Expand Up @@ -5061,6 +5054,14 @@ react-burger-menu@^1.10.14:
radium "^0.18.1"
snapsvg-cjs "0.0.4"

react-cookie@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/react-cookie/-/react-cookie-1.0.5.tgz#234190bd55ddfea361444a89c873077ab6abf651"
dependencies:
cookie "^0.3.1"
is-node "^1.0.2"
object-assign "^4.1.0"

react-dom@^15.4.2:
version "15.4.2"
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.4.2.tgz#015363f05b0a1fd52ae9efdd3a0060d90695208f"
Expand Down Expand Up @@ -5493,11 +5494,11 @@ semver-diff@^2.0.0:
dependencies:
semver "^5.0.3"

"semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.3.0, semver@~5.3.0:
"semver@2 || 3 || 4 || 5", semver@^5.0.3, semver@^5.1.0, semver@^5.3.0, semver@~5.3.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"

semver@^5.0.3, semver@~5.0.1:
semver@~5.0.1:
version "5.0.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.0.3.tgz#77466de589cd5d3c95f138aa78bc569a3cb5d27a"

Expand Down

0 comments on commit b5efd73

Please sign in to comment.