Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. passport.use('local-signup', new LocalStrategy({
  2. passReqToCallback : true // allows us to pass back the entire request to the callback
  3. },
  4. function(req, username, password, email, name, info, done) {
  5.  
  6. function findOrCreateUser(){
  7. // find a user in Mongo with provided username
  8. User.findOne({ 'username' : username }, function(err, user) {
  9. // In case of any error, return using the done method
  10. if (err){
  11. console.log('Error in SignUp: '+err);
  12. return done(err);
  13. }
  14. // already exists
  15. if (user) {
  16. console.log('User already exists with username: '+username);
  17. return done(null, false, console.log('message','User Already Exists'));
  18. } else {
  19. // if there is no user with that email
  20. // create the user
  21. var newUser = new User();
  22.  
  23. // set the user's local credentials
  24. newUser.username = username;
  25. newUser.password = createHash(password);
  26. newUser.email = email;
  27. newUser.name = name;
  28. newUser.info = info;
  29. // save the user
  30. newUser.save(function(err) {
  31. if (err){
  32. console.log('Error in Saving user: '+err);
  33. throw err;
  34. }
  35. console.log('User Registration succesful');
  36. return done(null, newUser);
  37. });
  38. }
  39. });
  40. }
  41. // Delay the execution of findOrCreateUser and execute the method
  42. // in the next tick of the event loop
  43. process.nextTick(findOrCreateUser);
  44. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement