Advertisement
Guest User

Untitled

a guest
Jul 20th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. // =========================================================================
  2. // LOCAL SIGNUP ============================================================
  3. // =========================================================================
  4. // we are using named strategies since we have one for login and one for signup
  5. // by default, if there was no name, it would just be called 'local'
  6.  
  7. passport.use('local-signup', new LocalStrategy({
  8. // by default, local strategy uses username and password, we will override with email
  9. firstNameField: 'firstName',
  10. lastNameField: 'lastName',
  11. usernameField: 'email',
  12. passwordField: 'password',
  13. jobTitleField: 'jobTitle',
  14. startDateField: 'startDate',
  15. passReqToCallback: true // allows us to pass back the entire request to the callback
  16. },
  17.  
  18. function(req, email, password, done) {
  19.  
  20. // find a user whose email is the same as the forms email
  21. // we are checking to see if the user trying to login already exists
  22. User.findOne({'email': email}, function(err, user) {
  23. // if there are any errors, return the error
  24. if (err)
  25. return done(err);
  26.  
  27. // check to see if theres already a user with that email
  28. if (user) {
  29. return done(null, false, {
  30. message: 'That email is already taken.'
  31. });
  32. }
  33. else { var token = crypto.randomBytes().toString();
  34. // if there is no user with that email
  35. // create the user
  36. var newUser = new User();
  37.  
  38. // set the user's local credentials
  39. newUser.firstName = req.body.firstName;
  40. newUser.lastName = req.body.lastName;
  41. newUser.email = email;
  42. newUser.password = newUser.generateHash(password); // use the generateHash function in our user model
  43. newUser.jobTitle = req.body.jobTitle;
  44. newUser.startDate = req.body.startDate;
  45. newUser.birthday = req.body.birthday;
  46. newUser.region = req.body.region;
  47. newUser.sector = req.body.sector;
  48. newUser.accountConfirmationToken = token;
  49. newUser.accountConfirmationTokenExpires = Date.now() + 3600000;
  50. newUser.accountVerified = 'false';
  51. newUser.isLineManager = 'false';
  52.  
  53. // save the user
  54. newUser.save(function(err) {
  55. if (err)
  56. throw err;
  57. else {
  58.  
  59. var data = {
  60. from: 'system',
  61. to: email,
  62. subject: 'Account Verification',
  63. text: 'You recently registered onto the App, to gain access to your account please verify your account.nn' +
  64. 'Please click on the following link, or paste this into your browser to complete the process:nn' +
  65. 'http://' + req.headers.host + '/verify/' + token + 'nn'
  66. };
  67.  
  68. mailgun.messages().send(data, function(error, body) {
  69. console.log(body);
  70. console.log("setting token 1");
  71. req.flash('info', 'An e-mail has been sent to ' + email + ' with further instructions.');
  72. });
  73. return done(null, newUser);
  74. }
  75. });
  76. }
  77.  
  78. });
  79.  
  80. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement