Simple image host.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

account_change_password.node.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var scrypt = require("scrypt");
  2. module.exports = function(ctx) {
  3. ctx.getPostData(function(err, data) {
  4. if (err)
  5. return ctx.fail(err);
  6. if (!data.oldPassword || !data.newPassword)
  7. return ctx.fail("You must provide passwords.");
  8. if (!ctx.session.loggedIn)
  9. return ctx.fail("You're not logged in.");
  10. ctx.db.query(
  11. "SELECT id, pass_hash "+
  12. "FROM users "+
  13. "WHERE id = $1",
  14. [ctx.session.userId],
  15. queryCallback
  16. );
  17. });
  18. function queryCallback(err, res) {
  19. if (err)
  20. return ctx.fail(err);
  21. var user = res.rows[0];
  22. if (!user)
  23. return ctx.fail("User doesn't exist.");
  24. scrypt.verify(
  25. new Buffer(user.pass_hash, "hex"),
  26. new Buffer(ctx.postData.data.oldPassword),
  27. function(err, success) {
  28. if (!success)
  29. return ctx.fail("Wrong password.");
  30. updatePassword();
  31. }
  32. );
  33. }
  34. function updatePassword() {
  35. var params = scrypt.params(ctx.conf.scrypt.maxtime);
  36. scrypt.hash(
  37. new Buffer(ctx.postData.data.newPassword),
  38. params,
  39. function(err, hash) {
  40. if (err)
  41. return ctx.fail(err);
  42. ctx.db.query(
  43. "UPDATE users "+
  44. "SET pass_hash = $1 "+
  45. "WHERE id = $2",
  46. [hash.toString("hex"), ctx.session.userId],
  47. function(err, res) {
  48. if (err)
  49. return ctx.fail(err);
  50. ctx.succeed();
  51. }
  52. );
  53. }
  54. );
  55. }
  56. }