Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(billing-ui): Improve credit card entry form #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions server/controllers/users-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ exports.postBilling = function(req, res, next){
});
});
};
exports.confirmEmail = function(req, res, next) {
var token = req.query.token;

if (!token) {
req.flash('errors', {
msg: 'Please check the URL, you didn\' provide any token to confirm your email adress'
});
return res.redirect(req.redirect.failure);

}

User.findOne({
emailConfirmToken: req.query.token
}, function(err, user) {

if (err || !user) {
req.flash('errors', {
msg: 'lease check the URL, you didn\' provide a valid token to confirm your email address'
});
return res.redirect(req.redirect.failure);
}

if (!user.emailConfirmed) {
user.emailConfirmed = true;
user.save(function(err) {
if (err) return next(err);
console.log(user.profile.website);

req.flash('success', {
msg: 'Thank you, you sucessfully confirmed your email address.'
});
res.redirect(req.redirect.success);
});

} else {
req.flash('errors', {
msg: 'Sorry, but the email address was already confirmed.'
});
return res.redirect(req.redirect.failure);
}

})
};

exports.postPlan = function(req, res, next){
var plan = req.body.plan;
Expand Down
38 changes: 38 additions & 0 deletions server/middleware/email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var nodemailer = require('nodemailer');
var mailgunApiTransport = require('nodemailer-mailgunapi-transport');
var User = require('../models/user');
var secrets = require('../config/secrets');


var transporter = nodemailer.createTransport(
mailgunApiTransport({
apiKey: secrets.mailgun.password,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here it should be apiKey and domain

domain: secrets.mailgun.user
}));

var config = {
from: 'postmaster@sandboxdaaf0cb261364f07a6390025ab706114.mailgun.org',
siteName: 'node-stripe-membership'
}

exports.sendSignup = function (req, user, cb) {


if(!user) { cb(false); return ; }

var mailOptions = {
to: user.email,
from: config.from,
subject: 'Confirm signup on' + config.siteName,
text: 'You are receiving this email because you (or someone else) signed up at ' + config.siteName + '.\n\n' +
'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
'http://' + req.headers.host + '/user/verify_email?token=' + user.emailConfirmToken + '\n\n' +
'If you did not request this, please ignore this email and your account won\'t be actived.\n'
};
transporter.sendMail(mailOptions, function(err) {
console.log(err);
cb(err ? false : true);

})

}
15 changes: 12 additions & 3 deletions server/middleware/passport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
var crypto = require('crypto');
var Email = require('./email');

module.exports = function(passport){

Expand Down Expand Up @@ -58,16 +60,23 @@ module.exports = function(passport){
// edit this portion to accept other properties when creating a user.
var user = new User({
email: req.body.email,
password: req.body.password // user schema pre save task hashes this password
password: req.body.password, // user schema pre save task hashes this password
emailConfirmToken: crypto.randomBytes(15).toString('hex')
});

user.save(function(err) {
if (err) return done(err, false, req.flash('error', 'Error saving user.'));
if (err) return done(err, false, req.flash('error', 'Sorry, but an error occured while saving. Try again later'));
var time = 14 * 24 * 3600000;
req.session.cookie.maxAge = time; //2 weeks
req.session.cookie.expires = new Date(Date.now() + time);
req.session.touch();
return done(null, user, req.flash('success', 'Thanks for signing up!!'));
Email.sendSignup(req, user, function (result) {
if(result) {
return done(null, user, req.flash('success', 'Thank you, your account was created. Check your emails to confirm the signup.'));
} else {
return done(null, false, req.flash('error', 'Sorry, but an error occured while creating the account. Try again later'));
}
})
});
});
};
Expand Down
2 changes: 2 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var userSchema = new mongoose.Schema({
picture: { type: String, default: '' }
},

emailConfirmToken: String,
emailConfirmed: {type: Boolean, default: false},
resetPasswordToken: String,
resetPasswordExpires: Date
});
Expand Down
7 changes: 6 additions & 1 deletion server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,14 @@ module.exports = function (app, passport) {
isAuthenticated,
users.deleteAccount);

app.get('/user/verify_email',
setRedirect({success: '/', failure: '/'}),
users.confirmEmail);


// use this url to receive stripe webhook events
app.post('/stripe/events',
stripeWebhook.middleware,
stripeEvents
);
};
};