Skip to content

Register, Sign-in and email working #40

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

var app = require('../app');
var debug = require('debug')('rest-api-nodejs-mongodb:server');
var debug = require('debug')('cantoralapp_server:server');
var http = require('http');

/**
Expand Down
49 changes: 49 additions & 0 deletions controllers/MailController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { body,validationResult } = require("express-validator");
const { sanitizeBody } = require("express-validator");
//helper file to prepare responses.
const apiResponse = require("../helpers/apiResponse");
const mailer = require("../helpers/mailer");
const { constants } = require("../helpers/constants");

exports.sendMail = [
// Validate fields.
body("firstName").isLength({ min: 1 }).trim().withMessage("First name must be specified.")
.isAlphanumeric().withMessage("First name has non-alphanumeric characters."),
body("lastName").isLength({ min: 1 }).trim().withMessage("Last name must be specified.")
.isAlphanumeric().withMessage("Last name has non-alphanumeric characters."),
body("email").isLength({ min: 1 }).trim().withMessage("Email must be specified.")
.isEmail().withMessage("Email must be a valid email address."),
// Sanitize fields.
sanitizeBody("firstName").escape(),
sanitizeBody("lastName").escape(),
sanitizeBody("email").escape(),
// Process request after validation and sanitization.
(req, res) => {
try {
// Extract the validation errors from a request.
const errors = validationResult(req);
if (!errors.isEmpty()) {
// Display sanitized values/errors messages.
return apiResponse.validationErrorWithData(res, "Validation Error.", errors.array());
}else {
let html = "<p>New email.</p><p>name: "+ req.body.firstName + req.body.lastName +"</p>";
mailer.send(
constants.confirmEmails.from,
req.body.email,
"Confirm Account",
html
).then((foo) => {
console.log(foo);
return apiResponse.successResponseWithData(res,"Email sent", foo);

}).catch(err => {
console.log(err);
return apiResponse.ErrorResponse(res,err);
});
}

}catch (err) {
//throw error in json response with status 500.
return apiResponse.ErrorResponse(res, err);
}
}];
2 changes: 1 addition & 1 deletion helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports.constants = {
email: "[email protected]"
},
confirmEmails: {
from : "[email protected]"
from : "[email protected]"
}
};
23 changes: 16 additions & 7 deletions helpers/mailer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
const nodemailer = require("nodemailer");

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
var mailConfig;
mailConfig = {
host: process.env.EMAIL_SMTP_HOST,
port: process.env.EMAIL_SMTP_PORT,
//secure: process.env.EMAIL_SMTP_SECURE, // lack of ssl commented this. You can uncomment it.
secure: process.env.EMAIL_SMTP_SECURE, // lack of ssl commented this. You can uncomment it.
auth: {
user: process.env.EMAIL_SMTP_USERNAME,
pass: process.env.EMAIL_SMTP_PASSWORD
}
});
};
let transporter = nodemailer.createTransport(mailConfig);

exports.send = function (from, to, subject, html)
exports.send = async function (from, to, subject, html)
{
// send mail with defined transport object
// visit https://nodemailer.com/ for more options
return transporter.sendMail({
from: from, // sender address e.g. [email protected] or "Fred Foo 👻" <[email protected]>

var mailOptions = {
from: from, // sender address e.g. <[email protected]>
to: to, // list of receivers e.g. [email protected], [email protected]
subject: subject, // Subject line e.g. 'Hello ✔'
//text: text, // plain text body e.g. Hello world?
html: html // html body e.g. '<b>Hello world?</b>'
});
};

let email = await transporter.sendMail(mailOptions);

console.log("email id sent: %s", email.messageId);
console.log("email sent: %s", email);
console.log("email response sent: %s", email.response);
};
3 changes: 2 additions & 1 deletion middlewares/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const jwt = require("express-jwt");
const secret = process.env.JWT_SECRET;

const authenticate = jwt({
secret: secret
secret: secret,
algorithms: ["HS256"]
});

module.exports = authenticate;
10 changes: 10 additions & 0 deletions models/AutorModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var mongoose = require("mongoose");

var Schema = mongoose.Schema;

var AutorSchema = new Schema({
Nombre: {type: String, required: true},
Apellido: {type: String, required: true},
}, {timestamps: true});

module.exports = mongoose.model("Autor", AutorSchema);
10 changes: 10 additions & 0 deletions models/SongModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var mongoose = require("mongoose");

var Schema = mongoose.Schema;

var SongSchema = new Schema({
Nombre: {type: String, required: true},
Apellido: {type: String, required: true},
}, {timestamps: true});

module.exports = mongoose.model("Song", SongSchema);
Loading