users.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict';
  2. const async = require('async');
  3. const config = require('config');
  4. const request = require('request');
  5. const bcrypt = require('bcrypt');
  6. const db = require('../db');
  7. const cache = require('../cache');
  8. const utils = require('../utils');
  9. module.exports = {
  10. login: (session, identifier, password, cb) => {
  11. async.waterfall([
  12. // check if a user with the requested identifier exists
  13. (next) => db.models.user.findOne({
  14. $or: [{ 'username': identifier }, { 'email.address': identifier }]
  15. }, next),
  16. // if the user doesn't exist, respond with a failure
  17. // otherwise compare the requested password and the actual users password
  18. (user, next) => {
  19. if (!user) return next(true, { status: 'failure', message: 'User not found' });
  20. bcrypt.compare(password, user.services.password.password, (err, match) => {
  21. if (err) return next(err);
  22. // if the passwords match
  23. if (match) {
  24. // store the session in the cache
  25. let sessionId = utils.guid();
  26. cache.hset('sessions', sessionId, cache.schemas.session());
  27. next(null, { status: 'success', message: 'Login successful', user, sessionId: sessionId });
  28. }
  29. else {
  30. next(null, { status: 'failure', message: 'User not found' });
  31. }
  32. });
  33. }
  34. ], (err, payload) => {
  35. // log this error somewhere
  36. if (err && err !== true) {
  37. console.error(err);
  38. return cb({ status: 'error', message: 'An error occurred while logging in' });
  39. }
  40. cb(payload);
  41. });
  42. },
  43. register: (session, username, email, password, recaptcha, cb) => {
  44. async.waterfall([
  45. // verify the request with google recaptcha
  46. (next) => {
  47. request({
  48. url: 'https://www.google.com/recaptcha/api/siteverify',
  49. method: 'POST',
  50. form: {
  51. //'secret': config.get("apis.recaptcha.secret"),
  52. 'response': recaptcha
  53. }
  54. }, next);
  55. },
  56. // check if the response from Google recaptcha is successful
  57. // if it is, we check if a user with the requested username already exists
  58. (response, body, next) => {
  59. console.log(456);
  60. let json = JSON.parse(body);
  61. console.log(json);
  62. //if (json.success !== true) return next('Response from recaptcha was not successful');
  63. db.models.user.findOne({ 'username': username }, next);
  64. },
  65. // if the user already exists, respond with that
  66. // otherwise check if a user with the requested email already exists
  67. (user, next) => {
  68. console.log(234);
  69. if (user) return next(true, { status: 'failure', message: 'A user with that username already exists' });
  70. db.models.user.findOne({ 'email.address': email }, next);
  71. },
  72. // if the user already exists, respond with that
  73. // otherwise, generate a salt to use with hashing the new users password
  74. (user, next) => {
  75. console.log(853);
  76. if (user) return next(true, { status: 'failure', message: 'A user with that email already exists' });
  77. bcrypt.genSalt(10, next);
  78. },
  79. // hash the password
  80. (salt, next) => {
  81. console.log(4682);
  82. bcrypt.hash(password, salt, next)
  83. },
  84. // save the new user to the database
  85. (hash, next) => {
  86. console.log(6842);
  87. db.models.user.create({
  88. username: username,
  89. email: {
  90. address: email,
  91. verificationToken: utils.generateRandomString(64)
  92. },
  93. services: {
  94. password: {
  95. password: hash
  96. }
  97. }
  98. }, next);
  99. },
  100. // respond with the new user
  101. (newUser, next) => {
  102. console.log(21465);
  103. next(null, { status: 'success', user: newUser })
  104. }
  105. ], (err, payload) => {
  106. console.log(476123123);
  107. // log this error somewhere
  108. if (err && err !== true) {
  109. console.error(err);
  110. return cb({ status: 'error', message: 'An error occurred while registering for an account' });
  111. }
  112. // respond with the payload that was passed to us earlier
  113. cb(payload);
  114. });
  115. },
  116. logout: (session, cb) => {
  117. if (!session) return cb({ status: 'failure', message: `You're not currently logged in` });
  118. //TODO Remove session
  119. session = null;
  120. cb({ status: 'success', message: `You've been successfully logged out` });
  121. },
  122. findByUsername: (session, username, cb) => {
  123. db.models.user.find({ username }, (err, account) => {
  124. if (err) throw err;
  125. account = account[0];
  126. cb({
  127. status: 'success',
  128. data: {
  129. username: account.username,
  130. createdAt: account.createdAt,
  131. statistics: account.statistics
  132. }
  133. });
  134. });
  135. }
  136. };