Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ########### from my custom functions User_fns file ##########
- const Promise = require('bluebird');
- // knex
- const dotenv = require("dotenv").config({ path: '../.env'}); // access .env dotfile settings
- const knex = require('knex');
- const environment = process.env.NODE_ENV;
- const knex_config = require('../knexfile');
- const database = require('knex')(knex_config[environment]);
- const bcrypt = require('bcrypt');
- const saltRounds = 10;
- // const createUser = (newUser, callback) => {
- // // your method logic
- // bcrypt.genSalt(saltRounds, function(err, salt) {
- // bcrypt.hash(newUser.plaintextPassword, salt, function(err, hash) {
- // // Store hash in your password DB.
- // console.log('hashed pw is', hash)
- // });
- // });
- // }
- const hashPassword = (password) => {
- return Promise.try(() => {
- bcrypt.hash(password, saltRounds, (err, hash) => {
- if(err){
- console.log(err)
- } else {
- // console.log(hash)
- return hash
- }
- })
- })
- .catch(function(err) {
- throw('error on attempt to hashPassword', err)
- })
- }
- const getUserByEmail = (email) => {
- return Promise.try(() => {
- return database("users").where({ email: email });
- }).then(function(results){
- if(results.length !== 0){
- console.log('there are results from checkUser_EmailExists, results object is:', results)
- return results
- } else {
- console.log('there are no results from checkUser_EmailExists, results object is:', results)
- return results
- }
- })
- .catch(function(err) {
- throw('error on attempt to checkUser_EmailExists', err)
- })
- }
- const createUser = (username, email, hashed_password) => {
- // our default usertype is 'general-hasNot-applied'
- return Promise.try(() => {
- return database('users').insert([
- { username: username,
- email: email,
- hashed_password: hashed_password,
- user_type: 'general-hasNot-applied'
- }
- ])
- .catch(function(err) {
- throw('error on attempt to createUser', err)
- })
- })
- }
- module.exports = {
- hashPassword,
- getUserByEmail,
- createUser
- };
- // ########### from http-post /register ############
- if(mapErrors_noErrors == true) {
- console.log('notice: there are no errors from validation result');
- Promise.try(() => {
- return User_fns.getUserByEmail(req.body.email);
- }).then((user) => {
- // an empty user object appears as empty array: []
- if (user === undefined || user.length == 0) {
- console.log('user obj is empty or does not exist');
- console.log('pw avail?', req.body.password);
- var password = req.body.password;
- return Promise.try(() => {
- console.log('check pw', password)
- return User_fns.hashPassword(password);
- }).then((returnedThing) => {
- console.log(returnedThing)
- req.flash('success', 'Thanks for registering.')
- // User_fns.createUser(req.body.username, req.body.email, hashed_password)
- res.render('pages/register', {
- data: req.body, // { message, email }
- errors: errors.mapped(),
- env: process.env.NODE_ENV
- })
- })
- } else {
- console.log('User object exists already, so we will notify visitor', user)
- 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.')
- res.render('pages/register', {
- data: req.body, // { message, email }
- errors: errors.mapped(),
- env: process.env.NODE_ENV
- })
- }
- }).catch((err) => {
- console.log('we have an err', err)
- });
- } else {
- // This will push data to the template to make it highlight the error fields
- console.log('notice: there ARE errors from validation result');
- res.render('pages/register', {
- data: req.body, // { message, email }
- errors: errors.mapped(),
- env: process.env.NODE_ENV
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement