Advertisement
Guest User

Untitled

a guest
Feb 29th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. // config/passport.js
  2.  
  3. // load all the things we need
  4. var LocalStrategy = require('passport-local').Strategy;
  5.  
  6. // load up the user model
  7. var User = require('../models/user');
  8.  
  9. // expose this function to our app using module.exports
  10. module.exports = function(passport) {
  11.  
  12. // =========================================================================
  13. // passport session setup ==================================================
  14. // =========================================================================
  15. // required for persistent login sessions
  16. // passport needs ability to serialize and unserialize users out of session
  17.  
  18. // used to serialize the user for the session
  19. passport.serializeUser(function(user, done) {
  20. done(null, user.id);
  21. });
  22.  
  23. // used to deserialize the user
  24. passport.deserializeUser(function(id, done) {
  25. User.findById(id, function(err, user) {
  26. done(err, user);
  27. });
  28. });
  29.  
  30.  
  31. // LOCAL LOGIN =============================================================
  32. // =========================================================================
  33. // we are using named strategies since we have one for login and one for signup
  34. // by default, if there was no name, it would just be called 'local'
  35.  
  36. passport.use('local-login', new LocalStrategy({
  37. // by default, local strategy uses username and password, we will override with email
  38. usernameField : 'email',
  39. passwordField : 'password',
  40. passReqToCallback : true // allows us to pass back the entire request to the callback
  41. },
  42. function(req, email, password, done) { // callback with email and password from our form
  43.  
  44. // find a user whose email is the same as the forms email
  45. // we are checking to see if the user trying to login already exists
  46. User.findOne({ 'local.email' : email }, function(err, user) {
  47. // if there are any errors, return the error before anything else
  48. if (err)
  49. return done(err);
  50.  
  51. // if no user is found, return the message
  52. if (!user)
  53. return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
  54.  
  55. // if the user is found but the password is wrong
  56. if (!user.validPassword(password))
  57. return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
  58.  
  59. // all is well, return successful user
  60. return done(null, user);
  61. });
  62.  
  63. }));
  64.  
  65.  
  66. // =========================================================================
  67. // LOCAL SIGNUP ============================================================
  68. // =========================================================================
  69. // we are using named strategies since we have one for login and one for signup
  70. // by default, if there was no name, it would just be called 'local'
  71.  
  72. passport.use('local-signup', new LocalStrategy({
  73. // by default, local strategy uses username and password, we will override with email
  74. usernameField : 'email',
  75. passwordField : 'password',
  76. passReqToCallback : true // allows us to pass back the entire request to the callback
  77. },
  78. function(req, email, password, done) {
  79.  
  80. // asynchronous
  81. // User.findOne wont fire unless data is sent back
  82. process.nextTick(function() {
  83.  
  84. // find a user whose email is the same as the forms email
  85. // we are checking to see if the user trying to login already exists
  86. User.findOne({ 'local.email' : email }, function(err, user) {
  87. // if there are any errors, return the error
  88. if (err)
  89. return done(err);
  90.  
  91. // check to see if theres already a user with that email
  92. if (user) {
  93. return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
  94. } else {
  95.  
  96. // if there is no user with that email
  97. // create the user
  98. var newUser = new User();
  99.  
  100. // set the user's local credentials
  101. newUser.local.email = email;
  102. newUser.local.password = newUser.generateHash(password);
  103. newUser.local.username = req.body.username;
  104. newUser.local.role = 'guest';
  105.  
  106. // save the user
  107. newUser.save(function(err) {
  108. if (err)
  109. throw err;
  110. return done(null, newUser);
  111. });
  112. }
  113.  
  114. });
  115.  
  116. });
  117.  
  118. }));
  119.  
  120. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement