Skip to content

Commit b5efd73

Browse files
committed
handling auth redirect with cookies
1 parent bc4dd87 commit b5efd73

File tree

6 files changed

+69
-56
lines changed

6 files changed

+69
-56
lines changed

app/components/Routes/AdminRoute.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import React, { PropTypes } from 'react';
2+
import cookie from 'react-cookie';
23
import { Route, Redirect } from 'react-router-dom';
34

45
const AdminRoute = ({ component, ...rest }) => (
56
<Route
6-
{...rest} render={props => (
7-
window.admin ? (
8-
React.createElement(component, props)
9-
) : (
10-
<Redirect
11-
to={{
12-
pathname: window.id ? '/unauthorized' : '/login',
13-
state: { from: props.location },
14-
}}
15-
/>
16-
)
17-
)}
7+
{...rest} render={(props) => {
8+
if (!window.id) { // checks for non authenticated accounts
9+
cookie.save('target', props.location.pathname, { path: '/' });
10+
}
11+
return (
12+
window.admin ? (
13+
React.createElement(component, props)
14+
) : (
15+
<Redirect
16+
to={{
17+
pathname: window.id ? '/unauthorized' : '/login',
18+
state: { from: props.location },
19+
}}
20+
/>
21+
)
22+
);
23+
}}
1824
/>
1925
);
2026

app/components/Routes/PrivateRoute.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import React, { PropTypes } from 'react';
2+
import cookie from 'react-cookie';
23
import { Route, Redirect } from 'react-router-dom';
34

45
const PrivateRoute = ({ component, ...rest }) => (
56
<Route
6-
{...rest} render={props => (
7-
window.id ? (
8-
React.createElement(component, props)
9-
) : (
10-
<Redirect
11-
to={{
12-
pathname: '/login',
13-
state: { from: props.location },
14-
}}
15-
/>
16-
)
17-
)}
7+
{...rest} render={(props) => {
8+
if (!window.id) { // checks for non authenticated accounts
9+
cookie.save('target', props.location.pathname, { path: '/' });
10+
}
11+
return (
12+
window.id ? (
13+
React.createElement(component, props)
14+
) : (
15+
<Redirect
16+
to={{
17+
pathname: '/login',
18+
state: { from: props.location },
19+
}}
20+
/>
21+
)
22+
);
23+
}}
1824
/>
1925
);
2026

app/data/github.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const data = [
4949
{ /* find . | grep ".js" | grep -vE ".min.js|node_modules|.git|.json" |
5050
xargs -I file cat file | wc -l */
5151
label: 'Lines of Javascript powering this website',
52-
value: '2531',
52+
value: '2543',
5353
link: 'https://github.com/mldangelo/mldangelo/graphs/contributors',
5454
},
5555
];

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
"express": "^4.15.2",
3838
"express-session": "^1.15.2",
3939
"extract-text-webpack-plugin": "^2.1.0",
40-
"file-loader": "^0.11.0",
40+
"file-loader": "^0.11.1",
4141
"forever": "^0.15.3",
4242
"github": "9.2.0",
4343
"html-minify-loader": "^1.1.0",
4444
"html-webpack-plugin": "^2.28.0",
4545
"last.fm.api": "^0.1.3",
4646
"moment": "^2.18.1",
47-
"mongoose": "^4.9.2",
47+
"mongoose": "^4.9.3",
4848
"morgan": "^1.8.1",
4949
"node-sass": "^4.5.2",
5050
"nodemon": "^1.11.0",
@@ -54,6 +54,7 @@
5454
"raw-loader": "^0.5.1",
5555
"react": "^15.4.2",
5656
"react-burger-menu": "^1.10.14",
57+
"react-cookie": "^1.0.5",
5758
"react-dom": "^15.4.2",
5859
"react-ga": "^2.1.2",
5960
"react-helmet": "^5.0.2",
@@ -69,7 +70,7 @@
6970
"url-loader": "^0.5.8",
7071
"webpack": "^2.3.2",
7172
"webpack-dev-middleware": "^1.10.1",
72-
"webpack-hot-middleware": "^2.17.1"
73+
"webpack-hot-middleware": "^2.18.0"
7374
},
7475
"devDependencies": {
7576
"babel-eslint": "^7.2.1",

server/routes/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ const routes = (app) => {
1010
app.get('/login/google/return', passport.authenticate('google', {
1111
failureRedirect: '/login',
1212
}), (req, res) => {
13-
if (req.user && req.user.isAdmin) {
14-
return res.redirect('/admin'); // redirect to admin dash for admin accounts
15-
}
16-
return res.redirect('/resume'); // the only other protected page. this works for now
13+
const target = req.cookies.target || '/';
14+
res.clearCookie('target', { path: '/' });
15+
return res.redirect(target);
1716
});
1817

1918
app.get('/logout', require('./views/logout'));

yarn.lock

+25-24
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ aws4@^1.2.1:
269269
version "1.6.0"
270270
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.6.0.tgz#83ef5ca860b2b32e4a0deedee8c771b9db57471e"
271271

272-
axios@^0.15.3:
273-
version "0.15.3"
274-
resolved "https://registry.yarnpkg.com/axios/-/axios-0.15.3.tgz#2c9d638b2e191a08ea1d6cc988eadd6ba5bdc053"
272+
axios@^0.16.0:
273+
version "0.16.0"
274+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.16.0.tgz#6ed9771d815f429e7510f2838262957c4953d3b6"
275275
dependencies:
276276
follow-redirects "1.0.0"
277277

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

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

@@ -2275,9 +2275,9 @@ eslint-plugin-react@^6.10.3:
22752275
jsx-ast-utils "^1.3.4"
22762276
object.assign "^4.0.4"
22772277

2278-
eslint@^3.18.0:
2279-
version "3.18.0"
2280-
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.18.0.tgz#647e985c4ae71502d20ac62c109f66d5104c8a4b"
2278+
eslint@^3.19.0:
2279+
version "3.19.0"
2280+
resolved "https://registry.yarnpkg.com/eslint/-/eslint-3.19.0.tgz#c8fc6201c7f40dd08941b87c085767386a679acc"
22812281
dependencies:
22822282
babel-code-frame "^6.16.0"
22832283
chalk "^1.1.3"
@@ -2326,10 +2326,6 @@ esprima@^2.6.0:
23262326
version "2.7.3"
23272327
resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581"
23282328

2329-
esprima@^3.1.1:
2330-
version "3.1.3"
2331-
resolved "https://registry.yarnpkg.com/esprima/-/esprima-3.1.3.tgz#fdca51cee6133895e3c88d535ce49dbff62a4633"
2332-
23332329
esprima@~1.0.4:
23342330
version "1.0.4"
23352331
resolved "https://registry.yarnpkg.com/esprima/-/esprima-1.0.4.tgz#9f557e08fc3b4d26ece9dd34f8fbf476b62585ad"
@@ -2557,9 +2553,9 @@ file-entry-cache@^2.0.0:
25572553
flat-cache "^1.2.1"
25582554
object-assign "^4.0.1"
25592555

2560-
file-loader@^0.10.1:
2561-
version "0.10.1"
2562-
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-0.10.1.tgz#815034119891fc6441fb5a64c11bc93c22ddd842"
2556+
file-loader@^0.11.0:
2557+
version "0.11.1"
2558+
resolved "https://registry.yarnpkg.com/file-loader/-/file-loader-0.11.1.tgz#6b328ee1234a729e4e47d36375dd6d35c0e1db84"
25632559
dependencies:
25642560
loader-utils "^1.0.2"
25652561

@@ -3283,6 +3279,10 @@ is-my-json-valid@^2.10.0:
32833279
jsonpointer "^4.0.0"
32843280
xtend "^4.0.0"
32853281

3282+
is-node@^1.0.2:
3283+
version "1.0.2"
3284+
resolved "https://registry.yarnpkg.com/is-node/-/is-node-1.0.2.tgz#d7d002745ef7debbb7477e988956ab0a4fccb653"
3285+
32863286
is-npm@^1.0.0:
32873287
version "1.0.0"
32883288
resolved "https://registry.yarnpkg.com/is-npm/-/is-npm-1.0.0.tgz#f2fb63a65e4905b406c86072765a1a4dc793b9f4"
@@ -3416,14 +3416,7 @@ js-tokens@^3.0.0:
34163416
version "3.0.1"
34173417
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"
34183418

3419-
js-yaml@^3.5.1:
3420-
version "3.8.2"
3421-
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.8.2.tgz#02d3e2c0f6beab20248d412c352203827d786721"
3422-
dependencies:
3423-
argparse "^1.0.7"
3424-
esprima "^3.1.1"
3425-
3426-
js-yaml@~3.7.0:
3419+
js-yaml@^3.5.1, js-yaml@~3.7.0:
34273420
version "3.7.0"
34283421
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80"
34293422
dependencies:
@@ -5061,6 +5054,14 @@ react-burger-menu@^1.10.14:
50615054
radium "^0.18.1"
50625055
snapsvg-cjs "0.0.4"
50635056

5057+
react-cookie@^1.0.5:
5058+
version "1.0.5"
5059+
resolved "https://registry.yarnpkg.com/react-cookie/-/react-cookie-1.0.5.tgz#234190bd55ddfea361444a89c873077ab6abf651"
5060+
dependencies:
5061+
cookie "^0.3.1"
5062+
is-node "^1.0.2"
5063+
object-assign "^4.1.0"
5064+
50645065
react-dom@^15.4.2:
50655066
version "15.4.2"
50665067
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.4.2.tgz#015363f05b0a1fd52ae9efdd3a0060d90695208f"
@@ -5493,11 +5494,11 @@ semver-diff@^2.0.0:
54935494
dependencies:
54945495
semver "^5.0.3"
54955496

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

5500-
semver@^5.0.3, semver@~5.0.1:
5501+
semver@~5.0.1:
55015502
version "5.0.3"
55025503
resolved "https://registry.yarnpkg.com/semver/-/semver-5.0.3.tgz#77466de589cd5d3c95f138aa78bc569a3cb5d27a"
55035504

0 commit comments

Comments
 (0)