Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

account_login.node.js 933B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.username || !data.password)
  7. return ctx.fail("You must provide a username and a password.");
  8. ctx.db.query(
  9. "SELECT id, username, pass_hash "+
  10. "FROM users "+
  11. "WHERE username=$1",
  12. [data.username],
  13. queryCallback
  14. );
  15. });
  16. function queryCallback(err, res) {
  17. if (err)
  18. return ctx.fail(err);
  19. var user = res.rows[0];
  20. ctx.session.loggedIn = true;
  21. ctx.session.userId = user.id;
  22. ctx.session.username = user.username;
  23. if (!user)
  24. return ctx.fail("Wrong username or password.");
  25. scrypt.verify(
  26. new Buffer(user.pass_hash, "hex"),
  27. new Buffer(ctx.postData.data.password),
  28. function(err, success) {
  29. if (success) {
  30. ctx.succeed({
  31. id: user.id
  32. })
  33. } else {
  34. ctx.fail("Wrong username or password.");
  35. }
  36. }
  37. );
  38. }
  39. }