Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. passport.use(new LocalStrategy({
  2. usernameField: 'email',
  3. passwordField: 'password'
  4. },
  5. function(username, password, cb) {
  6. UserSchema.findByUsername(username, function(err, user) {
  7. if (err) { return cb(err); }
  8. if (!user) { return cb(null, false); }
  9. if (user.password != password) { return cb(null, false); }
  10. return cb(null, user);
  11. });
  12. }));
  13.  
  14. router.post('/login',
  15. passport.authenticate('local', {
  16. failureRedirect: '/login' }),
  17. function(req, res) {
  18. res.redirect('/');
  19. });
  20.  
  21. const UserSchema = mongoose.Schema ({
  22. firstName: {
  23. type: String,
  24. index: true
  25. },
  26. lastName: {
  27. type: String,
  28. index: true
  29. },
  30. email: {
  31. type: String,
  32. index: true
  33. },
  34. password: {
  35. type: String,
  36. index: true
  37. },
  38. homeAirport: {
  39. type: String,
  40. index: true
  41. }
  42. })
  43.  
  44. UserSchema.plugin(passportLocalMongoose)
  45.  
  46. // Adding this function to find by username
  47. exports.findByUsername = function(username, cb) {
  48. process.nextTick(function() {
  49. for (var i = 0, len = records.length; i < len; i++) {
  50. var record = records[i];
  51. if (record.username === username) {
  52. return cb(null, record);
  53. }
  54. }
  55. return cb(null, null);
  56. });
  57. }
  58.  
  59. // Export it to the app
  60. module.exports = mongoose.model('user', UserSchema)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement