Skip to content

Commit f8a1dfb

Browse files
committed
Merge branch 'SubMain' of https://github.com/Darshanp2/Marketplace into SubMain
2 parents e5eb942 + 89267f8 commit f8a1dfb

File tree

10 files changed

+573
-418
lines changed

10 files changed

+573
-418
lines changed

app.js

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
const express = require("express");
2-
const app = express();
3-
const configRoutes = require("./routes");
4-
const static = express.static(__dirname + "/public");
5-
const exphbs = require("express-handlebars");
6-
const session = require("express-session");
7-
8-
9-
const handlebarsInstance = exphbs.create({
10-
defaultLayout: 'main',
11-
// Specify helpers which are only registered on this instance.
12-
helpers: {
13-
asJSON: (obj, spacing) => {
14-
if (typeof spacing === 'number')
15-
return new Handlebars.SafeString(JSON.stringify(obj, null, spacing));
16-
17-
return new Handlebars.SafeString(JSON.stringify(obj));
18-
}
19-
},
20-
partialsDir: ['views/partials/']
21-
});
22-
23-
app.use;
24-
app.use('/public', static);
25-
app.use(express.json());
26-
app.use(express.urlencoded({ extended: true }));
27-
28-
29-
app.engine('handlebars', handlebarsInstance.engine);
30-
app.set('view engine', 'handlebars');
31-
32-
33-
app.use(session({
34-
name: 'AuthCookie',
35-
secret: 'Dont Touch Me',
36-
resave: false,
37-
saveUninitialized: true
38-
}))
39-
40-
// app.use('/private', (req, res, next) => {
41-
// if (!req.session.user) {
42-
// return res.status(403).render("users/error", { title: "You are not logged In", error: "So please log in" });
43-
// } else {
44-
// next();
45-
// }
46-
// });
47-
48-
let m = function(req, res, next) {
49-
let time = new Date().toUTCString();
50-
let RM = req.method;
51-
let RR = req.originalUrl;
52-
if (!req.session.user) {
53-
console.log("[" + time + "]: " + RM + " " + RR + " Non-Authenticated User");
54-
} else {
55-
56-
console.log("[" + time + "]: " + RM + " " + RR + " Authenticated User");
57-
}
58-
next();
59-
}
60-
61-
app.use(m)
62-
configRoutes(app);
63-
64-
app.listen(3000, () => {
65-
console.log("We've now got a server!");
66-
console.log('Your routes will be running on http://localhost:3000');
67-
});
1+
const express = require("express");
2+
const app = express();
3+
const configRoutes = require("./routes");
4+
const static = express.static(__dirname + "/public");
5+
const exphbs = require("express-handlebars");
6+
const session = require("express-session");
7+
8+
9+
const handlebarsInstance = exphbs.create({
10+
defaultLayout: 'main',
11+
// Specify helpers which are only registered on this instance.
12+
helpers: {
13+
asJSON: (obj, spacing) => {
14+
if (typeof spacing === 'number')
15+
return new Handlebars.SafeString(JSON.stringify(obj, null, spacing));
16+
17+
return new Handlebars.SafeString(JSON.stringify(obj));
18+
}
19+
},
20+
partialsDir: ['views/partials/']
21+
});
22+
23+
app.use;
24+
app.use('/public', static);
25+
app.use(express.json());
26+
app.use(express.urlencoded({ extended: true }));
27+
28+
29+
app.engine('handlebars', handlebarsInstance.engine);
30+
app.set('view engine', 'handlebars');
31+
32+
33+
app.use(session({
34+
name: 'AuthCookie',
35+
secret: 'Dont Touch Me',
36+
resave: false,
37+
saveUninitialized: true
38+
}))
39+
40+
// app.use('/private', (req, res, next) => {
41+
// if (!req.session.user) {
42+
// return res.status(403).render("users/error", { title: "You are not logged In", error: "So please log in" });
43+
// } else {
44+
// next();
45+
// }
46+
// });
47+
48+
let m = function(req, res, next) {
49+
let time = new Date().toUTCString();
50+
let RM = req.method;
51+
let RR = req.originalUrl;
52+
if (!req.session.user) {
53+
console.log("[" + time + "]: " + RM + " " + RR + " Non-Authenticated User");
54+
} else {
55+
56+
console.log("[" + time + "]: " + RM + " " + RR + " Authenticated User");
57+
}
58+
next();
59+
}
60+
61+
app.use(m)
62+
configRoutes(app);
63+
64+
app.listen(3000, () => {
65+
console.log("We've now got a server!");
66+
console.log('Your routes will be running on http://localhost:3000');
67+
});

data/user.js

Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,105 @@
1-
const mongoCollections = require("../config/mongoCollections.js");
2-
const user = mongoCollections.user;
3-
const product = mongoCollections.product;
4-
let { ObjectId } = require("mongodb");
5-
6-
const bcrypt = require("bcryptjs");
7-
8-
const saltRounds = 1;
9-
10-
async function getUser(id) {
11-
const userCollection = await user();
12-
const prodCollection = await product();
13-
14-
let objectID = ObjectId(id);
15-
const userModel = await userCollection.findOne({ _id: objectID });
16-
const productRes = await prodCollection.find({ sellerID: id }).toArray();
17-
let result = {
18-
user: userModel,
19-
products: productRes,
20-
};
21-
return result;
22-
}
23-
24-
function removeObjectFromId(obj) {
25-
obj["_id"] = obj["_id"].toString();
26-
return obj;
27-
}
28-
29-
async function updateProfile(name, email, Password, address, phone, id) {
30-
let objectID = ObjectId(id);
31-
const userCollection = await user();
32-
console.log(name + " " + email + " " + Password + " " + address);
33-
const updatedInfo = await userCollection.updateOne(
34-
{ _id: objectID },
35-
{
36-
$set: {
37-
name: name,
38-
address: address,
39-
phoneNumber: phone,
40-
email: email,
41-
password: Password,
42-
},
43-
}
44-
);
45-
if (updatedInfo.modifiedCount === 0) return false;
46-
return true;
47-
}
48-
49-
async function createUser(name, address, phoneNumber, email, password) {
50-
console.log("inside CreateUSer");
51-
for (let i of email) {
52-
if (i == " ") throw `email has empty spaces`;
53-
}
54-
for (let i of password) if (i == " ") throw `password has empty spaces`;
55-
const hashedPassword = await bcrypt.hash(password, saltRounds);
56-
console.log("inside create user")
57-
if (await emailExists(email)) {
58-
return "email already taken";
59-
}
60-
61-
const userCollection = await user();
62-
let newUser = {
63-
name: name,
64-
address: address,
65-
phoneNumber: phoneNumber,
66-
email: email,
67-
hashedPassword: hashedPassword,
68-
activeCart: [],
69-
};
70-
71-
const insertInfo = await userCollection.insertOne(newUser);
72-
if (insertInfo.insertedCount === 0) throw "Failed to create a new user.";
73-
74-
return { userInserted: true };
75-
}
76-
77-
async function emailExists(email) {
78-
email = email.toLowerCase();
79-
80-
const loginCollection = await user();
81-
82-
return (await loginCollection.findOne({ email: email })) !== null;
83-
}
84-
85-
async function checkUser(email, password) {
86-
const userCollection = await user();
87-
88-
const res = await userCollection.findOne({
89-
email: email,
90-
});
91-
if (res == null) {
92-
throw `error`;
93-
}
94-
if (await bcrypt.compare(password, res.hashedPassword)) {
95-
return { userId: removeObjectFromId(res)._id, authenticated: true };
96-
} else {
97-
throw `Password not match`;
98-
}
99-
}
100-
module.exports = {
101-
updateProfile,
102-
getUser,
103-
createUser,
104-
checkUser,
105-
};
1+
const mongoCollections = require("../config/mongoCollections.js");
2+
const user = mongoCollections.user;
3+
const product = mongoCollections.product;
4+
let { ObjectId } = require("mongodb");
5+
6+
const bcrypt = require("bcryptjs");
7+
8+
const saltRounds = 1;
9+
10+
async function getUser(id) {
11+
const userCollection = await user();
12+
const prodCollection = await product();
13+
14+
let objectID = ObjectId(id);
15+
const userModel = await userCollection.findOne({ _id: objectID });
16+
const productRes = await prodCollection.find({ sellerID: id }).toArray();
17+
let result = {
18+
user: userModel,
19+
products: productRes,
20+
};
21+
return result;
22+
}
23+
24+
function removeObjectFromId(obj) {
25+
obj["_id"] = obj["_id"].toString();
26+
return obj;
27+
}
28+
29+
async function updateProfile(name, email, Password, address, phone, id) {
30+
let objectID = ObjectId(id);
31+
const userCollection = await user();
32+
console.log(name + " " + email + " " + Password + " " + address);
33+
const updatedInfo = await userCollection.updateOne(
34+
{ _id: objectID },
35+
{
36+
$set: {
37+
name: name,
38+
address: address,
39+
phoneNumber: phone,
40+
email: email,
41+
password: Password,
42+
},
43+
}
44+
);
45+
if (updatedInfo.modifiedCount === 0) return false;
46+
return true;
47+
}
48+
49+
async function createUser(name, address, phoneNumber, email, password) {
50+
console.log("inside CreateUSer");
51+
for (let i of email) {
52+
if (i == " ") throw `email has empty spaces`;
53+
}
54+
for (let i of password) if (i == " ") throw `password has empty spaces`;
55+
const hashedPassword = await bcrypt.hash(password, saltRounds);
56+
console.log("inside create user")
57+
if (await emailExists(email)) {
58+
return "email already taken";
59+
}
60+
61+
const userCollection = await user();
62+
let newUser = {
63+
name: name,
64+
address: address,
65+
phoneNumber: phoneNumber,
66+
email: email,
67+
hashedPassword: hashedPassword,
68+
activeCart: [],
69+
};
70+
71+
const insertInfo = await userCollection.insertOne(newUser);
72+
if (insertInfo.insertedCount === 0) throw "Failed to create a new user.";
73+
74+
return { userInserted: true };
75+
}
76+
77+
async function emailExists(email) {
78+
email = email.toLowerCase();
79+
80+
const loginCollection = await user();
81+
82+
return (await loginCollection.findOne({ email: email })) !== null;
83+
}
84+
85+
async function checkUser(email, password) {
86+
const userCollection = await user();
87+
88+
const res = await userCollection.findOne({
89+
email: email,
90+
});
91+
if (res == null) {
92+
throw `error`;
93+
}
94+
if (await bcrypt.compare(password, res.hashedPassword)) {
95+
return { userId: removeObjectFromId(res)._id, authenticated: true };
96+
} else {
97+
throw `Password not match`;
98+
}
99+
}
100+
module.exports = {
101+
updateProfile,
102+
getUser,
103+
createUser,
104+
checkUser,
105+
};

package.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
{
2-
"name": "project",
3-
"version": "1.0.0",
4-
"description": "",
5-
"main": "app.js",
6-
"scripts": {
7-
"start": "node app.js",
8-
"test": "echo \"Error: no test specified\" && exit 1"
9-
},
10-
"author": "",
11-
"license": "ISC",
12-
"dependencies": {
13-
"bcryptjs": "^2.4.3",
14-
"express": "^4.17.1",
15-
"express-handlebars": "^6.0.2",
16-
"express-session": "^1.17.2",
17-
"express-sesssion": "^1.15.5",
18-
"mongodb": "^4.2.0",
19-
"multer": "^1.4.3",
20-
"utils": "^0.3.1"
21-
}
22-
}
1+
{
2+
"name": "project",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "node app.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"bcryptjs": "^2.4.3",
14+
"express": "^4.17.1",
15+
"express-handlebars": "^6.0.2",
16+
"express-session": "^1.17.2",
17+
"express-sesssion": "^1.15.5",
18+
"mongodb": "^4.2.0",
19+
"multer": "^1.4.3",
20+
"utils": "^0.3.1"
21+
}
22+
}

0 commit comments

Comments
 (0)