Public API for a society manager application

user-controller.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var Redis = require("ioredis");
  2. var redis = new Redis();
  3. var auth_gen = require("./../utils/auth-keys.js");
  4. var permissions_controller = require("./permissions-controller.js");
  5. module.exports = {
  6. get_password: function (user, complete) {
  7. var user_key = "user:" + user;
  8. redis.hget(user_key, "password", function (err, password) {
  9. if (password) {
  10. complete(password);
  11. } else {
  12. complete("");
  13. }
  14. });
  15. },
  16. get_public_user_info: function (user, complete) {
  17. var user_key = "user:" + user;
  18. redis.hgetall(user_key, function(err, result) {
  19. var public = {};
  20. public.username = user;
  21. public.societies = result.societies || [];
  22. public.friends = result.friends || [];
  23. public.accepted_events = result.accepted_events || [];
  24. });
  25. },
  26. get_user_from_auth: function (auth, complete) {
  27. var auth_key = "auth-key:" + auth;
  28. redis.get(auth_key, function (err, username) {
  29. if (username) {
  30. complete(username);
  31. } else {
  32. complete("");
  33. }
  34. });
  35. },
  36. user_exists: function (user, complete) {
  37. var user_key = "user:" + user;
  38. redis.hgetall(user_key, function (err, result) {
  39. complete(!!result.password);
  40. });
  41. },
  42. authenticate: function (user, pass, complete) {
  43. permissions_controller.user_can_auth(user, pass, function (success) {
  44. var user_key = "user:" + user;
  45. if (success) {
  46. redis.hget(user_key, "auth-key", function (auth) {
  47. var new_auth_key = auth_gen.generate(user);
  48. if (auth) {
  49. redis.del("auth-key:" + auth);
  50. }
  51. redis.set("auth-key:" + auth, new_auth_key);
  52. redis.hset(user_key, "auth-key", new_auth_key);
  53. complete({
  54. "logged_in": 1,
  55. "auth-key": new_auth_key,
  56. "error": 0
  57. });
  58. });
  59. } else {
  60. complete({
  61. "logged_in": 0,
  62. "error": 1
  63. });
  64. }
  65. });
  66. },
  67. register: function (user, pass, complete) {
  68. var user_key = "user:" + user;
  69. var auth_key = "";
  70. var new_user = {};
  71. this.user_exists(user, function (exists) {
  72. if (exists) {
  73. complete({
  74. "registered": 0,
  75. "error": 1
  76. });
  77. } else {
  78. bcrypt.hash(pass, null, null, function (err, hash) {
  79. new_user["password"] = hash;
  80. new_user["auth-key"] = auth_gen.generate(user);
  81. auth_key = "auth-key:" + new_user["auth-key"];
  82. redis.hset(user_key, "password", new_user["password"]);
  83. redis.hset(user_key, "auth-key", new_user["auth-key"]);
  84. redis.hset(user_key, "societies", JSON.stringify([]));
  85. redis.hset(user_key, "friends", JSON.stringify([]));
  86. redis.hset(user_key, "pending_events", JSON.stringify([]));
  87. redis.hset(user_key, "accepted_events", JSON.stringify([]));
  88. redis.hset(user_key, "declined_events", JSON.stringify([]));
  89. redis.set(auth_key, user);
  90. complete({
  91. "registered": 1,
  92. "auth-key": new_user["auth-key"],
  93. "error": 0
  94. })
  95. });
  96. }
  97. })
  98. }
  99. }