Skip to content

Commit 411eb1f

Browse files
committed
removed pub button from marketplace, fixing routes for google oauth
1 parent 6aca4bb commit 411eb1f

File tree

11 files changed

+102
-40
lines changed

11 files changed

+102
-40
lines changed

app/src/components/login/SignIn.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = (props) => {
318318
>
319319
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
320320
</svg>
321-
</Button>
321+
</Button>*/}
322322
<Button
323323
fullWidth
324324
variant="contained"
@@ -344,7 +344,7 @@ const SignIn: React.FC<LoginInt & RouteComponentProps> = (props) => {
344344
>
345345
<path d="M15.545 6.558a9.42 9.42 0 0 1 .139 1.626c0 2.434-.87 4.492-2.384 5.885h.002C11.978 15.292 10.158 16 8 16A8 8 0 1 1 8 0a7.689 7.689 0 0 1 5.352 2.082l-2.284 2.284A4.347 4.347 0 0 0 8 3.166c-2.087 0-3.86 1.408-4.492 3.304a4.792 4.792 0 0 0 0 3.063h.003c.635 1.893 2.405 3.301 4.492 3.301 1.078 0 2.004-.276 2.722-.764h-.003a3.702 3.702 0 0 0 1.599-2.431H8v-3.08h7.545z" />
346346
</svg>
347-
</Button> */}
347+
</Button>
348348
<Button
349349
fullWidth
350350
variant="contained"

app/src/components/top/NavBar.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useRef, useState } from 'react';
2-
import { Link } from 'react-router-dom';
2+
import { Link, useHistory } from 'react-router-dom';
33
import Avatar from '@mui/material/Avatar';
44
import Button from '@mui/material/Button';
55
import MoreVertIcon from '@mui/icons-material/MoreVert';
@@ -23,6 +23,9 @@ const NavBar = () => {
2323
const [projectName, setProjectName] = useState(state.name || '');
2424
const [invalidProjectName, setInvalidProjectName] = useState(false);
2525
const [invalidProjectNameMessage, setInvalidProjectNameMessage] = useState('');
26+
const urlAdd = useHistory();
27+
const isMarketplace = urlAdd.location.pathname === '/marketplace';
28+
2629
const isDarkMode = useSelector(
2730
(state: RootState) => state.darkMode.isDarkMode
2831
);
@@ -156,15 +159,15 @@ const NavBar = () => {
156159
</div>
157160
</Link>
158161
<div style={buttonContainerStyle}>
159-
{state.published ? (
162+
{isMarketplace ? null : (state.published ? (
160163
<button style={buttonStyle} onClick={handleUnpublish}>
161164
Unpublish
162165
</button>
163166
) : (
164167
<button style={buttonStyle} onClick={handlePublish}>
165168
Publish
166169
</button>
167-
)}
170+
))}
168171
<NewExportButton />
169172
<Button
170173
style={moreVertButtonStyle}

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@ if (isDev) {
1010
}
1111

1212
export const getProjects = (): Promise<any> => {
13-
let userId = window.localStorage.getItem('ssid');
14-
const body = JSON.stringify({ userId });
1513
const projects = fetch(`${serverURL}/getProjects`, {
1614
method: 'POST',
1715
headers: {
1816
'content-type': 'application/json'
1917
},
2018
// need credentials for userid pull from cookie
21-
credentials: 'include',
22-
body
19+
credentials: 'include'
2320
})
2421
.then((res) => res.json())
2522
.then((data) => {
23+
console.log('........................ data', data)
2624
return data;
2725
})
2826
.catch((err) => console.log(`Error getting project ${err}`));
@@ -39,8 +37,6 @@ export const saveProject = (
3937
const body = JSON.stringify({
4038
name,
4139
project: { ...newProject},
42-
userId: window.localStorage.getItem('ssid'),
43-
username: window.localStorage.getItem('username'),
4440
comments: []
4541
});
4642
const project = fetch(`${serverURL}/saveProject`, {
@@ -69,8 +65,6 @@ export const publishProject = (
6965
_id: workspace._id,
7066
name: name,
7167
project: { ...newProject},
72-
userId: window.localStorage.getItem('ssid'),
73-
username: window.localStorage.getItem('username'),
7468
comments: [],
7569
});
7670

@@ -101,7 +95,6 @@ export const unpublishProject = (
10195
): Promise<Object> => {
10296
const body = JSON.stringify({
10397
_id: projectData._id,
104-
userId: window.localStorage.getItem('ssid'),
10598
});
10699

107100
const response = fetch(`${serverURL}/unpublishProject`, {
@@ -129,7 +122,7 @@ export const unpublishProject = (
129122
export const deleteProject = (project: any): Promise<Object> => {
130123
const body = JSON.stringify({
131124
_id: project._id,
132-
userId: window.localStorage.getItem('ssid')
125+
// userId: window.localStorage.getItem('ssid')
133126
});
134127
const deletedProject = fetch(`${serverURL}/deleteProject`, {
135128
method: 'DELETE',

app/src/index.tsx

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import {
1010
import { DndProvider } from 'react-dnd';
1111
import { HTML5Backend } from 'react-dnd-html5-backend';
1212
import App from './components/App';
13-
import Cookies from 'js-cookie';
1413
import FBPassWord from './components/login/FBPassWord';
1514
import MarketplaceContainer from './containers/MarketplaceContainer';
1615
import ProjectDashboard from './Dashboard/ProjectContainer';
16+
import { useState, useEffect } from 'react';
1717
import { Provider } from 'react-redux';
1818
import React from 'react';
1919
import ReactDOM from 'react-dom';
@@ -28,18 +28,53 @@ const client = new ApolloClient({
2828
cache: new InMemoryCache()
2929
});
3030

31-
const PrivateRoute = ({ component: Component, ...rest }) => (
31+
const isDev = process.env.NODE_ENV === 'development';
32+
const { DEV_PORT, API_BASE_URL } = require('../../config.js');
33+
let serverURL = API_BASE_URL;
34+
35+
//check if we're in dev mode
36+
if (isDev) {
37+
serverURL = `http://localhost:${DEV_PORT}`;
38+
}
39+
40+
const PrivateRoute = ({ component: Component, ...rest }) => {
41+
const [isLoggedIn, setIsLoggedIn] = useState(null);
42+
43+
useEffect(() => {
44+
45+
const projects = fetch(`${serverURL}/loggedIn`, {
46+
method: 'GET',
47+
headers: {
48+
'content-type': 'application/json'
49+
},
50+
// need credentials for userid pull from cookie
51+
credentials: 'include'
52+
})
53+
.then((res) => res.json())
54+
.then((data) => {
55+
console.log('........................ data', data);
56+
setIsLoggedIn(data);
57+
58+
})
59+
.catch((err) => console.log(`Error getting project ${err}`));
60+
}, []);
61+
62+
return (
3263
<Route
3364
{...rest}
3465
render={(props) => {
35-
return Cookies.get('ssid') || window.localStorage.getItem('ssid') ? (
36-
<Component {...props} />
37-
) : (
38-
<Redirect to="/login" />
39-
);
66+
if (isLoggedIn) {
67+
console.log("should be app")
68+
// User is logged in, render the protected component
69+
return <Component {...props} />;
70+
} else if(isLoggedIn !== null) {
71+
console.log("redirect login")
72+
// User is not logged in, redirect to the login page
73+
return <Redirect to="/login" />;
74+
}
4075
}}
4176
/>
42-
);
77+
)};
4378

4479

4580
ReactDOM.render(

config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ const config = {
33
DEV_PORT: 5656,
44
API_BASE_URL: isProduction
55
? 'https://app.reactype.dev'
6-
: 'http://localhost:5656',
6+
: 'http://localhost:8080',
7+
// : 'http://localhost:5656',
78
API_BASE_URL2: isProduction
89
? 'https://app.reactype.dev'
910
: 'http://localhost:8080',

server/controllers/cookieController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CookieController } from '../interfaces';
22

3+
34
const cookieController: CookieController = {
45
// setSSIDCookie - store the user id from database in cookie
56
setSSIDCookie: (req, res, next) => {

server/controllers/marketplaceController.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ const marketplaceController: MarketplaceController = {
3737
* @return sends the updated entire project document to the frontend
3838
*/
3939
publishProject: async (req, res, next) => {
40-
const { _id, project, comments, userId, username, name } = req.body;
40+
const { _id, project, comments, name } = req.body;
41+
const username = req.cookies.username;
42+
const userId = req.cookies.ssid;
4143
const createdAt = Date.now();
4244

43-
if (userId === req.cookies.ssid) {
45+
try{
4446
if (mongoose.isValidObjectId(_id)) {
4547
const noPub = {...project}
4648
delete noPub.published;
@@ -67,7 +69,7 @@ const marketplaceController: MarketplaceController = {
6769
return next();
6870
}
6971
}
70-
else {
72+
catch {
7173

7274
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
7375
return next({
@@ -85,12 +87,12 @@ const marketplaceController: MarketplaceController = {
8587
* @return sends the updated project to the frontend
8688
*/
8789
unpublishProject: (req, res, next) => {
88-
// pull project name and userId from req.body
89-
const { _id, userId } = req.body;
90+
const { _id } = req.body;
91+
const userId = req.cookies.ssid;
9092
//check if req.cookies.ssid matches userId
9193

92-
if (userId === req.cookies.ssid ) {
93-
Projects.findOneAndUpdate({ _id }, {published: false}, { new: true }, (err, result) => {
94+
try {
95+
Projects.findOneAndUpdate({ _id, userId }, {published: false}, { new: true }, (err, result) => {
9496
if (err || result === null) {
9597
return next({
9698
log: `Error in marketplaceController.unpublishProject: ${err || null}`,
@@ -103,7 +105,7 @@ const marketplaceController: MarketplaceController = {
103105
return next();
104106
});
105107
}
106-
else {
108+
catch {
107109
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
108110
return next({
109111
log: `Error in marketplaceController.unpublishProject`,

server/controllers/projectController.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const projectController: ProjectController = {
1010
saveProject: (req, res, next) => {
1111

1212
// pull project name and project itself from body
13-
const { name, project, userId, username, comments } = req.body;
13+
const { name, project, comments } = req.body;
14+
const username = req.cookies.username;
15+
const userId = req.cookies.ssid;
1416
//deleted published from project
1517
const noPub = {...project};
1618
delete noPub.published;
@@ -43,7 +45,7 @@ const projectController: ProjectController = {
4345

4446
// gets all of current user's projects
4547
getProjects: (req, res, next) => {
46-
const { userId } = req.body;
48+
const userId = req.cookies.ssid
4749
Projects.find({ userId }, (err, projects: Array<{_id: string; published: boolean; project: object }>) => {
4850
if (err) {
4951
return next({
@@ -68,7 +70,8 @@ const projectController: ProjectController = {
6870
// delete project from database
6971
deleteProject: async (req, res, next) => {
7072
// pull project name and userId from req.body
71-
const { _id, userId } = req.body;
73+
const { _id } = req.body;
74+
const userId = req.cookies.ssid;
7275
// try {
7376
// const response = await Projects.findOneAndDelete({ _id: _id, username: userId });
7477
// res.locals.deleted = response;

server/controllers/sessionController.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ const sessionController: SessionController = {
2424
// find session from request session ID in mongodb
2525
const session = await Sessions.findOne({ cookieId });
2626
if (!session) {
27+
console.log('no session')
28+
res.locals.loggedIn = false;
2729
return res.redirect('/');
2830
}
31+
res.locals.loggedIn = true;
2932
return next();
3033
} catch (err) {
3134
return next({
@@ -40,7 +43,7 @@ const sessionController: SessionController = {
4043
// startSession - create and save a new session into the database
4144
startSession: (req, res, next) => {
4245
// first check if user is logged in already
43-
Sessions.findOne({ cookieId: res.locals.id }, (err, ses) => {
46+
Sessions.findOne({ cookieId: res.locals.id || req.user.id }, (err, ses) => {
4447
if (err) {
4548
return next({
4649
log: `Error in sessionController.startSession find session: ${err}`,
@@ -53,11 +56,12 @@ const sessionController: SessionController = {
5356
}
5457
if (!ses) {
5558
Sessions.create(
56-
{ cookieId: res.locals.id },
59+
//checking if logged in via the login form (res.locals.id) or oauth(req.user.id)
60+
{ cookieId: res.locals.id || req.user.id },
5761
(error, session: SessionCookie) => {
5862
if (error) {
5963
return next({
60-
log: `Error in sessionController.startSession create session: ${err}`,
64+
log: `Error in sessionController.startSession create session: ${error}`,
6165
message: {
6266
err: 'Error in sessionController.startSession create session, check server logs for details'
6367
}

server/routers/auth.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import express from 'express';
22
const passport = require('passport');
33
import config from '../../config';
44
import { Request } from 'express';
5+
import sessionController from '../controllers/sessionController';
56

67
// trying to add interface
78
interface UserReq extends Request {
89
user: {
910
id: string;
11+
username: string;
12+
googleId: string;
1013
};
1114
}
1215

@@ -41,9 +44,19 @@ router.get(
4144
router.get(
4245
'/google/callback',
4346
passport.authenticate('google'),
47+
sessionController.startSession,
4448
(req: UserReq, res) => {
4549
console.log('google authenicate function being run');
46-
res.cookie('ssid', req.user.id);
50+
res.cookie('ssid', req.user.id, {
51+
httpOnly: true,
52+
sameSite: 'none',
53+
secure: true,
54+
});
55+
res.cookie('username', req.user.username, {
56+
httpOnly: true,
57+
sameSite: 'none',
58+
secure: true,
59+
});
4760
return res.redirect(API_BASE_URL);
4861
}
4962
);

0 commit comments

Comments
 (0)