Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cy testing #170

Open
wants to merge 183 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
183 commits
Select commit Hold shift + click to select a range
c5c4bd4
Diagraming first day
michael-szczepanski Jun 6, 2023
a3d3ecb
Merge pull request #1 from sharmine-s/notes
camybish Jun 6, 2023
2f62ced
Delete .DS_Store
sharmine-s Jun 7, 2023
82d58fe
Delete .DS_Store in api folder
sharmine-s Jun 7, 2023
abfc726
Delete videos folder in frontend cypress tests
sharmine-s Jun 7, 2023
7658ab6
added login heading
ChristieGriffiths Jun 8, 2023
a0e91f5
increased margin size
ChristieGriffiths Jun 8, 2023
3e4fdf5
removed redundant role tag in login form
ChristieGriffiths Jun 8, 2023
acc433a
added /cypress/screenshots to frontend gitignore file
wimdavies Jun 8, 2023
dc07d18
console.log message now correctly confirms success/unsuccess of login
ChristieGriffiths Jun 8, 2023
5cb8a1c
testdrove adding User name on Model
wimdavies Jun 8, 2023
5fe7bcc
deleted redundant package-lock.json
ChristieGriffiths Jun 8, 2023
d0fe121
add header and labels to form. Add function to check valid email and …
sarahc-dev Jun 8, 2023
c5d0fdf
removed h1 tag from within form tag and place into empty tag, and add…
sharmine-s Jun 8, 2023
6be8998
Merge pull request #4 from sharmine-s/login-html
sharmine-s Jun 8, 2023
afe70cf
Merge pull request #3 from sharmine-s/signup_html
sharmine-s Jun 8, 2023
2cd8237
actually adding the User model change
wimdavies Jun 8, 2023
3275954
Merge remote-tracking branch 'origin/main' into name-on-signup
hanguyen21 Jun 8, 2023
0e06a33
merging
wimdavies Jun 8, 2023
8e46384
Create a post form created, Post model updated for date input
michael-szczepanski Jun 8, 2023
cafdfc5
addname on Signup form
hanguyen21 Jun 8, 2023
190acb0
add vaidation for server error. Amend css to make errors red
sarahc-dev Jun 8, 2023
195bac1
altered mongoose schema to force email to be unique
sharmine-s Jun 8, 2023
31e7898
fixing git issue
wimdavies Jun 8, 2023
04fd1cc
add novalidate to disable html errors
sarahc-dev Jun 8, 2023
dcfde07
added default name on Model to enable tests to pass; reordered schema…
wimdavies Jun 8, 2023
b9edbd9
Merge pull request #5 from sharmine-s/post_creation
hanguyen21 Jun 8, 2023
d481709
Merge pull request #6 from sharmine-s/email-uniqueness-2
hanguyen21 Jun 8, 2023
b54f4fc
add frontend error handling to login page
sarahc-dev Jun 8, 2023
7e57e38
Merge branch 'main' into name-on-signup
wimdavies Jun 8, 2023
4fba244
signing up with an existing email now returns a 409 error
sharmine-s Jun 8, 2023
74f165c
Merge pull request #7 from sharmine-s/name-on-signup
michael-szczepanski Jun 8, 2023
6c4f0b4
Adding time to posts; rewrote a unit test to include a valid time
wimdavies Jun 8, 2023
b253862
added backend test to check that error is thrown when signing up dupl…
sharmine-s Jun 8, 2023
1136333
Merge pull request #9 from sharmine-s/duplicate-email-error-code
sarahc-dev Jun 8, 2023
74ec0b8
change date to the right format
hanguyen21 Jun 8, 2023
edf38d7
add merges
sarahc-dev Jun 8, 2023
ce5b36b
Merge remote-tracking branch 'origin/main' into user_alerts
sarahc-dev Jun 8, 2023
fd2acba
password now encrypted on sign up
ChristieGriffiths Jun 8, 2023
5726be2
added backend validation message from server
sarahc-dev Jun 8, 2023
23c182c
updated validations on login
sarahc-dev Jun 8, 2023
49b1c5b
Working on adding user name to token. Currently name is undefined wit…
wimdavies Jun 8, 2023
b51bca7
log in now decrypts passwords and refactored 1 test, others failing
ChristieGriffiths Jun 8, 2023
33e19f2
add validation error to create post
sarahc-dev Jun 8, 2023
8125c5d
2 out of 3 paths fixed
camybish Jun 8, 2023
7597660
added post sort by date with newest posts appearing first
michael-szczepanski Jun 9, 2023
4b60b3b
fixed posts.spec test failing because of controller change
michael-szczepanski Jun 9, 2023
7b3d296
fixed failing test to pass which just needed async and await
sharmine-s Jun 9, 2023
3b59990
Post Page now refreshes after new post received
camybish Jun 9, 2023
5aaf8b6
Merge pull request #10 from sharmine-s/user_alerts
ChristieGriffiths Jun 9, 2023
d6a511e
Worked out how to populate user name and id into newly created Posts
wimdavies Jun 9, 2023
249860b
posts redirects to login if logged out or invalid token
sarahc-dev Jun 9, 2023
fe687fd
Frontend ready to display post.user.name; requires API post Index to …
wimdavies Jun 9, 2023
7e160cb
like button and user_id retrieval complete
michael-szczepanski Jun 9, 2023
2f4fb79
clean up files and add comment
sarahc-dev Jun 9, 2023
1cce5a6
created new Navbar component with Logout in it, so far the logout but…
sharmine-s Jun 9, 2023
071618f
logout button on navbar now worksgit status
sharmine-s Jun 9, 2023
367ab9e
Added bar loader for invalid access to /posts
camybish Jun 9, 2023
cf0e8ad
Merge branch 'main' into user-flow
camybish Jun 9, 2023
dc35c84
trying to add css, currently not working
sharmine-s Jun 9, 2023
2e3f2bb
Working implementation.
wimdavies Jun 9, 2023
d35e9e6
likes now correctly update the database
michael-szczepanski Jun 9, 2023
46bd9c2
Removed comments; removed unnecessary .populate in Create
wimdavies Jun 9, 2023
2d1aef5
created individual css files to add margins for sign up, login and posts
ChristieGriffiths Jun 9, 2023
f04f257
styled topnav and logout button
sharmine-s Jun 9, 2023
ae0683d
Started SPIKE on default avatar
camybish Jun 9, 2023
da7b3ac
Started SPIKE on default avatar 2
camybish Jun 9, 2023
b9f288b
edited css for topnav
sharmine-s Jun 9, 2023
d82d9e1
fixed mistake where tests were greyed out
sharmine-s Jun 9, 2023
3139327
fixed another issue with the tests, all clear now
sharmine-s Jun 9, 2023
b7bd942
Merge branch 'main' into posts-date-and-name
wimdavies Jun 9, 2023
5ae42e3
Merge pull request #12 from sharmine-s/encrypted-password
sarahc-dev Jun 9, 2023
29405d7
Merge pull request #11 from sharmine-s/post-sort
ChristieGriffiths Jun 9, 2023
1edc99a
Merge pull request #15 from sharmine-s/navbar
ChristieGriffiths Jun 9, 2023
5c9efc2
Merge branch 'main' into posts-date-and-name
wimdavies Jun 9, 2023
d6f3bff
Merge branch 'main' into user-flow
wimdavies Jun 9, 2023
a356cbe
Merge branch 'main' into posts-date-and-name
wimdavies Jun 9, 2023
44e251a
Merge pull request #16 from sharmine-s/posts-date-and-name
wimdavies Jun 9, 2023
dfedb13
Merge branch 'main' into like-button
michael-szczepanski Jun 9, 2023
1482c21
Correcting errors in conflict resolves on tokens.js and posts.js cont…
wimdavies Jun 9, 2023
81e51ed
Merge pull request #17 from sharmine-s/fixing-errors
wimdavies Jun 9, 2023
11c1158
fixed some weird ass bullcrap
michael-szczepanski Jun 11, 2023
78de7ae
Merge branch 'main' into like-button
michael-szczepanski Jun 11, 2023
4682708
add loading state to fix bug where no posts in db
sarahc-dev Jun 12, 2023
803e864
Merge pull request #14 from sharmine-s/like-button
wimdavies Jun 12, 2023
03f1e93
Merge branch 'main' into user-flow
michael-szczepanski Jun 12, 2023
499461f
Merge pull request #13 from sharmine-s/user-flow
michael-szczepanski Jun 12, 2023
140185f
fix issue with if statement
sarahc-dev Jun 12, 2023
ce3ed03
formatting changes
michael-szczepanski Jun 12, 2023
9f76f81
I like git less and less these days. Missing jwt_decoder dependency m…
michael-szczepanski Jun 12, 2023
8895ff0
Merge pull request #18 from sharmine-s/fix-feed
wimdavies Jun 12, 2023
ae46cc4
Merge remote-tracking branch 'origin/main' into default-avatar-spike
camybish Jun 12, 2023
b8d4fe9
Package.json test script ammended
michael-szczepanski Jun 12, 2023
e9b1988
test to check post has correct properties
ChristieGriffiths Jun 12, 2023
0187977
Initial implementation of default avatar complete
camybish Jun 12, 2023
2e09bc3
add update comment to schema and attempt test
sarahc-dev Jun 12, 2023
17819cd
Merge pull request #19 from sharmine-s/default-avatar-spike
sarahc-dev Jun 12, 2023
bc4149d
merge from main
sarahc-dev Jun 12, 2023
b44689b
npm installed cloudinary
camybish Jun 12, 2023
8d561fb
Merge remote-tracking branch 'origin/main' into image-api
camybish Jun 12, 2023
52520fe
complete test for post controller update
ChristieGriffiths Jun 12, 2023
c84b681
merging main
ChristieGriffiths Jun 12, 2023
8748b6d
test drive to add comment
sarahc-dev Jun 12, 2023
1a460ca
tokens controller.create extra test case
michael-szczepanski Jun 12, 2023
3dd48bd
backend cloudinary config
wimdavies Jun 12, 2023
2b52054
removed redundant console.logs
michael-szczepanski Jun 12, 2023
81c8bd2
link between login and signup
hanguyen21 Jun 12, 2023
29475d3
add method to add comment - not tested
sarahc-dev Jun 12, 2023
68a9c7c
removed unnecessary process.env.PUBLIC_URL from avatar display
wimdavies Jun 12, 2023
0a4de40
Merge pull request #21 from sharmine-s/link-login-signup
camybish Jun 12, 2023
7d392c3
Merge pull request #20 from sharmine-s/testing-debt
wimdavies Jun 12, 2023
ffdaca1
Merging main changes into image-api
wimdavies Jun 12, 2023
42df2fe
remove postRefresh in onclick and amend console.log in feed for bette…
sarahc-dev Jun 13, 2023
699aa27
Merge pull request #22 from sharmine-s/bugfix-refresh-page
camybish Jun 13, 2023
541c8a3
erased cloudinary dependencies on backend
wimdavies Jun 13, 2023
63420a5
merged tests PR
sharmine-s Jun 13, 2023
b7ce121
fixed bug caused by parallel jest testing
michael-szczepanski Jun 13, 2023
14993db
changed patch route to /comments
sharmine-s Jun 13, 2023
ffd271b
Feed now correctly rerenders after post creation
michael-szczepanski Jun 13, 2023
4ef4042
removed redundant css code
michael-szczepanski Jun 13, 2023
d1c2b6a
Revert "Merge pull request #22 from sharmine-s/bugfix-refresh-page"
ChristieGriffiths Jun 13, 2023
1228015
add logo across the board
ChristieGriffiths Jun 13, 2023
22c7e14
added comments collection which includes userId, and commentId now sh…
sharmine-s Jun 13, 2023
aed2d74
Added template for cloudinary upload template, needs implimentation
camybish Jun 13, 2023
d1e64cf
Merge pull request #23 from sharmine-s/bug-fixes
camybish Jun 13, 2023
b515ce0
Merge remote-tracking branch 'origin/main' into image-api
wimdavies Jun 13, 2023
470996f
css shenanigans for post display
michael-szczepanski Jun 13, 2023
47df37b
Merge branch 'main' into logo-take2
michael-szczepanski Jun 13, 2023
b1fa858
padding fix
michael-szczepanski Jun 13, 2023
fa8edb4
Merge pull request #24 from sharmine-s/logo-take2
wimdavies Jun 13, 2023
ab6eef1
Merge pull request #25 from sharmine-s/bug-fixes
ChristieGriffiths Jun 13, 2023
757de22
populate comment in find
sarahc-dev Jun 13, 2023
67d9b61
refreshPage bugfix and DOM compliance
michael-szczepanski Jun 13, 2023
be09fdd
change to to prevent duplicates
sarahc-dev Jun 13, 2023
967e562
clean up files, add comments, populate user field in comment
sarahc-dev Jun 13, 2023
e357bd7
amend populate to not include password
sarahc-dev Jun 13, 2023
c9a65bb
Merge pull request #26 from sharmine-s/refresh-bug-fix
sharmine-s Jun 13, 2023
2ecc3f7
Implemented the basic widget component, and used it in SignUpForm
wimdavies Jun 13, 2023
048f4a3
Merge remote-tracking branch 'origin/main' into image-api
wimdavies Jun 13, 2023
6e6f1e8
Merge remote-tracking branch 'origin/main' into comments
sharmine-s Jun 14, 2023
2db4732
started creating comments model spec test, needs to be updated
sharmine-s Jun 14, 2023
2526693
created passing tests for comments model
sharmine-s Jun 14, 2023
aa9a472
Tidied up widget and commented it for wider usage. Decoupled from Sig…
wimdavies Jun 14, 2023
432bcac
add tests for model and add comments to start tests for controller
sharmine-s Jun 14, 2023
75ba669
Merge pull request #27 from sharmine-s/comments
camybish Jun 14, 2023
e9432e4
Merge pull request #28 from sharmine-s/image-api
sharmine-s Jun 14, 2023
2ef7508
created a fetch for creating a comment
michael-szczepanski Jun 14, 2023
2e02740
comments are now correctly assigned to posts
michael-szczepanski Jun 14, 2023
a857aec
initial set up for the work
wimdavies Jun 14, 2023
c822c74
posts now somehow display
michael-szczepanski Jun 14, 2023
f77cb63
basic intergration of widget into postcreateform, possibly broken
ChristieGriffiths Jun 14, 2023
9347940
css added for comment to be consistent with post
michael-szczepanski Jun 14, 2023
1780bf5
fixed naming error; assigned imageUrl in PostCreateForm POST request …
wimdavies Jun 14, 2023
c47fc8c
Implemented post imageURl storage on DB
wimdavies Jun 14, 2023
44a02a1
Implemented imageUrl as <img> in Post.js
wimdavies Jun 14, 2023
7dd3583
added e2e cypress testing for sprint 2 features
camybish Jun 14, 2023
21d0f77
users can upload their avata when they sign up
hanguyen21 Jun 14, 2023
6bad912
Fixed issue with button type, copied in some basic css for the button
wimdavies Jun 14, 2023
ff990b6
CSS for comments
michael-szczepanski Jun 14, 2023
c0af4b3
added css for login, signup
hanguyen21 Jun 14, 2023
3ea9f01
add height to image
sarahc-dev Jun 14, 2023
1aea9f9
add preview image and ability to delete, remove package.json in root …
sarahc-dev Jun 15, 2023
785dc23
Changed button text; updated widget params to crop correctly and not …
wimdavies Jun 15, 2023
d884666
Merge pull request #30 from sharmine-s/avata
sarahc-dev Jun 15, 2023
ffaa12e
Fixed issue with duplicate images, by resetting imageUrl to empty string
wimdavies Jun 15, 2023
0a4c1ca
Merge remote-tracking branch 'origin/main' into image-posts
wimdavies Jun 15, 2023
8b33bc0
Updated empty post text error message to be more clear
wimdavies Jun 15, 2023
b24c771
Continuing e2e testing
camybish Jun 15, 2023
a02a321
Merge pull request #31 from sharmine-s/image-posts
wimdavies Jun 15, 2023
20fbe8a
conflict resolutions
michael-szczepanski Jun 15, 2023
6db1291
neverending story about merge conflicts
michael-szczepanski Jun 15, 2023
e8316a9
All the new files
camybish Jun 15, 2023
fcaa0ff
style consistency in feed
michael-szczepanski Jun 15, 2023
2787c71
Merge pull request #29 from sharmine-s/create-and-view-comments
michael-szczepanski Jun 15, 2023
bcb221e
fix bug where default avatar was not showing
sarahc-dev Jun 15, 2023
093222b
Restored tests from yesterday
camybish Jun 15, 2023
3c78b67
Ready to update tests
camybish Jun 15, 2023
c56f1d9
Merge pull request #32 from sharmine-s/bugfix-avatar
sharmine-s Jun 15, 2023
e111d9b
Merge remote-tracking branch 'origin/main' into cy-testing
camybish Jun 15, 2023
439be49
Adding extra files
camybish Jun 15, 2023
9f357d1
Figuring out how to mock an image upload
camybish Jun 15, 2023
694cc38
comment testing
hanguyen21 Jun 15, 2023
150762f
comments tested, uploads not
camybish Jun 15, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ typings/
# cypress.io
cypress/screenshots
cypress/videos

# .DS_Store
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The signature is created using a 'secret', which must be kept private (i.e. not
Here, we've used an environment variable called `JWT_SECRET`, which you'll see used in the commands to start the application and run the tests (below). You can change the value of that environment variable to anything you like.
## Card wall

REPLACE THIS TEXT WITH A LINK TO YOUR CARD WALL
[Trello](https://trello.com/b/ucgll1v8/acebook-airbenders)

## Quickstart

Expand Down
Binary file removed api/.DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const tokenChecker = (req, res, next) => {
};

// route setup
app.use("/posts", tokenChecker, postsRouter);
app.use("/posts", tokenChecker, postsRouter); // tokenChecker is a middleware function
app.use("/tokens", tokensRouter);
app.use("/users", usersRouter);

// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
next(createError(404));
});

// error handler
Expand Down
75 changes: 67 additions & 8 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,84 @@
const Post = require("../models/post");
const Comment = require("../models/comment");
const TokenGenerator = require("../models/token_generator");

const PostsController = {
Index: (req, res) => {
Post.find(async (err, posts) => {
// .find gets data out, .populate adds the referenced user fields
// sort will take an object with a format of {property: sort, property: sort}
// sort = -1 is descending, sort = 1 is ascending order
// in the current format, if time is exactly the same it will sort alphabetically by message
// note: (this is purely for the proof of concept, sounds silly to take into account)
Post.find()
.populate({ path: "user", select: ["name", "avatar"] })
.populate({
path: "comments",
populate: {
path: "user",
select: ["name", "avatar"],
},
})
.sort({ time: -1, message: 1 })
.exec((err, posts) => {
if (err) {
throw err;
}
const token = TokenGenerator.jsonwebtoken(req.user_id);
// .json() on the backend sends an http response containing a json.
res.status(200).json({ posts: posts, token: token });
});
},

Create: (req, res) => {
const post = new Post(req.body);
post.user = req.user_id;

post.save(err => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(200).json({ posts: posts, token: token });
const token = TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "OK", token: token });
});
},
Create: (req, res) => {
const post = new Post(req.body);
post.save(async (err) => {

Update: (req, res) => {
// .findOneAndUpdate(filter, changes, return function)
Post.findOneAndUpdate({ _id: req.body.postId }, { likes: req.body.likes }, async (err, posts) => {
if (err) {
throw err;
}
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "Post liked", token: token });
});
},

CreateComment: (req, res) => {
// POST http://localhost:8080/posts/comments
// Header - Authorization: "bearer {token}"
// Body - { "postId": "64887a8097403dcf437532d0", "message": "i am comment" }

const comment = new Comment(req.body);
comment.user = req.user_id;

comment.save((err, comment) => {
if (err) {
throw err;
}
const token = TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ message: "Comment posted", commentId: comment._id });
});
},

UpdatePost: (req, res) => {
// Comments array inside Post contains a list of comment IDs
// PATCH http://localhost:8080/posts/comments
// Header - Authorization: "bearer {token}"
// Body = {"postId": "64887a8097403dcf437532d0", "commentId": "64888985ee20f9d200ee9e0a"}

const token = await TokenGenerator.jsonwebtoken(req.user_id)
res.status(201).json({ message: 'OK', token: token });
Post.findOneAndUpdate({ _id: req.body.postId }, { $addToSet: { comments: req.body.commentId } }, { new: true }).then(post => {
const token = TokenGenerator.jsonwebtoken(req.user_id);
res.status(201).json({ post: post, token: token });
});
},
};
Expand Down
13 changes: 7 additions & 6 deletions api/controllers/tokens.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const User = require("../models/user");
const TokenGenerator = require("../models/token_generator")
const bcrypt = require('bcrypt');


const SessionsController = {

Expand All @@ -9,17 +11,16 @@ const SessionsController = {

User.findOne({ email: email }).then(async (user) => {
if (!user) {
console.log("auth error: user not found")
res.status(401).json({ message: "auth error" });
} else if (user.password !== password) {
console.log("auth error: passwords do not match")
res.status(401).json({ message: "auth error" });
res.status(401).json({ message: "auth error, user does not exist" });
} else if (await bcrypt.compare(password, user.password) === false) {
res.status(401).json({ message: "auth error, password incorrect" });
} else {
const token = await TokenGenerator.jsonwebtoken(user.id)
const token = TokenGenerator.jsonwebtoken(user.id)
res.status(201).json({ token: token, message: "OK" });
}
});
}
};


module.exports = SessionsController;
8 changes: 6 additions & 2 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ const UsersController = {
const user = new User(req.body);
user.save((err) => {
if (err) {
res.status(400).json({message: 'Bad request'})
if (err.code === 11000) {
res.status(409).json({ message: "Email already exists. Please choose a different email." });
} else {
res.status(400).json({ message: "Bad request" });
}
} else {
res.status(201).json({ message: 'OK' });
res.status(201).json({ message: "OK" });
}
});
},
Expand Down
12 changes: 12 additions & 0 deletions api/models/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const CommentSchema = new Schema({
message: String,
time: Date,
user: { type: Schema.Types.ObjectId, ref: "User" },
});

const Comment = mongoose.model("Comment", CommentSchema);

module.exports = Comment;
12 changes: 9 additions & 3 deletions api/models/post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;

const PostSchema = new mongoose.Schema({
message: String
const PostSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: "User" },
message: String,
imageUrl: String,
time: Date,
likes: { type: [String], default: [] },
comments: [{ type: Schema.Types.ObjectId, ref: "Comment", default: [] }],
});

const Post = mongoose.model("Post", PostSchema);
const Post = mongoose.model("Post", PostSchema)

module.exports = Post;
15 changes: 13 additions & 2 deletions api/models/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
const mongoose = require("mongoose");
const { Schema } = mongoose;
const bcrypt = require('bcrypt');

const UserSchema = new mongoose.Schema({
email: { type: String, required: true },
const UserSchema = new Schema({
name: { type: String, required: true, default: "Name" },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
avatar: { type: String, default: '/default_avatar.png'}
});

UserSchema.pre('save', async function (next) {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(this.password, salt);
this.password = hashedPassword;
next();
});

const User = mongoose.model("User", UserSchema);
Expand Down
Loading