Advertisement
Guest User

Untitled

a guest
May 28th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. // ########## user_fns.js
  2.  
  3. const hashPassword = (password) => {
  4.  
  5. bcrypt.hash(password, saltRounds, (err, hash) => {
  6. if(err){
  7. console.log(err)
  8. } else {
  9. // console.log(hash)
  10. return hash
  11. }
  12. })
  13. }
  14.  
  15.  
  16. const getUserByEmail = (email) => {
  17. return Promise.try(() => {
  18. return database("users").where({ email: email });
  19. }).then(function(results){
  20. if(results.length !== 0){
  21. console.log('there are results from checkUser_EmailExists, results object is:', results)
  22. return results
  23. } else {
  24. console.log('there are no results from checkUser_EmailExists, results object is:', results)
  25. return results
  26. }
  27. })
  28. .catch(function(err) {
  29. throw('error on attempt to checkUser_EmailExists', err)
  30. })
  31. }
  32.  
  33. const createUser = (username, email, hashed_password) => {
  34.  
  35. // our default usertype is 'general-hasNot-applied'
  36. return Promise.try(() => {
  37. database('users').insert([
  38. { username: username,
  39. email: email,
  40. hashed_password: hashed_password,
  41. user_type: 'general-hasNot-applied'
  42. }
  43. ])
  44. .catch(function(err) {
  45. throw('error on attempt to createUser', err)
  46. })
  47. })
  48. }
  49.  
  50.  
  51.  
  52. // ######### excerpt from the API file that uses the above functions
  53. return Promise.try(() => {
  54. // Here, we return the result of calling our own function. That return value is a Promise.
  55. return User_fns.getUserByEmail(req.body.email);
  56. }).then((user) => {
  57. // B - if user obj does not exist, we can safely create one
  58. // an empty user object appears as empty array: []
  59. if (user === undefined || user.length == 0) {
  60. // array empty or does not exist
  61. console.log('user obj is empty or does not exist');
  62. // safe to create user, b/c this one we searched for does not exist.
  63. // - B1. hash pw.
  64. console.log('pw avail?', req.body.password);
  65. var password = req.body.password;
  66.  
  67. var hashed_password = User_fns.hashPassword(password);
  68.  
  69. console.log('hashed_password 123?', hashed_password)
  70.  
  71.  
  72. User_fns.createUser(req.body.username, req.body.email, hashed_password)
  73. req.flash('success', 'Thanks for registering.')
  74.  
  75. res.render('pages/register', {
  76. data: req.body, // { message, email }
  77. errors: errors.mapped(),
  78. env: process.env.NODE_ENV
  79. })
  80. // - B2. insert user into users table.
  81.  
  82. // passing in the request objects from form, and hashed_password
  83.  
  84. // should even pass in some of their info, such as username, so its clear that it worked
  85. // - B3. flash confirmation
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement