Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =========================================================================
- // LOCAL SIGNUP ============================================================
- // =========================================================================
- // we are using named strategies since we have one for login and one for signup
- // by default, if there was no name, it would just be called 'local'
- passport.use('local-signup', new LocalStrategy({
- // by default, local strategy uses username and password, we will override with email
- firstNameField: 'firstName',
- lastNameField: 'lastName',
- usernameField: 'email',
- passwordField: 'password',
- jobTitleField: 'jobTitle',
- startDateField: 'startDate',
- passReqToCallback: true // allows us to pass back the entire request to the callback
- },
- function(req, email, password, done) {
- // 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({'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, {
- message: 'That email is already taken.'
- });
- }
- else { var token = crypto.randomBytes().toString();
- // if there is no user with that email
- // create the user
- var newUser = new User();
- // set the user's local credentials
- newUser.firstName = req.body.firstName;
- newUser.lastName = req.body.lastName;
- newUser.email = email;
- newUser.password = newUser.generateHash(password); // use the generateHash function in our user model
- newUser.jobTitle = req.body.jobTitle;
- newUser.startDate = req.body.startDate;
- newUser.birthday = req.body.birthday;
- newUser.region = req.body.region;
- newUser.sector = req.body.sector;
- newUser.accountConfirmationToken = token;
- newUser.accountConfirmationTokenExpires = Date.now() + 3600000;
- newUser.accountVerified = 'false';
- newUser.isLineManager = 'false';
- // save the user
- newUser.save(function(err) {
- if (err)
- throw err;
- else {
- var data = {
- from: 'system',
- to: email,
- subject: 'Account Verification',
- text: 'You recently registered onto the App, to gain access to your account please verify your account.nn' +
- 'Please click on the following link, or paste this into your browser to complete the process:nn' +
- 'http://' + req.headers.host + '/verify/' + token + 'nn'
- };
- mailgun.messages().send(data, function(error, body) {
- console.log(body);
- console.log("setting token 1");
- req.flash('info', 'An e-mail has been sent to ' + email + ' with further instructions.');
- });
- return done(null, newUser);
- }
- });
- }
- });
- }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement