|
| 1 | +/** |
| 2 | + * Created by sreekanth on 1/3/15. |
| 3 | + */ |
| 4 | +// load all the things we need |
| 5 | +var LocalStrategy = require('passport-local').Strategy; |
| 6 | +// load up the user model |
| 7 | +var User = require('../models/user'); |
| 8 | + |
| 9 | +// load the auth variables |
| 10 | +var configAuth = require('./auth'); // use this one for testing |
| 11 | + |
| 12 | +module.exports = function(passport) { |
| 13 | + |
| 14 | + // ========================================================================= |
| 15 | + // LOCAL LOGIN ============================================================= |
| 16 | + // ========================================================================= |
| 17 | + passport.use('local-login', new LocalStrategy({ |
| 18 | + // by default, local strategy uses username and password, we will override with email |
| 19 | + usernameField : 'email', |
| 20 | + passwordField : 'password', |
| 21 | + passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) |
| 22 | + }, |
| 23 | + function(req, email, password, done) { |
| 24 | + if (email) |
| 25 | + email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching |
| 26 | + |
| 27 | + // asynchronous |
| 28 | + process.nextTick(function() { |
| 29 | + User.findOne({ 'local.email' : email }, function(err, user) { |
| 30 | + // if there are any errors, return the error |
| 31 | + if (err) |
| 32 | + return done(err); |
| 33 | + |
| 34 | + // if no user is found, return the message |
| 35 | + if (!user) |
| 36 | + return done(null, false, req.flash('loginMessage', 'No user found.')); |
| 37 | + |
| 38 | + if (!user.validPassword(password)) |
| 39 | + return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); |
| 40 | + |
| 41 | + // all is well, return user |
| 42 | + else |
| 43 | + return done(null, user); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + })); |
| 48 | + |
| 49 | + // ========================================================================= |
| 50 | + // LOCAL SIGNUP ============================================================ |
| 51 | + // ========================================================================= |
| 52 | + passport.use('local-signup', new LocalStrategy({ |
| 53 | + // by default, local strategy uses username and password, we will override with email |
| 54 | + usernameField : 'email', |
| 55 | + passwordField : 'password', |
| 56 | + passReqToCallback : true // allows us to pass in the req from our route (lets us check if a user is logged in or not) |
| 57 | + }, |
| 58 | + function(req, email, password, done) { |
| 59 | + if (email) |
| 60 | + email = email.toLowerCase(); // Use lower-case e-mails to avoid case-sensitive e-mail matching |
| 61 | + |
| 62 | + // asynchronous |
| 63 | + process.nextTick(function() { |
| 64 | + // if the user is not already logged in: |
| 65 | + if (!req.user) { |
| 66 | + User.findOne({ 'local.email' : email }, function(err, user) { |
| 67 | + // if there are any errors, return the error |
| 68 | + if (err) |
| 69 | + return done(err); |
| 70 | + |
| 71 | + // check to see if theres already a user with that email |
| 72 | + if (user) { |
| 73 | + return done(null, false, req.flash('signupMessage', 'That email is already taken.')); |
| 74 | + } else { |
| 75 | + |
| 76 | + // create the user |
| 77 | + var newUser = new User(); |
| 78 | + |
| 79 | + newUser.local.email = email; |
| 80 | + newUser.local.password = newUser.generateHash(password); |
| 81 | + |
| 82 | + newUser.save(function(err) { |
| 83 | + if (err) |
| 84 | + throw err; |
| 85 | + |
| 86 | + return done(null, newUser); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + }); |
| 91 | + // if the user is logged in but has no local account... |
| 92 | + } else if ( !req.user.local.email ) { |
| 93 | + // ...presumably they're trying to connect a local account |
| 94 | + var user = req.user; |
| 95 | + user.local.email = email; |
| 96 | + user.local.password = user.generateHash(password); |
| 97 | + user.save(function(err) { |
| 98 | + if (err) |
| 99 | + throw err; |
| 100 | + return done(null, user); |
| 101 | + }); |
| 102 | + } else { |
| 103 | + // user is logged in and already has a local account. Ignore signup. (You should log out before trying to create a new account, user!) |
| 104 | + return done(null, req.user); |
| 105 | + } |
| 106 | + |
| 107 | + }); |
| 108 | + |
| 109 | + })); |
| 110 | +}; |
| 111 | + |
0 commit comments