-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate request body with ajv (#131)
* Fix withUser of undefined * Add validation using ajv * fix
- Loading branch information
Showing
12 changed files
with
261 additions
and
165 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,15 @@ | ||
export const ValidateProps = { | ||
user: { | ||
username: { type: 'string', minLength: 4, maxLength: 20 }, | ||
name: { type: 'string', minLength: 1, maxLength: 50 }, | ||
password: { type: 'string', minLength: 8 }, | ||
email: { type: 'string', minLength: 1 }, | ||
bio: { type: 'string', minLength: 0, maxLength: 160 }, | ||
}, | ||
post: { | ||
content: { type: 'string', minLength: 1, maxLength: 280 }, | ||
}, | ||
comment: { | ||
content: { type: 'string', minLength: 1, maxLength: 280 }, | ||
}, | ||
}; |
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,18 @@ | ||
import Ajv from 'ajv'; | ||
|
||
export function validateBody(schema) { | ||
const ajv = new Ajv(); | ||
const validate = ajv.compile(schema); | ||
return (req, res, next) => { | ||
const valid = validate(req.body); | ||
if (valid) { | ||
return next(); | ||
} else { | ||
console.log(validate.errors); | ||
const error = validate.errors[0]; | ||
return res | ||
.status(400) | ||
.end(`"${error.instancePath.substring(1)}" ${error.message}`); | ||
} | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { validateBody } from './ajv'; | ||
export { default as all } from './all'; | ||
export { default as auth } from './auth'; | ||
export { default as database } from './database'; |
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
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
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 |
---|---|---|
@@ -1,35 +1,47 @@ | ||
import { ValidateProps } from '@/api-lib/constants'; | ||
import { UNSAFE_findUserForAuth, updateUserById } from '@/api-lib/db'; | ||
import { all } from '@/api-lib/middlewares'; | ||
import { all, validateBody } from '@/api-lib/middlewares'; | ||
import { ncOpts } from '@/api-lib/nc'; | ||
import bcrypt from 'bcryptjs'; | ||
import nc from 'next-connect'; | ||
|
||
const handler = nc(ncOpts); | ||
handler.use(all); | ||
|
||
handler.put(async (req, res) => { | ||
if (!req.user) { | ||
res.json(401).send('you need to be authenticated'); | ||
return; | ||
} | ||
const { oldPassword, newPassword } = req.body; | ||
handler.put( | ||
validateBody({ | ||
type: 'object', | ||
properties: { | ||
oldPassword: ValidateProps.user.password, | ||
newPassword: ValidateProps.user.password, | ||
}, | ||
required: ['oldPassword', 'newPassword'], | ||
additionalProperties: false, | ||
}), | ||
async (req, res) => { | ||
if (!req.user) { | ||
res.json(401).send('you need to be authenticated'); | ||
return; | ||
} | ||
const { oldPassword, newPassword } = req.body; | ||
|
||
if ( | ||
!(await bcrypt.compare( | ||
oldPassword, | ||
( | ||
await UNSAFE_findUserForAuth(req.db, req.user._id, true) | ||
).password | ||
)) | ||
) { | ||
res.status(401).send('The password you has entered is incorrect.'); | ||
return; | ||
} | ||
const password = await bcrypt.hash(newPassword, 10); | ||
if ( | ||
!(await bcrypt.compare( | ||
oldPassword, | ||
( | ||
await UNSAFE_findUserForAuth(req.db, req.user._id, true) | ||
).password | ||
)) | ||
) { | ||
res.status(401).send('The password you has entered is incorrect.'); | ||
return; | ||
} | ||
const password = await bcrypt.hash(newPassword, 10); | ||
|
||
await updateUserById(req.db, req.user._id, { password }); | ||
await updateUserById(req.db, req.user._id, { password }); | ||
|
||
res.end('ok'); | ||
}); | ||
res.end('ok'); | ||
} | ||
); | ||
|
||
export default handler; |
Oops, something went wrong.
4f4f28a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: