Advertisement
Guest User

Untitled

a guest
May 29th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. // ########### from my custom functions User_fns file ##########
  2.  
  3. const Promise = require('bluebird');
  4.  
  5. // knex
  6. const dotenv = require("dotenv").config({ path: '../.env'}); // access .env dotfile settings
  7. const knex = require('knex');
  8. const environment = process.env.NODE_ENV;
  9. const knex_config = require('../knexfile');
  10. const database = require('knex')(knex_config[environment]);
  11.  
  12. const bcrypt = require('bcrypt');
  13. const saltRounds = 10;
  14.  
  15. // const createUser = (newUser, callback) => {
  16. // // your method logic
  17. // bcrypt.genSalt(saltRounds, function(err, salt) {
  18. // bcrypt.hash(newUser.plaintextPassword, salt, function(err, hash) {
  19. // // Store hash in your password DB.
  20. // console.log('hashed pw is', hash)
  21.  
  22. // });
  23. // });
  24. // }
  25.  
  26. const hashPassword = (password) => {
  27.  
  28. return Promise.try(() => {
  29. bcrypt.hash(password, saltRounds, (err, hash) => {
  30. if(err){
  31. console.log(err)
  32. } else {
  33. // console.log(hash)
  34. return hash
  35. }
  36. })
  37. })
  38. .catch(function(err) {
  39. throw('error on attempt to hashPassword', err)
  40. })
  41. }
  42.  
  43. const getUserByEmail = (email) => {
  44. return Promise.try(() => {
  45. return database("users").where({ email: email });
  46. }).then(function(results){
  47. if(results.length !== 0){
  48. console.log('there are results from checkUser_EmailExists, results object is:', results)
  49. return results
  50. } else {
  51. console.log('there are no results from checkUser_EmailExists, results object is:', results)
  52. return results
  53. }
  54. })
  55. .catch(function(err) {
  56. throw('error on attempt to checkUser_EmailExists', err)
  57. })
  58. }
  59.  
  60. const createUser = (username, email, hashed_password) => {
  61. // our default usertype is 'general-hasNot-applied'
  62. return Promise.try(() => {
  63. return database('users').insert([
  64. { username: username,
  65. email: email,
  66. hashed_password: hashed_password,
  67. user_type: 'general-hasNot-applied'
  68. }
  69. ])
  70. .catch(function(err) {
  71. throw('error on attempt to createUser', err)
  72. })
  73. })
  74. }
  75. module.exports = {
  76. hashPassword,
  77. getUserByEmail,
  78. createUser
  79. };
  80.  
  81. // ########### from http-post /register ############
  82. if(mapErrors_noErrors == true) {
  83. console.log('notice: there are no errors from validation result');
  84.  
  85.  
  86. Promise.try(() => {
  87. return User_fns.getUserByEmail(req.body.email);
  88. }).then((user) => {
  89.  
  90. // an empty user object appears as empty array: []
  91. if (user === undefined || user.length == 0) {
  92.  
  93. console.log('user obj is empty or does not exist');
  94.  
  95. console.log('pw avail?', req.body.password);
  96. var password = req.body.password;
  97.  
  98. return Promise.try(() => {
  99. console.log('check pw', password)
  100. return User_fns.hashPassword(password);
  101.  
  102. }).then((returnedThing) => {
  103. console.log(returnedThing)
  104.  
  105. req.flash('success', 'Thanks for registering.')
  106.  
  107. // User_fns.createUser(req.body.username, req.body.email, hashed_password)
  108.  
  109. res.render('pages/register', {
  110. data: req.body, // { message, email }
  111. errors: errors.mapped(),
  112. env: process.env.NODE_ENV
  113. })
  114.  
  115. })
  116.  
  117. } else {
  118. console.log('User object exists already, so we will notify visitor', user)
  119.  
  120. req.flash('success', 'Sorry, a user with that email address already exists. You may already have an account, or perhaps you mistyped your email address.')
  121.  
  122. res.render('pages/register', {
  123. data: req.body, // { message, email }
  124. errors: errors.mapped(),
  125. env: process.env.NODE_ENV
  126. })
  127. }
  128.  
  129. }).catch((err) => {
  130. console.log('we have an err', err)
  131. });
  132.  
  133.  
  134. } else {
  135. // This will push data to the template to make it highlight the error fields
  136. console.log('notice: there ARE errors from validation result');
  137. res.render('pages/register', {
  138. data: req.body, // { message, email }
  139. errors: errors.mapped(),
  140. env: process.env.NODE_ENV
  141. })
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement