Fixed email sending

This commit is contained in:
Jonathan Barrow
2022-10-01 00:55:39 -04:00
parent 29edbab43c
commit ee98bb006f
3 changed files with 23 additions and 65 deletions

View File

@@ -3,31 +3,4 @@ const config = require('../config.json');
const transporter = nodemailer.createTransport(config.email);
/**
* Sends an email with the specified subject and message to an email address
* @param {String} email the destination email address
* @param {String} subject The Subject of the email
* @param {String} message The body of the email
*/
async function send(email, subject = 'No email subject provided', message = 'No email body provided') {
const options = {
from: config.email.auth.user,
to: email,
subject: subject,
html: message
};
return new Promise(resolve => {
transporter.sendMail(options, (error) => {
if (error) {
console.warn(error);
}
return resolve();
});
});
}
module.exports = {
send: send
};
module.exports = transporter;

View File

@@ -8,7 +8,7 @@ const { NEXAccount } = require('../../../models/nex-account');
const deviceCertificateMiddleware = require('../../../middleware/device-certificate');
const ratelimit = require('../../../middleware/ratelimit');
const database = require('../../../database');
const mailer = require('../../../mailer');
const util = require('../../../util');
const logger = require('../../../../logger');
require('moment-timezone');
@@ -173,19 +173,7 @@ router.post('/', ratelimit, deviceCertificateMiddleware, async (request, respons
await session.endSession();
}
await mailer.send(
pnid.get('email'),
'[Pretendo Network] Please confirm your e-mail address',
`Hello,
Your Pretendo Network ID activation is almost complete. Please click the link below to confirm your e-mail address and complete the activation process.
https://account.pretendo.cc/account/email-confirmation?token=` + pnid.get('identification.email_token') + `
If you are unable to connect to the above URL, please enter the following confirmation code on the device to which your Prentendo Network ID is linked.
<<Confirmation code: ` + pnid.get('identification.email_code') + '>>'
);
await util.sendConfirmationEmail(pnid);
response.send(xmlbuilder.create({
person: {
@@ -203,8 +191,7 @@ router.get('/@me/profile', async (request, response) => {
response.set('Content-Type', 'text/xml');
response.set('Server', 'Nintendo 3DS (http)');
response.set('X-Nintendo-Date', new Date().getTime());
const { pnid } = request;
const person = await database.getUserProfileJSONByPID(pnid.get('pid'));
@@ -246,7 +233,7 @@ router.get('/@me/devices', async (request, response) => {
response.set('Content-Type', 'text/xml');
response.set('Server', 'Nintendo 3DS (http)');
response.set('X-Nintendo-Date', new Date().getTime());
const { pnid, headers } = request;
response.send(xmlbuilder.create({
@@ -487,30 +474,18 @@ router.put('/@me/emails/@primary', async (request, response) => {
}).end());
}
pnid.email.address = email.get('address');
pnid.email.reachable = false;
pnid.email.validated = false;
pnid.email.id = crypto.randomBytes(4).readUInt32LE();
pnid.set('email.address', email.get('address'));
pnid.set('email.reachable', false);
pnid.set('email.validated', false);
pnid.set('email.validated_date', '');
pnid.set('email.id', crypto.randomBytes(4).readUInt32LE());
// TODO: Change these, they are slow
await pnid.generateEmailValidationCode();
await pnid.generateEmailValidationToken();
await pnid.save();
await mailer.send(
email.get('address'),
'[Pretendo Network] Please confirm your e-mail address',
`Hello,
Your Pretendo Network ID activation is almost complete. Please click the link below to confirm your e-mail address and complete the activation process.
https://account.pretendo.cc/account/email-confirmation?token=` + pnid.get('identification.email_token') + `
If you are unable to connect to the above URL, please enter the following confirmation code on the device to which your Prentendo Network ID is linked.
<<Confirmation code: ` + pnid.get('identification.email_code') + '>>'
);
await util.sendConfirmationEmail(pnid);
response.send('');
});

View File

@@ -1,6 +1,7 @@
const crypto = require('crypto');
const NodeRSA = require('node-rsa');
const aws = require('aws-sdk');
const mailer = require('./mailer');
const cache = require('./cache');
const config = require('../config.json');
@@ -41,7 +42,7 @@ async function generateToken(cryptoOptions, tokenOptions) {
// They take no extra crypto options
if (!cryptoOptions) {
const aesKey = await cache.getServiceAESKey('account', 'hex');
const dataBuffer = Buffer.alloc(1 + 1 + 4 + 8);
dataBuffer.writeUInt8(tokenOptions.system_type, 0x0);
@@ -228,6 +229,14 @@ function nascError(errorCode) {
return params;
}
async function sendConfirmationEmail(pnid) {
await mailer.sendMail({
to: pnid.get('email.address'),
subject: '[Pretendo Network] Please confirm your e-mail address',
html: `Hello,\n\nYour Pretendo Network ID activation is almost complete. Please click the link below to confirm your e-mail address and complete the activation process.\n\nhttps://account.pretendo.cc/account/email-confirmation?token=${pnid.get('identification.email_token')}\n\nIf you are unable to connect to the above URL, please enter the following confirmation code on the device to which your Prentendo Network ID is linked.\n\n<<Confirmation code: ${pnid.get('identification.email_code')}>>`
});
}
module.exports = {
nintendoPasswordHash,
nintendoBase64Decode,
@@ -237,5 +246,6 @@ module.exports = {
unpackToken,
fullUrl,
uploadCDNAsset,
nascError
nascError,
sendConfirmationEmail
};