update code

master
NguyễnLêTrung 2 years ago
parent 6adde976d5
commit b45009f6fb
  1. 1
      package.json
  2. 33
      src/api/controllers/v1/auth.controller.js
  3. 62
      src/api/controllers/v1/path.controller.js
  4. 30
      src/api/middlewares/authen.middleware.js
  5. 8
      src/api/routes/v1/auth.route.js
  6. 6
      src/config/locales/vi.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",

@ -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);
});
}
};

@ -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,23 +172,39 @@ 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);
}

@ -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();
};

@ -35,4 +35,12 @@ router.route('/login-token')
middleware.genarateToken,
controller.loginToken
);
router.route("/:id/reset-password").
post(
middleware.load,
middleware.checkCurrentPassword,
controller.updatePassword
);
export default router;

@ -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"
}
Loading…
Cancel
Save