Skip to content

Commit 3999bf3

Browse files
authored
variable injection -> cookies (#81)
* switched from injecting variables in templates to using cookies * fixed linter errors, removed unused code
1 parent 326b630 commit 3999bf3

File tree

9 files changed

+382
-546
lines changed

9 files changed

+382
-546
lines changed

app/components/Routes/AdminRoute.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import PropTypes from 'prop-types';
33
import cookie from 'js-cookie';
44
import { Route, Redirect } from 'react-router-dom';
55

6+
const { id, admin } = cookie.get();
7+
68
const AdminRoute = ({ component, ...rest }) => (
79
<Route
810
{...rest}
911
render={(props) => {
10-
if (!window.id) { // checks for non authenticated accounts
12+
if (!id) { // checks for non authenticated accounts
1113
cookie.set('target', props.location.pathname);
1214
}
1315
return (
14-
window.admin ? (
16+
admin ? (
1517
React.createElement(component, props)
1618
) : (
1719
<Redirect
1820
to={{
19-
pathname: window.id ? '/unauthorized' : '/login',
21+
pathname: id ? '/unauthorized' : '/login',
2022
state: { from: props.location },
2123
}}
2224
/>

app/components/Routes/PrivateRoute.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ import PropTypes from 'prop-types';
33
import cookie from 'js-cookie';
44
import { Route, Redirect } from 'react-router-dom';
55

6+
const { id } = cookie.get();
67
const PrivateRoute = ({ component, ...rest }) => (
78
<Route
89
{...rest}
910
render={(props) => {
10-
if (!window.id) { // checks for non authenticated accounts
11+
if (!id) { // checks for non authenticated accounts
1112
cookie.set('target', props.location.pathname);
1213
}
1314
return (
14-
window.id ? (
15+
id ? (
1516
React.createElement(component, props)
1617
) : (
1718
<Redirect

app/components/Template/Hamburger.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import { Link } from 'react-router-dom';
33

44
import Menus from 'react-burger-menu';
5+
import cookie from 'js-cookie';
56

67
import routes from '../../data/routes';
78

@@ -39,6 +40,7 @@ class Hamburger extends Component {
3940
}
4041

4142
render() {
43+
const { id, admin } = cookie.get();
4244
return (
4345
<div className="hamburger-container">
4446
<nav className="main" id="hambuger-nav">
@@ -55,8 +57,8 @@ class Hamburger extends Component {
5557
</Link>
5658
</li>
5759
))}
58-
{window.admin ? <li><a href="/admin"><h3>Admin</h3></a></li> : null}
59-
{window.id ? <li><a href="/logout"><h3>Logout</h3></a></li> : null}
60+
{admin ? <li><a href="/admin"><h3>Admin</h3></a></li> : null}
61+
{id ? <li><a href="/logout"><h3>Logout</h3></a></li> : null}
6062
</ul>
6163
</Menu>
6264
</div>

app/components/Template/Header.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
3+
import cookie from 'js-cookie';
34

45
import Hamburger from './Hamburger';
56
import routes from '../../data/routes';
67

8+
const { id, admin } = cookie.get();
9+
710
const Header = () => (
811
<header id="header">
912
<h1 className="index-link">
@@ -18,8 +21,8 @@ const Header = () => (
1821
<Link to={l.path}>{l.label}</Link>
1922
</li>
2023
))}
21-
{window.admin ? <li><a href="/admin">Admin</a></li> : null}
22-
{window.id ? <li><a href="/logout">Logout</a></li> : null}
24+
{admin ? <li><a href="/admin">Admin</a></li> : null}
25+
{id ? <li><a href="/logout">Logout</a></li> : null}
2326
</ul>
2427
</nav>
2528
<Hamburger />

app/layouts/Main.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import Helmet from 'react-helmet';
44

55
import ReactGA from 'react-ga';
6+
import cookie from 'js-cookie';
67

78
import Header from '../components/Template/Header';
89
import Nav from '../components/Template/Nav';
@@ -18,9 +19,10 @@ class Main extends Component {
1819

1920
componentDidMount() {
2021
if (process.env.NODE_ENV === 'production') {
22+
const { id } = cookie.get();
2123
ReactGA.set({
2224
page: window.location.pathname,
23-
userId: window.id,
25+
userId: id,
2426
});
2527
ReactGA.pageview(window.location.pathname);
2628
}

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"build": "node -r babel-register ./node_modules/webpack/bin/webpack --config ./webpack/webpack.production.config.js --progress --profile --colors && rimraf ./dist && mv ./tmp ./dist",
77
"build-dev": "rimraf ./dist && babel-node ./node_modules/webpack/bin/webpack --config ./webpack/webpack.config.js --progress --profile --colors",
88
"clean": "rimraf ./dist && rimraf ./tmp",
9-
"deploy": "yarn && npm run build && npm run forever-stop || true && npm run forever-start",
9+
"predeploy": "yarn && npm run build",
10+
"deploy": "npm run forever-stop || true && npm run forever-start",
1011
"dev": "nodemon server/server.js --ignore app/ --ignore test/ --exec babel-node",
1112
"forever-start": "forever start --minUptime 1000ms --spinSleepTime 1000ms -a -l forever.log -o out.log -e err.log -c 'nodemon server/server.js --exitcrash --exec babel-node' server/server.js",
1213
"forever-stop": "forever stop server/server.js",

server/routes/views/app.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,36 @@ const routes = (app) => {
3838

3939
app.get('/*', (req, res) => {
4040
const content = middleware.fileSystem.readFileSync(path.join(__dirname, '../../../dist/index.html'));
41-
const key = '<div id="root"></div>';
42-
const index = content.indexOf(key) + key.length;
43-
const inject = req.user ? `<script type="text/javascript">window.id="${req.user._id}";window.admin=${req.user.isAdmin};</script>` : '';
44-
res.send(content.slice(0, index) + inject + content.slice(index));
41+
42+
if (req.user) {
43+
res.cookie('id', req.user._id.toString(), { path: '/' });
44+
if (req.user.isAdmin) {
45+
res.cookie('admin', req.user.isAdmin, { path: '/' });
46+
}
47+
} else {
48+
res.clearCookie('admin', { path: '/' });
49+
res.clearCookie('id', { path: '/' });
50+
}
51+
52+
res.set('Content-Type', 'text/html');
53+
res.send(content);
4554
});
4655
} else {
4756
app.use('/dist', express.static(path.join(__dirname, '../../../dist')));
4857
const content = fs.readFileSync(path.join(__dirname, '../../../dist/index.html'), 'utf8');
49-
const key = '<div id=root></div>';
50-
const index = content.indexOf(key) + key.length;
5158

5259
app.get('/*', (req, res) => {
53-
const inject = req.user ? `<script type="text/javascript">window.id="${req.user._id}";window.admin=${req.user.isAdmin};</script>` : '';
54-
res.send(content.slice(0, index) + inject + content.slice(index));
60+
if (req.user) {
61+
res.cookie('id', req.user._id.toString(), { path: '/' });
62+
if (req.user.isAdmin) {
63+
res.cookie('admin', req.user.isAdmin, { path: '/' });
64+
}
65+
} else {
66+
res.clearCookie('admin', { path: '/' });
67+
res.clearCookie('id', { path: '/' });
68+
}
69+
res.set('Content-Type', 'text/html');
70+
res.send(content);
5571
});
5672
}
5773
};

server/routes/views/logout.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export default (req, res) => {
22
if (req.user) req.logout();
3+
res.clearCookie('admin', { path: '/' });
4+
res.clearCookie('id', { path: '/' });
5+
36
res.redirect('/');
47
};

0 commit comments

Comments
 (0)