Skip to content

Commit 9500d79

Browse files
committed
serve app from server(unsecure) and pull session id from mongo
1 parent b3f6ae2 commit 9500d79

File tree

6 files changed

+36
-17
lines changed

6 files changed

+36
-17
lines changed

main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ const createWindow = () => {
119119
});
120120

121121
// and load the index.html of the app.
122-
mainWindow.loadURL(`file://${__dirname}/build/index.html`);
122+
// now loading what the server serves, this url will need to change when/if we decide to put reactype on the web
123+
mainWindow.loadURL(`http://localhost:8080`);
123124
// load page once window is loaded
124125
mainWindow.once('ready-to-show', () => {
125126
mainWindow.show();

server/controllers/sessionController.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const sessionController = {};
33

44
// isLoggedIn finds appropriate session for this request in database, then verifies whether or not the session is still valid
55
sessionController.isLoggedIn = (req, res, next) => {
6-
// find cookie with current user's ssid value
6+
// find session from request session ID in mongodb
77
Session.findOne({ cookieId: req.cookies.ssid }, (err, session) => {
88
if (err) {
99
return next({
@@ -37,21 +37,24 @@ sessionController.startSession = (req, res, next) => {
3737
// if session doesn't exist, create a session
3838
// if valid user logged in/signed up, res.locals.id should be user's id generated from mongodb, which we will set as this session's cookieId
3939
} else if (!session) {
40-
Session.create({ cookieId: res.locals.id }, err => {
40+
Session.create({ cookieId: res.locals.id }, (err, session) => {
4141
if (err) {
4242
return next({
4343
log: `Error in sessionController.startSession create session: ${err}`,
4444
message: {
4545
err: `Error in sessionController.startSession create session, check server logs for details`
4646
}
4747
});
48+
} else {
49+
console.log('Successful startSession');
50+
res.locals.ssid = session.id;
51+
return next();
4852
}
49-
console.log('Successful startSession');
50-
return next();
5153
});
5254
// if session exists, move onto next middleware
5355
} else {
5456
console.log('Session exists, moving on');
57+
res.locals.ssid = session.id;
5558
return next();
5659
}
5760
});

server/server.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const express = require('express');
2+
const https = require('https');
3+
const fs = require('fs');
24
const mongoose = require('mongoose');
35
const path = require('path');
46
const cookieParser = require('cookie-parser');
@@ -27,10 +29,10 @@ app.use(express.json());
2729
app.use(cookieParser());
2830

2931
// statically serve everything in build folder
30-
app.use('/build', express.static(path.resolve(__dirname, '../build')));
32+
app.use('/', express.static(path.resolve(__dirname, '../build')));
3133

3234
app.get('/', (req, res) => {
33-
res.status(200).sendFile(path.resolve(__dirname, '../src/public/index.html'));
35+
res.status(200).sendFile(path.resolve(__dirname, '../build/index.html'));
3436
});
3537

3638
app.post(
@@ -39,7 +41,7 @@ app.post(
3941
cookieController.setSSIDCookie,
4042
sessionController.startSession,
4143
(req, res) => {
42-
return res.status(200).json({ userId: res.locals.id });
44+
return res.status(200).json({ sessionId: res.locals.ssid });
4345
}
4446
);
4547

@@ -49,7 +51,7 @@ app.post(
4951
cookieController.setSSIDCookie,
5052
sessionController.startSession,
5153
(req, res) => {
52-
return res.status(200).json({ userId: res.locals.id });
54+
return res.status(200).json({ sessionId: res.locals.ssid });
5355
}
5456
);
5557

@@ -72,7 +74,21 @@ app.use((err, req, res, next) => {
7274
return res.status(errorObj.status).json(errorObj.message);
7375
});
7476

75-
// starts server on PORT
77+
//starts server on PORT
7678
app.listen(PORT, () => {
77-
console.log(`Server listening on port: ${PORT}`);
78-
});
79+
console.log(`Server listening on port: ${PORT}`)
80+
})
81+
82+
// For security, if serving app from a server, it should be https, but we haven't got it working yet
83+
84+
// https
85+
// .createServer(
86+
// {
87+
// key: fs.readFileSync(path.resolve(__dirname, 'server.key')),
88+
// cert: fs.readFileSync(path.resolve(__dirname, 'server.cert'))
89+
// },
90+
// app
91+
// )
92+
// .listen(PORT, () => {
93+
// console.log(`Server listening on port: ${PORT}`);
94+
// });

src/components/login/SignIn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const SignIn: React.FC<LoginProps> = props => {
115115
} else {
116116
props.history.push('/app');
117117
alert('Login successful!');
118-
console.log('Data is', data);
118+
//console.log('Data is', data);
119119
}
120120
})
121121
.catch(err => console.log(err));

src/containers/RightContainer.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React, { Component } from 'react';
22
import { connect } from 'react-redux';
3-
import { compose } from 'redux';
43
import { withStyles, Theme } from '@material-ui/core/styles';
54
import HtmlAttr from '../components/bottom/HtmlAttr';
65
import { PropsInt, ApplicationStateInt } from '../interfaces/Interfaces';
@@ -54,7 +53,7 @@ class RightContainer extends Component<BottomTabsPropsInt> {
5453
textAlign: 'center'
5554
}}
5655
>
57-
<h3>This is the right column everyone!</h3>
56+
{/* <h3>This is the right column everyone!</h3> */}
5857
<RouteLink to={`/`} style={{ textDecoration: 'none' }}>
5958
<Button variant="contained" color="secondary">
6059
Sign Out

src/public/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
77
<!-- Original meta tag with content security policy -->
8-
<!-- <meta
8+
<meta
99
http-equiv="Content-Security-Policy"
1010
content="default-src 'self' https: filesystem: data: ws: 'unsafe-eval' 'unsafe-inline'"
11-
/> -->
11+
/>
1212
<title>ReacType</title>
1313
<link
1414
rel="stylesheet"

0 commit comments

Comments
 (0)