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_create.node.js 896B

123456789101112131415161718192021222324252627282930313233343536373839
  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. if (!/^[a-zA-Z0-9_\-]+$/.test(data.username))
  9. return ctx.fail("Username contains illegal characters.");
  10. var params = scrypt.paramsSync(ctx.conf.scrypt.maxtime);
  11. scrypt.kdf(new Buffer(data.password), params, function(err, hash) {
  12. if (err)
  13. return ctx.fail(err);
  14. ctx.db.query(
  15. "INSERT INTO users (username, pass_hash) "+
  16. "VALUES ($1, $2) "+
  17. "RETURNING id",
  18. [data.username, hash.toString("hex")],
  19. queryCallback
  20. );
  21. });
  22. });
  23. function queryCallback(err, res) {
  24. if (err)
  25. return ctx.fail(err);
  26. ctx.login(ctx.postData.data.username, res.rows[0].id);
  27. ctx.succeed({
  28. id: res.rows[0].id
  29. });
  30. }
  31. }