Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // load all the things we need
- var LocalStrategy = require('passport-local').Strategy;
- // load up the user model
- var User = require('../app/models/user');
- // expose this function to our app using module.exports
- module.exports = function(passport) {
- passport.serializeUser(function(user, done) {
- done(null, user.id);
- });
- passport.deserializeUser(function(id, done) {
- User.findById(id, function(err, user) {
- done(err, user);
- });
- });
- passport.use('local-signup', new LocalStrategy({
- // by default, local strategy uses username and password, we will override with email
- first_nameField : 'first_name',
- last_nameField : 'last_name',
- usernameField : 'email',
- passwordField : 'password',
- passReqToCallback : true // allows us to pass back the entire request to the callback
- },
- function(req, first_name, last_name, email, password, done) {
- // asynchronous
- // User.findOne wont fire unless data is sent back
- process.nextTick(function() {
- // find a user whose email is the same as the forms email
- // we are checking to see if the user trying to login already exists
- User.findOne({ 'local.email' : email }, function(err, user) {
- // if there are any errors, return the error
- if (err) {
- return done(err);
- }
- // check to see if theres already a user with that email
- if (user) {
- return done(null, false, req.flash('message', 'That email is already taken.'));
- } else {
- // if there is no user with that email
- // create the user
- var newUser = new User();
- // set the user's local credentials
- newUser.local.first_name = first_name;
- newUser.local.last_name = last_name;
- newUser.local.email = email;
- newUser.local.password = newUser.generateHash(password);
- // save the user
- newUser.save(function(err) {
- if (err) {
- throw err;
- }
- return done(null, newUser);
- });
- }
- });
- });
- }));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement