Skip to content

Commit 28f11c1

Browse files
committed
fixed search and removed username from cookies
1 parent 9f8eef2 commit 28f11c1

File tree

5 files changed

+43
-39
lines changed

5 files changed

+43
-39
lines changed

app/src/components/marketplace/Searchbar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ const SearchBar = ({marketplaceProjects, updateDisplayProjects }):JSX.Element =>
2929
//a-zA-Z letters (lowercase and uppercase) and underscore symbol
3030
//All together: match either the start/end of a line/string or any character that is not a letter.
3131
//Looks for anything that has the searchText in between non-letter characters
32+
const patternString2 = '(?:^|.)' + searchText.toLowerCase() + '(?:$|.)';
33+
//Only difference is that (?:^|.) (?:$|.) look for anything that matches the string in between any other characters for the username search
34+
//test3 and 1test) would both match for string 'test'
35+
3236
const searchResults = marketplaceProjects.reduce((results, curr) => {
33-
if(curr.name.match(patternString) || curr.username.match(patternString))
37+
if(curr.name.match(patternString) || curr.username.match(patternString2))
3438
results.push(curr)
3539
return results;
3640
}, [])

server/controllers/cookieController.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ const cookieController: CookieController = {
1212
return next();
1313
},
1414

15-
/**
16-
* Stores the current user's username in cookie (for use in cloning projects)
17-
*/
18-
setUserCookie: (req, res, next) => {
19-
res.cookie('username', res.locals.username, {
20-
httpOnly: true,
21-
sameSite: 'none',
22-
secure: true
23-
});
24-
return next();
25-
}
2615
};
2716

2817
export default cookieController;

server/controllers/marketplaceController.ts

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MarketplaceController } from '../interfaces';
2-
import { Projects } from '../models/reactypeModels';
2+
import { Projects, Users } from '../models/reactypeModels';
33

44
// array of objects, objects inside
55
type Projects = { project: {} }[];
@@ -109,32 +109,45 @@ const marketplaceController: MarketplaceController = {
109109
*
110110
*/
111111
cloneProject: (req, res, next) => {
112-
// pulls cookies from request
113-
const userId = req.cookies.ssid;
114-
const username = req.cookies.username;
115112
const { updatedProject } = req.body;
116-
updatedProject.userId = userId;
117-
updatedProject.username = username;
118-
updatedProject.project.forked = true; // updated the forked tag
119-
delete updatedProject._id; // removes the old project id from the object
120-
updatedProject.createdAt = Date.now();
113+
114+
// pulls cookies from request
115+
const currentuserID = req.cookies.ssid
116+
//getting the username based on the cookies ssid
117+
Users.findOne({ _id: currentuserID }, (err, user) => {
118+
if (err) {
119+
return next({
120+
log: `Error in marketplaceController.cloneProjects findUser: ${err}`,
121+
message: {
122+
err: 'Error in marketplaceController.cloneProjects findUser, check server logs for details'
123+
}
124+
});
125+
}
126+
//adding the current user's username and userID since the project is now cloned
127+
updatedProject.username = user.username;
128+
updatedProject.userId = currentuserID;
129+
updatedProject.project.forked = true; // updated the forked tag
130+
delete updatedProject._id; // removes the old project id from the object
131+
updatedProject.createdAt = Date.now();
132+
updatedProject.published = false;
121133

122-
Projects.create(
123-
// creates a copy of the project to the user's library
124-
updatedProject,
125-
(err, result) => {
126-
if (err) {
127-
return next({
128-
log: `Error in marketplaceController.cloneProject: ${err}`,
129-
message: {
130-
err: 'Error in marketplaceController.cloneProject, check server logs for details'
131-
}
132-
});
134+
Projects.create(
135+
// creates a copy of the project to the user's library with a new generated _id
136+
updatedProject,
137+
(err, result) => {
138+
if (err) {
139+
return next({
140+
log: `Error in marketplaceController.cloneProject: ${err}`,
141+
message: {
142+
err: 'Error in marketplaceController.cloneProject, check server logs for details'
143+
}
144+
});
145+
}
146+
res.locals.clonedProject = result;
147+
return next();
133148
}
134-
res.locals.clonedProject = result;
135-
return next();
136-
}
137-
);
149+
);
150+
})
138151
},
139152
};
140153
export default marketplaceController;

server/interfaces.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Document, NativeError } from 'mongoose';
33

44
export interface CookieController {
55
setSSIDCookie: RequestHandler;
6-
setUserCookie: RequestHandler;
76
}
87

98
export interface ProjectController {

server/server.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ app.post(
159159
'/signup',
160160
userController.createUser,
161161
cookieController.setSSIDCookie,
162-
cookieController.setUserCookie,
163162
sessionController.startSession,
164163
(req, res) => res.status(200).json({ sessionId: res.locals.ssid })
165164
);
@@ -168,7 +167,6 @@ app.post(
168167
'/login',
169168
userController.verifyUser,
170169
cookieController.setSSIDCookie,
171-
cookieController.setUserCookie,
172170
sessionController.startSession,
173171
(req, res) => res.status(200).json({ sessionId: res.locals.ssid })
174172
);
@@ -222,6 +220,7 @@ app.get(
222220
// Clone from marketplace
223221
app.post(
224222
'/cloneProject',
223+
sessionController.isLoggedIn,
225224
marketplaceController.cloneProject,
226225
(req, res) => res.status(200).json(res.locals.clonedProject)
227226
);

0 commit comments

Comments
 (0)