Guest User

Untitled

a guest
Jan 15th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. passport.use('local-signup', new localStrategy(
  2. {
  3. usernameField : 'username',
  4. passwordField : 'password',
  5. passReqToCallback: true // allows us to pass back the entire request to the callback
  6. },
  7.  
  8. function(req, username, password, done) {
  9.  
  10. // asynchronous
  11. // userModel.findOne wont fire unless data is sent back
  12. process.nextTick(function() {
  13.  
  14. var recaptcha = nconf.get('recaptcha');
  15.  
  16. if (recaptcha && req.recaptcha.error) {
  17. return done(null, false, req.flash('error', 'The recaptcha could not be verified.'));
  18. }
  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. userModel.findOne({ 'local.username': username }, function(err, user) {
  23.  
  24. // if there are any errors, return the error
  25. if (err)
  26. console.log(err);
  27. // return done(err);
  28.  
  29. // check to see if there's already a user with that email
  30. if (user) {
  31. return done(null, false, req.flash('error', 'This user already exists.'));
  32. } else {
  33.  
  34. // if there is no user with that email
  35. // create the user
  36. var newUser = new userModel();
  37.  
  38. // set the user's local credentials
  39. newUser.local.username = username;
  40. newUser.local.password = newUser.generateHash(password);
  41. newUser.data.email = req.body.email;
  42. newUser.data.phone = req.body.phone;
  43.  
  44. // save the user
  45. newUser.save(function(err) {
  46. if (err)
  47. throw err;
  48. return done(null, newUser);
  49. });
  50. }
  51. });
  52. });
  53. })
  54. );
  55.  
  56. router.post('/register', middleware.recaptcha.verify, auth.register);
  57.  
  58. main.register = function (req, res, next) {
  59. var data = {
  60. error: req.flash('error')[ 0 ],
  61. recaptcha: req.recaptcha
  62. };
  63.  
  64. res.render('register', data);
  65. };
  66.  
  67. /*
  68. global module,
  69. require
  70. */
  71.  
  72. var mongoose = require('mongoose'),
  73. nconf = require('nconf'),
  74. path = require('path'),
  75. fs = require('fs'),
  76. bcrypt = require('bcrypt-nodejs');
  77.  
  78. // define the schema for our user model
  79. var userSchema = mongoose.Schema({
  80. local: {
  81. username: String,
  82. password: String
  83. },
  84.  
  85. settings: {
  86. showLastAuctions: Boolean,
  87. useGravatarImage: { type: Boolean, default: false }
  88. },
  89.  
  90. data: {
  91. admin: { type: Boolean, default: false },
  92. email: String,
  93. phone: String,
  94. profileImg: String,
  95. realname: String,
  96. birthday: String,
  97. registerDate: Date,
  98. status: String,
  99.  
  100. paymentUnits: Number,
  101.  
  102. auctions: [ {
  103. id: String,
  104. name: String,
  105. date: Date,
  106. endPrice: String
  107. } ],
  108.  
  109. connections: [ {
  110. remoteAddress: String,
  111. hostname: String,
  112. location: String,
  113. lastSeen: Date
  114. } ]
  115. }
  116. }//,{
  117. // collection: 'users'
  118. //}
  119. );
  120.  
  121.  
  122. module.exports = mongoose.model('User', userSchema);
Add Comment
Please, Sign In to add comment