From b45009f6fbb9c9c2ae222233848d8c00d5d7d7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85nL=C3=AATrung?= Date: Mon, 8 May 2023 11:47:37 +0700 Subject: [PATCH] update code --- package.json | 1 + src/api/controllers/v1/auth.controller.js | 33 ++++++++++++ src/api/controllers/v1/path.controller.js | 64 ++++++++++++++--------- src/api/middlewares/auth.middleware.js | 2 +- src/api/middlewares/authen.middleware.js | 30 +++++++++-- src/api/routes/v1/auth.route.js | 12 ++++- src/config/locales/vi.json | 6 ++- 7 files changed, 117 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index cc363b7..977454c 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "ioredis": "^4.14.0", "joi": "^10.4.1", "jsonwebtoken": "^8.5.1", + "jszip": "^3.10.1", "lodash": "^4.17.4", "method-override": "^2.3.8", "moment-timezone": "^0.5.13", diff --git a/src/api/controllers/v1/auth.controller.js b/src/api/controllers/v1/auth.controller.js index 58a696c..e8d29b9 100644 --- a/src/api/controllers/v1/auth.controller.js +++ b/src/api/controllers/v1/auth.controller.js @@ -1,5 +1,6 @@ import messages from '../../../config/messages'; +import { hash, compare } from 'bcryptjs'; import { handler as ErrorHandler } from '../../middlewares/error'; import User from '../../../common/models/user.model'; @@ -30,3 +31,35 @@ exports.loginToken = async (req, res, next) => res.json({ message: messages.CREATE_SUCCESS, data: { token: req.locals.token, data: req.locals.user } }); + + +exports.updatePassword = async (req, res, next) => { + const { current_password, new_password } = req.body; + // cónt + + const { user } = req.locals; + // console.log(user); + if (user) { + const rounds = 10; + const new_pass = await hash(new_password, rounds); + return User.update( + { password: new_pass }, + { + where: { + id: user.id + } + }, + + ).then(async () => { + const user = await User.get(12); + console.log(user); + res.json({ + test: 1, + code: 0, + message: messages.UPDATE_SUCCESS + }); + }).catch(ex => { + ErrorHandler(ex, req, res, next); + }); + } +}; \ No newline at end of file diff --git a/src/api/controllers/v1/path.controller.js b/src/api/controllers/v1/path.controller.js index 7de1364..a4da57f 100644 --- a/src/api/controllers/v1/path.controller.js +++ b/src/api/controllers/v1/path.controller.js @@ -104,16 +104,16 @@ exports.delete = (req, res, next) => { multer({ dest: `${dir}` }); const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri); const newpath = req.body.path.replace(cdnConfig.uri, storageConfig.uri_backup); - // fs.rm(path, { recursive: true }, err => { - // if (err) { - // return res.status(400).json({ code: 400, message: 'lỗi', detail: err }); - // } - // return res.json({ code: 0, message: 'success' }); - // }); - fs.rename(path, newpath, (err) => { - if (err) throw err; + fs.rm(path, { recursive: true }, err => { + if (err) { + return res.status(400).json({ code: 400, message: 'lỗi', detail: err }); + } return res.json({ code: 0, message: 'success' }); }); + // fs.rename(path, newpath, (err) => { + // if (err) throw err; + // return res.json({ code: 0, message: 'success' }); + // }); return null; } catch (ex) { return ErrorHandel(ex, req, res, next); @@ -172,24 +172,40 @@ exports.forceDelete = (req, res, next) => { } }; +// exports.deleteMultiple = (req, res, next) => { +// try { +// const user = req.user; +// const dir = `${storageConfig.uri_backup}/${user.id}`; +// multer({ dest: `${dir}` }); +// if (req.body.data) { +// req.body.data.forEach((e) => { +// const path = e.path.replace(cdnConfig.uri, storageConfig.uri); +// const newpath = e.path.replace(cdnConfig.uri, storageConfig.uri_backup); +// console.log(path, newpath); +// fs.rename(path, newpath, (err) => { +// if (err) throw err; +// return { code: 0, message: 'success' }; +// }); +// }); +// } +// return res.json({ code: 0, message: 'success' }); +// } catch (ex) { +// return ErrorHandel(ex, req, res, next); +// } +// }; + + exports.deleteMultiple = (req, res, next) => { try { - const user = req.user; - const dir = `${storageConfig.uri_backup}/${user.id}`; - multer({ dest: `${dir}` }); - if (req.body.data) { - req.body.data.forEach((e) => { - const path = e.path.replace(cdnConfig.uri, storageConfig.uri); - const newpath = e.path.replace(cdnConfig.uri, storageConfig.uri_backup); - console.log(path, newpath); - fs.rename(path, newpath, (err) => { - if (err) throw err; - return { code: 0, message: 'success' }; - }); - }); - } - return res.json({ code: 0, message: 'success' }); + const path = req.body.path.replace(cdnConfig.uri, storageConfig.uri); + fs.rm(path, { recursive: true }, err => { + if (err) { + return res.status(400).json({ code: 400, message: 'lỗi', detail: err }); + } + return res.json({ code: 0, message: 'success' }); + }); + return null; } catch (ex) { return ErrorHandel(ex, req, res, next); } -}; +}; \ No newline at end of file diff --git a/src/api/middlewares/auth.middleware.js b/src/api/middlewares/auth.middleware.js index 4fcc3da..f2997ef 100644 --- a/src/api/middlewares/auth.middleware.js +++ b/src/api/middlewares/auth.middleware.js @@ -328,4 +328,4 @@ module.exports = { getTokenInfo, authorize, checkStaffPermission -}; +}; \ No newline at end of file diff --git a/src/api/middlewares/authen.middleware.js b/src/api/middlewares/authen.middleware.js index 464e220..a00101d 100644 --- a/src/api/middlewares/authen.middleware.js +++ b/src/api/middlewares/authen.middleware.js @@ -7,6 +7,19 @@ import User from '../..//common/models/user.model'; /** * Load item by id add to req locals. */ + +exports.load = async (req, res, next) => { + try { + const user = await User.get(req.params.id); + req.locals = req.locals ? req.locals : {}; + req.locals.user = user; + return next(); + } catch (ex) { + return ErrorHandler(ex, req, res, next); + } +}; + + exports.checkEmail = async (req, res, next) => { try { const user = await User.getUserByPhoneOrEmailRegister({ email: req.body.email }); @@ -34,6 +47,7 @@ exports.loadUser = async (req, res, next) => { }; exports.checkPassword = async (req, res, next) => { const { user } = req.locals; + // console.log(user); const isCheck = await User.passwordMatches(user, req.body.password); if (!isCheck) { return res.status(400).json({ message: 'Email or password incorrect' }); @@ -45,9 +59,9 @@ exports.genarateToken = async (req, res, next) => { user = pick(req.locals.user, ['name', 'avatar', 'email', 'phone', 'service']); user.id = req.locals.user.id; - // req.locals = { - // user - // }; + req.locals = { + user + }; const inforToken = {}; inforToken.access_token = JWT.sign(user, process.env.NODE_ENV || 'development', { expiresIn: 60 * 60 }); @@ -55,6 +69,7 @@ exports.genarateToken = async (req, res, next) => { inforToken.access_expired_at = Moment.tz(new Date(), 'Asia/Ho_Chi_Minh').unix() + (60 * 60 * 24); inforToken.expRefreshTime = Moment.tz(new Date(), 'Asia/Ho_Chi_Minh').unix(); req.locals.token = inforToken; + console.log(req.locals.token); return next(); }; exports.loginGoogle = async (req, res, next) => { @@ -107,3 +122,12 @@ exports.loadUserViaThirdParty = async (req, res, next) => { } }; +exports.checkCurrentPassword = async (req, res, next) => { + const { user } = req.locals; + // console.log(user); + const isCheck = await User.passwordMatches(user, req.body.current_password); + if (!isCheck) { + return res.status(400).json({ message: 'Password incorrect'}); + } + return next(); +}; \ No newline at end of file diff --git a/src/api/routes/v1/auth.route.js b/src/api/routes/v1/auth.route.js index 0140ac8..7b9bc28 100644 --- a/src/api/routes/v1/auth.route.js +++ b/src/api/routes/v1/auth.route.js @@ -26,7 +26,7 @@ router.route('/login-password') middleware.checkPassword, middleware.genarateToken, controller.login - ); + ); router.route('/login-token') .post( validate(loginToken), @@ -35,4 +35,12 @@ router.route('/login-token') middleware.genarateToken, controller.loginToken ); -export default router; + +router.route("/:id/reset-password"). + post( + middleware.load, + middleware.checkCurrentPassword, + controller.updatePassword + ); + +export default router; \ No newline at end of file diff --git a/src/config/locales/vi.json b/src/config/locales/vi.json index 4e82dc0..70fad09 100644 --- a/src/config/locales/vi.json +++ b/src/config/locales/vi.json @@ -3,5 +3,9 @@ "Không tìm thấy tài khoản này!": "Không tìm thấy tài khoản này!", "Unexpected field": "Unexpected field", "File too large": "File too large", - "Invalid file!": "Invalid file!" + "Invalid file!": "Invalid file!", + "Forbidden": "Forbidden", + "Cannot read properties of undefined (reading 'replace')": "Cannot read properties of undefined (reading 'replace')", + "invalid input syntax for type integer: \"undefined\"": "invalid input syntax for type integer: \"undefined\"", + "Unauthorized": "Unauthorized" } \ No newline at end of file