<commit_before>'use strict'

const Core = require('./../core')
const User = require('./../models').User
const _ = require('lodash')
const Joi = require('joi')
const Boom = require('boom')
const Promise = require('when')
let Users

/**
 * User controller: web routes related to user stuff get routed to this controller
 * Handles the session configuration and rendering of views with data
 */
Users = {
  showSignup: {
    handler: function (request, reply) {
      if (request.auth.isAuthenticated) {
        return reply.redirect('/profile')
      }

      return reply.view('signup')
    },
    auth: {
      mode: 'try',
      strategy: 'session'
    },
    plugins: {
      'hapi-auth-cookie': {
        redirectTo: false
      }
    }
  },

  signup: {
    handler: function (request, reply) {
      if (request.auth.isAuthenticated) {
        return reply.redirect('/profile')
      }

      const payload = request.payload

      return User.findByEmail(payload.email).then(function (user) {
        if (!_.isEmpty(user)) {
          return Promise.reject(Boom.conflict('E-Mail address is already registered.'))
        }

        user = new User({
          email: payload.email,
          password: <PASSWORD>,
          scope: [ 'user' ]
        })

        return user.generatePassword()
      }).then(function (user) {
        return user.save()
      }).then(function (user) {
        request.cookieAuth.set(user)
        return reply.redirect('/profile')
      }).catch(function (error) {
        console.log(error)
        return reply.view('signup', { errormessage: error.output.payload.message }).code(400)
      })
    },
    validate: {
      payload: {
        email: Joi.string().required().label('Email address'),
        password: Joi.string().required().min(6).label('Password')
      },
      options: {
        abortEarly: false
      },
      failAction: function (request, reply, source, error) {
        // extracts the key which caused the failed validation ("email" or "password")
        const errorKey = error.data.details[ 0 ].path
        error[ errorKey ] = {
          // Use the Joi error message
          message: error.data.details[ 0 ].message
        }

        const values = error.data._object
        const data = {
          values: values,
          errors: error
        }

        console.log(data)

        return reply.view('signup', data).code(400)
      }
    }
  },
<commit_msg>improve reply status for signup error<commit_after>'use strict'

const Core = require('./../core')
const User = require('./../models').User
const _ = require('lodash')
const Joi = require('joi')
const Boom = require('boom')
const Promise = require('when')
let Users

/**
 * User controller: web routes related to user stuff get routed to this controller
 * Handles the session configuration and rendering of views with data
 */
Users = {
  showSignup: {
    handler: function (request, reply) {
      if (request.auth.isAuthenticated) {
        return reply.redirect('/profile')
      }

      return reply.view('signup')
    },
    auth: {
      mode: 'try',
      strategy: 'session'
    },
    plugins: {
      'hapi-auth-cookie': {
        redirectTo: false
      }
    }
  },

  signup: {
    handler: function (request, reply) {
      if (request.auth.isAuthenticated) {
        return reply.redirect('/profile')
      }

      const payload = request.payload

      return User.findByEmail(payload.email).then(function (user) {
        if (!_.isEmpty(user)) {
          return Promise.reject(Boom.conflict('E-Mail address is already registered.'))
        }

        user = new User({
          email: payload.email,
          password: <PASSWORD>,
          scope: [ 'user' ]
        })

        return user.generatePassword()
      }).then(function (user) {
        return user.save()
      }).then(function (user) {
        request.cookieAuth.set(user)
        return reply.redirect('/profile')
      }).catch(function (error) {
        const status = error.isBoom ? error.output.statusCode : 400
        return reply.view('signup', { errormessage: error.output.payload.message }).code(status)
      })
    },
    validate: {
      payload: {
        email: Joi.string().required().label('Email address'),
        password: <PASSWORD>.<PASSWORD>().<PASSWORD>().min(6).label('Password')
      },
      options: {
        abortEarly: false
      },
      failAction: function (request, reply, source, error) {
        // extracts the key which caused the failed validation ("email" or "password")
        const errorKey = error.data.details[ 0 ].path
        error[ errorKey ] = {
          // Use the Joi error message
          message: error.data.details[ 0 ].message
        }

        const values = error.data._object
        const data = {
          values: values,
          errors: error
        }

        console.log(data)

        return reply.view('signup', data).code(400)
      }
    }
  },

{'fraction_non_alphanumeric': 0.13442490398221144, 'fraction_numerical': 0.0036385688295936932, 'mean_word_length': 2.165706973768394, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086606', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1382, 'n_words': 353}