Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var mongoose = require('mongoose');
  4. var bcrypt = require('bcrypt-nodejs');
  5. var Schema = mongoose.Schema;
  6.  
  7. var userSchema = new Schema({
  8.  
  9. local : {
  10. email : String,
  11. password: String
  12. },
  13. facebook : {
  14. id : String,
  15. token : String,
  16. email : String,
  17. name : String
  18. },
  19. twitter : {
  20. id : String,
  21. token : String,
  22. displayName : String,
  23. username : String
  24. },
  25. google : {
  26. id : String,
  27. token : String,
  28. email : String,
  29. name : String
  30. }
  31. });
  32.  
  33.  
  34. // methods ======================
  35. // generating a hash
  36. userSchema.methods.generateHash = function(password) {
  37. return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
  38. };
  39.  
  40. // checking if password is valid
  41. userSchema.methods.validPassword = function(password) {
  42. return bcrypt.compareSync(password, this.local.password);
  43. };
  44.  
  45. module.exports = mongoose.model('User', userSchema);
  46.  
  47. passport.use('local-login', new LocalStrategy({
  48.  
  49. usernameField : 'email',
  50. passwordField : 'password',
  51. passReqToCallback : true
  52. },
  53. function(req, email, password, done) {
  54.  
  55.  
  56. User.findOne({ 'local.email' : email }, function(err, user) {
  57. else
  58. if (err)
  59. return done(err);
  60.  
  61. // if no user is found, return the message
  62. if (!user)
  63. return done(null, false, req.flash('loginMessage', 'No user found.'));
  64.  
  65. // if the user is found but the password is wrong
  66. if (!user.validPassword(password))
  67. return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
  68.  
  69. // all is well, return successful user
  70. return done(null, user);
  71. });
  72.  
  73. }));
  74.  
  75.  
  76. ////////////////////////
  77. passport.use('local-signup', new LocalStrategy({
  78.  
  79. usernameField : 'email',
  80. passwordField : 'password',
  81. passReqToCallback : true
  82. },
  83. function(req, email, password, done) {
  84.  
  85. // asynchronous
  86. // User.findOne wont fire unless data is sent back
  87. process.nextTick(function() {
  88.  
  89.  
  90. User.findOne({ 'local.email' : email }, function(err, user){
  91. // if there are any errors, return the error
  92. if (err)
  93. return done(err);
  94.  
  95. // check to see if theres already a user with that email
  96. if (user) {
  97. return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
  98. } else {
  99.  
  100. // if there is no user with that email
  101. // create the user
  102. var newUser = new User();
  103.  
  104. // set the user's local credentials
  105. newUser.local.email = email;
  106. newUser.local.password =password;//newUser.generateHash(password);
  107.  
  108. // save the user
  109. newUser.save(function(err) {
  110. if (err)
  111. throw err;
  112. return done(null, newUser);
  113. });
  114. }
  115.  
  116. });
  117.  
  118. });
  119.  
  120. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement