Advertisement
Guest User

Untitled

a guest
Apr 6th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. // login returns 401 Unauthorized
  2.  
  3. function login(email, password, callback) {
  4. const mysql = require('mysql');
  5. const bcrypt = require('bcrypt');
  6.  
  7. const connection = mysql.createConnection({
  8. host: configuration.staging_host,
  9. user: configuration.staging_user,
  10. password: configuration.staging_password,
  11. database: configuration.staging_db
  12. });
  13.  
  14. connection.connect();
  15.  
  16. const query = 'SELECT id, email, encrypted_password FROM admins WHERE email = ?';
  17.  
  18. connection.query(query, [ email ], function(err, results) {
  19. if (err) return callback(err);
  20. if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
  21. const user = results[0];
  22.  
  23. bcrypt.compare(password, user.encrypted_password, function(err, isValid) {
  24. if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
  25.  
  26. callback(null, {
  27. user_id: user.id.toString(),
  28. // nickname: user.nickname,
  29. email: user.email
  30. });
  31. });
  32. });
  33. }
  34.  
  35.  
  36. // get by email, returns my email and id from the db
  37.  
  38. function getByEmail(email, callback) {
  39. const mysql = require('mysql');
  40.  
  41. const connection = mysql.createConnection({
  42. host: configuration.staging_host,
  43. user: configuration.staging_user,
  44. password: configuration.staging_password,
  45. database: configuration.staging_db
  46. });
  47.  
  48. connection.connect();
  49.  
  50. const query = 'SELECT id, email FROM admins WHERE email = ?';
  51.  
  52. connection.query(query, [ email ], function(err, results) {
  53. if (err || results.length === 0) return callback(err || null);
  54.  
  55. const user = results[0];
  56. callback(null, {
  57. user_id: user.id.toString(),
  58. // nickname: user.nickname,
  59. email: user.email
  60. });
  61. });
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement