Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var nodemailer = require('nodemailer');
- var passport = require('passport');
- var LocalStrategy = require('passport-local').Strategy;
- var User = require('../models/user');
- exports.register = function(req, res, next) {
- res.render('register', { title: 'Rafflefly | Register' });
- }
- exports.post_register = function(req, res, next) {
- var first_name = req.body.first_name;
- var last_name = req.body.last_name;
- var email = req.body.email;
- var password = req.body.password;
- // validations
- req.checkBody('first name', 'The first name is required').notEmpty();
- req.checkBody('last name', 'The last name is required').notEmpty();
- req.checkBody('email', 'An account email is required').notEmpty();
- req.checkBody('email', 'This account email is not valid').isEmail();
- req.checkBody('password', 'An account password is required').notEmpty();
- if (error) {
- req.flash('error', 'The form could not send successfully');
- } else {
- var newUser = new User({
- first_name: first_name,
- last_name: last_name,
- email: email,
- password: password,
- });
- nodemailer.createTestAccount((err, account) => {
- // create reusable transporter object using the default SMTP transport
- if (process.env.NODE_ENV === 'production') {
- transporter = nodemailer.createTransport({
- host: "smtp.sendgrid.net",
- port: 587,
- auth: {
- user: process.env.SENDGRID_USERNAME,
- pass: process.env.SENDGRID_PASSWORD,
- }
- });
- } else {
- transporter = nodemailer.createTransport({
- host: "smtp.ethereal.email",
- port: 587,
- auth: {
- user: 'qkkvnabtziufbksa@ethereal.email',
- pass: 'A4W9HF2WbhAav263VM',
- }
- });
- }
- // setup email data with unicode symbols
- let mailOptions = {
- from: process.env.GLOBAL_EMAIL || 'ben@benbagley.co.uk', // sender address
- to: `${email}`, // list of receivers
- subject: 'Welcome to Rafflefly', // Subject line
- html: `Welcome to Rafflefly` // html body
- };
- // send mail with defined transport object
- transporter.sendMail(mailOptions, (error, info) => {
- if (error) {
- return console.log(error);
- }
- req.flash('success', 'You are now registered, you can now login!');
- res.redirect('/');
- });
- });
- User.createUser(newUser, function(err, user) {
- if(err) throw err;
- console.log(user);
- });
- }
- };
- passport.use(new LocalStrategy({
- usernameField: 'email'
- },
- function(email, password, done) {
- User.getUserByEmail(email, function(err, user){
- if(err) throw err;
- if(!user){
- return done(null, false, {message: 'Unknown Email Address'});
- }
- User.comparePassword(password, user.password, function(err, isMatch){
- if(err) throw err;
- if(isMatch){
- return done(null, user);
- } else {
- return done(null, false, {message: 'Invalid password'});
- }
- });
- });
- }));
- passport.serializeUser(function(user, done) {
- done(null, user.id);
- });
- passport.deserializeUser(function(id, done) {
- User.getUserById(id, function(err, user) {
- done(err, user);
- });
- });
- exports.login = function(req, res, next) {
- res.render('login', { title: 'Rafflefly | Login' });
- }
- exports.post_login = function(req, res, next) {
- res.post('/login', passport.authenticate('local', {
- successRedirect: '/dashboard',
- failureRedirect: '/login',
- failureFlash: true })
- );
- }
- doctype html
- html
- head
- title= title
- meta(name='viewport' content='width=device-width, initial-scale=1')
- meta(charset='utf-8')
- link(rel='stylesheet' href='/stylesheets/output.css')
- link(rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css')
- body(class='antialiased min-h-screen')
- include navigation
- include error
- block content
- include footer
- script(src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js')
- script(src='https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js')
- script(src='/javascripts/fontawesome-all.min.js' defer)
- script(src='/javascripts/site.js')
Add Comment
Please, Sign In to add comment