-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution 4.15: bloglist expansion, step3
- Loading branch information
1 parent
28c0b06
commit 5ec001c
Showing
9 changed files
with
573 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const bcrypt = require('bcrypt') | ||
const usersRouter = require('express').Router() | ||
const User = require('../models/user') | ||
|
||
usersRouter.post('/', async (request, response) => { | ||
const body = request.body | ||
|
||
const saltRounds = 10 | ||
const passwordHash = await bcrypt.hash(body.password, saltRounds) | ||
|
||
const user = new User({ | ||
username: body.username, | ||
name: body.name, | ||
passwordHash, | ||
}) | ||
|
||
const savedUser = await user.save() | ||
|
||
response.json(savedUser) | ||
}) | ||
|
||
usersRouter.get('/', async (request, response) => { | ||
const users = await User.find({}) | ||
response.json(users) | ||
}) | ||
|
||
module.exports = usersRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const mongoose = require('mongoose') | ||
const uniqueValidator = require('mongoose-unique-validator') | ||
|
||
const userSchema = new mongoose.Schema({ | ||
username: { | ||
type: String, | ||
unique: true, | ||
}, | ||
name: String, | ||
passwordHash: String, | ||
notes: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Blog', | ||
}, | ||
], | ||
}) | ||
|
||
userSchema.plugin(uniqueValidator) | ||
|
||
userSchema.set('toJSON', { | ||
transform: (document, returnedObject) => { | ||
returnedObject.id = returnedObject._id.toString() | ||
delete returnedObject._id | ||
delete returnedObject.__v | ||
// the passwordHash should not be revealed | ||
delete returnedObject.passwordHash | ||
}, | ||
}) | ||
|
||
const User = mongoose.model('User', userSchema) | ||
|
||
module.exports = User |
Oops, something went wrong.