/* global __dirname:true, process:true */

/**
 * Module dependencies
 */

var oidc             = require('../oidc')
  , settings         = require('../boot/settings')
  , mailer           = require('../boot/mailer')
  , passwordProvider = require('../providers').password
  , passport         = require('passport')
  , qs               = require('qs')
  , User             = require('../models/User')
  , nodemailer       = require('nodemailer')
  , path             = require('path')
  , url              = require('url')
  , templatesDir     = path.resolve(process.cwd(), 'email')
  , consolidate      = require('consolidate')
  , PasswordsDisabledError = require('../errors/PasswordsDisabledError')
  ;


/**
 * Signup Endpoint
 */

module.exports = function (server) {

  /**
   * Signup page
   */

  var getSignupHandler = [
    oidc.selectConnectParams,
    oidc.validateAuthorizationParams,
    oidc.verifyClient,
    function (req, res, next) {
      res.render('signup', {
        params:    qs.stringify(req.query),
        request:   req.query,
        providers: settings.providers
      });
    }
  ];


  /**
   * Password signup handler
   */

  function createUser(req, res, next) {
    User.insert(req.body, { private: true }, function (err, user) {
      if (err) {
        res.render('signup', {
          params:    qs.stringify(req.body),
          request:   req.body,
          providers: settings.providers,
          error:     err.message
        });
      } else {
        passport.authenticate('password', function (err, user, info) {
          if (!user) {
          } else {
            req.login(user, function (err) {
              next(err);
            });
          }
        })(req, res, next);
      }
    });
  }

  function sendVerificationEmail(req, res, next) {
    if (!passwordProvider.emailVerification.enable) {
      next();
    } else {

      var locals = {
        email: req.user.email,
        name: {
          first: req.user.givenName,
          last: req.user.familyName
        },
        verifyURL: url.resolve(settings.issuer, 'email/verify?token=' + req.user.emailVerifyToken)
      };

      mailer.sendMail('verifyEmail', locals, {

        from: mailer.from,
        to: locals.email,
        subject: 'Verify your e-mail address'

      }, function(err, responseStatus) {

        if (err) {
          return next(err);
        } else {
          next();
        }

      });
    }
  }

  var postSignupHandler = [
    oidc.selectConnectParams,
    oidc.validateAuthorizationParams,
    oidc.verifyClient,
    createUser,
    sendVerificationEmail,
    oidc.determineUserScope,
    oidc.promptToAuthorize,
    oidc.authorize
  ];


  if (oidc.beforeAuthorize) {
    postSignupHandler.splice(postSignupHandler.length - 1, 0, oidc.beforeAuthorize);
  }


  /**
   * Passwords Disabled Handler
   */

  function passwordsDisabledHandler (req, res, next) {
    next(new PasswordsDisabledError());
  }


  // Only register the password signup handlers
  // if the password protocol is enabled.
  if (settings.providers.password) {
    server.get('/signup', getSignupHandler);
    server.post('/signup', postSignupHandler);
  } else {
    server.get('/signup', passwordsDisabledHandler);
    server.post('/signup', passwordsDisabledHandler);
  }

};




@@ -1883,435 +1883,53 @@
 e %7B%0A
-%0A
       
-var locals = %7B%0A        email: req.user.email,%0A        name: %7B%0A          first: req.user.givenName,%0A          last: req.user.familyName%0A        %7D,%0A        verifyURL: url.resolve(settings.issuer, 'email/verify?token=' + req.user.emailVerifyToken)%0A      %7D;%0A%0A      mailer.sendMail('verifyEmail', locals, %7B%0A%0A        from: mailer.from,%0A        to: locals.email,%0A        subject: 'Verify your e-mail address'%0A%0A      %7D, 
+req.user.sendVerificationEmail(
 function
 (err
@@ -1924,16 +1924,17 @@
 function
+ 
 (err, re
@@ -1953,101 +1953,26 @@
 ) %7B%0A
-%0A        if (err) %7B%0A          return next(err);%0A        %7D else %7B%0A          next();%0A        %7D%0A
+        next(err);
 %0A   

Use user.sendVerificationEmail