Skip to content

Commit c455516

Browse files
committed
commit before new branch
1 parent 090b3fe commit c455516

File tree

9 files changed

+97
-93
lines changed

9 files changed

+97
-93
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const { Projects } = require('../models/reactypeModels');
2+
const projectController = {};
3+
4+
// saveProject saves current workspace to database
5+
6+
projectController.saveProject = (req, res, next) => {
7+
console.log('Inside projectController.saveProject...');
8+
// pull project name and project itself from body
9+
const { name, project } = req.body;
10+
// pull ssid from cookies for user id
11+
const userId = req.cookies.ssid;
12+
Projects.findOneAndUpdate(
13+
{ name, userId },
14+
// pushes the saved project into projects array of project
15+
{ project },
16+
// this options returns as result the new project that was saved, otherwise result would be the projects array before it was updated
17+
{ upsert: true, new: true },
18+
(err, result) => {
19+
if (err) {
20+
return next({
21+
log: `Error in projectController.saveProject: ${err}`,
22+
message: {
23+
err: `Error in projectController.saveProject, check server logs for details`
24+
}
25+
});
26+
} else {
27+
res.locals.savedProject = result;
28+
console.log('Successful saveProject');
29+
return next();
30+
}
31+
}
32+
);
33+
};
34+
35+
// gets all of current project's projects
36+
37+
projectController.getProjects = (req, res, next) => {
38+
console.log('Inside projectController.getProjects...');
39+
const userId = req.cookies.ssid;
40+
Projects.find({ userId }, (err, projects) => {
41+
if (err) {
42+
return next({
43+
log: `Error in projectController.getProjects: ${err}`,
44+
message: {
45+
err: `Error in projectController.getProjects, check server logs for details`
46+
}
47+
});
48+
} else {
49+
console.log('Successful getProjects');
50+
res.locals.projects = projects;
51+
return next();
52+
}
53+
});
54+
};
55+
56+
module.exports = projectController;

server/controllers/sessionController.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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+
console.log('Inside isLoggedIn');
7+
console.log('req.cookies is', req.cookies);
68
// find session from request session ID in mongodb
79
Sessions.findOne({ cookieId: req.cookies.ssid }, (err, session) => {
810
if (err) {
@@ -14,9 +16,11 @@ sessionController.isLoggedIn = (req, res, next) => {
1416
});
1517
// no session found, redirect to signup page
1618
} else if (!session) {
19+
console.log('No session found, redirecting to signup page');
1720
return res.redirect('/signup');
1821
} else {
1922
// session found, move onto next middleware
23+
console.log('Session found, moving onto next middleware');
2024
return next();
2125
}
2226
});

server/controllers/userController.js

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ userController.createUser = (req, res, next) => {
99
console.log('Creating user...');
1010
const { email, username, password } = req.body;
1111
// error handling if username or password is missing
12+
// TODO make this more vague for security purposes
1213
if (!username) {
1314
return res.status(400).json('No username input');
1415
}
@@ -20,7 +21,7 @@ userController.createUser = (req, res, next) => {
2021
}
2122

2223
// create user using username and password
23-
Users.create({ email, username, password, projects: [] }, (err, newUser) => {
24+
Users.create({ username, password, email }, (err, newUser) => {
2425
if (err) {
2526
return next({
2627
log: `Error in userController.createUser: ${err}`,
@@ -76,55 +77,4 @@ userController.verifyUser = (req, res, next) => {
7677
});
7778
};
7879

79-
// saveProject saves current workspace to database
80-
81-
userController.saveProject = (req, res, next) => {
82-
console.log('Inside userController.saveProject...');
83-
const project = req.body;
84-
// pull user id from cookie to find current user in db
85-
const _id = req.cookies.ssid;
86-
Users.findByIdAndUpdate(
87-
{ _id },
88-
// pushes the saved project into projects array of user
89-
{ $push: { projects: project } },
90-
// this options returns as result the new project that was saved, otherwise result would be the projects array before it was updated
91-
{ new: true },
92-
(err, result) => {
93-
if (err) {
94-
return next({
95-
log: `Error in userController.saveProject: ${err}`,
96-
message: {
97-
err: `Error in userController.saveProject, check server logs for details`
98-
}
99-
});
100-
} else {
101-
res.locals.savedProject = result.projects[result.projects.length - 1];
102-
console.log('Successful saveProject');
103-
return next();
104-
}
105-
}
106-
);
107-
};
108-
109-
// gets all of current user's projects
110-
111-
userController.getProjects = (req, res, next) => {
112-
console.log('Inside userController.getProjects...');
113-
const _id = req.cookies.ssid;
114-
Users.findOne({ _id }, (err, user) => {
115-
if (err) {
116-
return next({
117-
log: `Error in userController.getProjects: ${err}`,
118-
message: {
119-
err: `Error in userController.getProjects, check server logs for details`
120-
}
121-
});
122-
} else {
123-
console.log('Successful getProjects');
124-
res.locals.projects = user.projects;
125-
return next();
126-
}
127-
});
128-
};
129-
13080
module.exports = userController;

server/models/reactypeModels.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@ const Schema = mongoose.Schema;
2121
const userSchema = new Schema({
2222
username: { type: String, required: true, unique: true },
2323
email: { type: String, required: false, unique: true },
24-
password: { type: String, required: true },
25-
projects: [
26-
{
27-
name: String,
28-
project: { type: Object }
29-
}
30-
]
24+
password: { type: String, required: true }
3125
});
3226

3327
// salt will go through 10 rounds of hashing
@@ -57,7 +51,18 @@ const sessionSchema = new Schema({
5751
createdAt: { type: Date, expires: 3600, default: Date.now }
5852
});
5953

54+
const projectSchema = new Schema({
55+
name: { type: String, required: true, unique: true },
56+
project: Object,
57+
userId: {
58+
type: Schema.Types.ObjectId,
59+
ref: 'Users'
60+
},
61+
createdAt: { type: Date, default: Date.now }
62+
});
63+
6064
const Users = mongoose.model('Users', userSchema);
6165
const Sessions = mongoose.model('Sessions', sessionSchema);
66+
const Projects = mongoose.model('Projects', projectSchema);
6267

63-
module.exports = { Users, Sessions };
68+
module.exports = { Users, Sessions, Projects };

server/server.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const cookieParser = require('cookie-parser');
88
const userController = require('./controllers/userController');
99
const cookieController = require('./controllers/cookieController');
1010
const sessionController = require('./controllers/sessionController');
11+
const projectController = require('./controllers/projectController');
1112
const app = express();
1213

1314
const PORT = 8080;
@@ -69,7 +70,7 @@ app.post(
6970
app.post(
7071
'/saveProject',
7172
sessionController.isLoggedIn,
72-
userController.saveProject,
73+
projectController.saveProject,
7374
(req, res) => {
7475
return res.status(200).json(res.locals.savedProject);
7576
}
@@ -78,7 +79,7 @@ app.post(
7879
app.get(
7980
'/getProjects',
8081
sessionController.isLoggedIn,
81-
userController.getProjects,
82+
projectController.getProjects,
8283
(req, res) => {
8384
return res.status(200).json(res.locals.projects);
8485
}
@@ -105,8 +106,8 @@ app.use((err, req, res, next) => {
105106

106107
//starts server on PORT
107108
// app.listen(PORT, () => {
108-
// console.log(`Server listening on port: ${PORT}`)
109-
// })
109+
// console.log(`Server listening on port: ${PORT}`);
110+
// });
110111

111112
// For security, if serving app from a server, it should be https
112113
// https working now, but still needs nodeIntegration on to work <- Electron throws security issue warning because of this

src/components/login/SignIn.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useHistory,
99
RouteComponentProps
1010
} from 'react-router-dom';
11-
import { sessionIsCreated, githubOauth } from '../../helperFunctions/auth';
11+
import { sessionIsCreated } from '../../helperFunctions/auth';
1212

1313
import Avatar from '@material-ui/core/Avatar';
1414
import Button from '@material-ui/core/Button';
@@ -144,7 +144,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
144144
Sign In
145145
</Button>
146146

147-
<a href="https://localhost:8080/github">
147+
<a href="/github">
148148
<img src="/images/githublogin.png" />
149149
</a>
150150
<br></br>

src/components/login/SignUp.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const SignUp: React.FC<LoginInt & RouteComponentProps> = props => {
8383

8484
const handleSignUp = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
8585
e.preventDefault();
86-
console.log('click fired on handleLogin');
86+
console.log('click fired on handleSignup');
8787
newUserIsCreated(username, email, password).then(userCreated => {
8888
if (userCreated) {
8989
console.log('user created');

src/containers/RightContainer.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component, useState } from 'react';
1+
import React, { Component, useState, useEffect } from 'react';
22
import { connect } from 'react-redux';
33
import { withStyles, Theme } from '@material-ui/core/styles';
44
import HtmlAttr from '../components/bottom/HtmlAttr';
@@ -37,7 +37,7 @@ function saveProject(project: ApplicationStateInt, name: String) {
3737
console.log('Saving project to DB...');
3838
const body = JSON.stringify({ name, project });
3939
console.log('Project name is', name);
40-
fetch('https://localhost:8080/saveProject', {
40+
fetch('/saveProject', {
4141
method: 'POST',
4242
headers: {
4343
'content-type': 'application/json'
@@ -52,14 +52,22 @@ function saveProject(project: ApplicationStateInt, name: String) {
5252

5353
function getProjects() {
5454
console.log("Loading user's projects...");
55-
fetch('https://localhost:8080/getProjects', {
55+
fetch('/getProjects', {
5656
credentials: 'include'
5757
})
5858
.then(res => res.json())
59-
.then(data => console.log("User's projects are", data))
59+
.then(data => {
60+
console.log("User's projects are", data);
61+
window.localStorage.setItem('Projects', data);
62+
})
6063
.catch(err => console.log(err));
6164
}
65+
6266
const RightContainer = (props: BottomTabsPropsInt) => {
67+
// useEffect(() => {
68+
// getProjects();
69+
// });
70+
6371
const { classes, components, focusComponent, focusChild } = props;
6472
// looks through the children of currently focused component (based on left bar) and finds the number of child html elements it has
6573
const htmlAttribCount = focusComponent.childrenArray.filter(

src/helperFunctions/auth.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const sessionIsCreated = (
66
username,
77
password
88
});
9-
const result = fetch('https://localhost:8080/login', {
9+
const result = fetch('/login', {
1010
method: 'POST',
1111
credentials: 'include',
1212
headers: {
@@ -42,7 +42,7 @@ export const newUserIsCreated = (
4242
email,
4343
password
4444
});
45-
const result = fetch('https://localhost:8080/signup', {
45+
const result = fetch('/signup', {
4646
method: 'POST',
4747
credentials: 'include',
4848
headers: {
@@ -67,23 +67,3 @@ export const newUserIsCreated = (
6767
});
6868
return result;
6969
};
70-
71-
export const githubOauth = (): Promise<boolean> => {
72-
const result = fetch('https://github.com/login/oauth/authorize', {
73-
method: 'GET',
74-
mode: 'no-cors',
75-
params: {
76-
client_id: process.env.GITHUB_ID
77-
}
78-
})
79-
.then(res => res.json())
80-
.then(data => {
81-
console.log('Data from github oauth', data);
82-
return true;
83-
})
84-
.catch(err => {
85-
console.log('Error from github oauth', err);
86-
return false;
87-
});
88-
return result;
89-
};

0 commit comments

Comments
 (0)