From 98298d07d21014b36ca552877e82270fa508a423 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Tue, 6 Apr 2021 15:29:07 -0700 Subject: [PATCH] Adding CORS to fastify app and sending email on load. --- package-lock.json | 51 ++++++++++++++++++++++++++++++++++++++++++++--- package.json | 4 +++- src/index.js | 13 ++++++++++++ src/mail/index.js | 37 ++++++++++++++++++++++++++++++++++ ui/index.html | 2 ++ 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 src/mail/index.js diff --git a/package-lock.json b/package-lock.json index 8145a28..0270734 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,9 +12,11 @@ "dotenv": "^8.2.0", "fastify": "^3.12.0", "fastify-cookie": "^5.1.0", + "fastify-cors": "^5.2.0", "fastify-static": "^4.0.1", "jsonwebtoken": "^8.5.1", - "mongodb": "^3.6.4" + "mongodb": "^3.6.4", + "nodemailer": "^6.5.0" } }, "node_modules/@fastify/forwarded": { @@ -342,6 +344,15 @@ "fastify-plugin": "^3.0.0" } }, + "node_modules/fastify-cors": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/fastify-cors/-/fastify-cors-5.2.0.tgz", + "integrity": "sha512-Lde71qT23M3Ip3pmny3uN6q6lQ4J5J0/YWoqe2stL4sMT99R5LEtTJ2La2zijFOR5OFhxvxqGgR7BIc2x3amPg==", + "dependencies": { + "fastify-plugin": "^3.0.0", + "vary": "^1.1.2" + } + }, "node_modules/fastify-error": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/fastify-error/-/fastify-error-0.3.0.tgz", @@ -617,8 +628,7 @@ "bson": "^1.1.4", "denque": "^1.4.1", "require_optional": "^1.0.1", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "safe-buffer": "^5.1.2" }, "engines": { "node": ">=4" @@ -652,6 +662,14 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "node_modules/nodemailer": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.5.0.tgz", + "integrity": "sha512-Tm4RPrrIZbnqDKAvX+/4M+zovEReiKlEXWDzG4iwtpL9X34MJY+D5LnQPH/+eghe8DLlAVshHAJZAZWBGhkguw==", + "engines": { + "node": ">=6.0.0" + } + }, "node_modules/on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -992,6 +1010,14 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, + "node_modules/vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", @@ -1274,6 +1300,15 @@ "fastify-plugin": "^3.0.0" } }, + "fastify-cors": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/fastify-cors/-/fastify-cors-5.2.0.tgz", + "integrity": "sha512-Lde71qT23M3Ip3pmny3uN6q6lQ4J5J0/YWoqe2stL4sMT99R5LEtTJ2La2zijFOR5OFhxvxqGgR7BIc2x3amPg==", + "requires": { + "fastify-plugin": "^3.0.0", + "vary": "^1.1.2" + } + }, "fastify-error": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/fastify-error/-/fastify-error-0.3.0.tgz", @@ -1523,6 +1558,11 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "nodemailer": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.5.0.tgz", + "integrity": "sha512-Tm4RPrrIZbnqDKAvX+/4M+zovEReiKlEXWDzG4iwtpL9X34MJY+D5LnQPH/+eghe8DLlAVshHAJZAZWBGhkguw==" + }, "on-finished": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", @@ -1791,6 +1831,11 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" }, + "vary": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", + "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index 0a6cbfe..1c98675 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,10 @@ "dotenv": "^8.2.0", "fastify": "^3.12.0", "fastify-cookie": "^5.1.0", + "fastify-cors": "^5.2.0", "fastify-static": "^4.0.1", "jsonwebtoken": "^8.5.1", - "mongodb": "^3.6.4" + "mongodb": "^3.6.4", + "nodemailer": "^6.5.0" } } diff --git a/src/index.js b/src/index.js index 3b666eb..f799c51 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import './env.js' import { fastify } from 'fastify' import fastifyStatic from 'fastify-static' import fastifyCookie from 'fastify-cookie'; +import fastifyCors from 'fastify-cors'; import path from 'path' import { fileURLToPath } from 'url' import { connectDb } from './db.js' @@ -10,6 +11,7 @@ import { authorizeUser } from './accounts/authorize.js' import { logUserIn } from './accounts/logUserIn.js' import { logUserOut } from './accounts/logUserOut.js' import { getUserFromCookies } from './accounts/user.js' +import { sendEmail, mailInit } from './mail/index.js' // ESM specific "features" const __filename = fileURLToPath(import.meta.url) @@ -19,6 +21,17 @@ const app = fastify() async function startApp() { try { + await mailInit() + await sendEmail({ + subject: "New func", + html: /*html*/ `

New HTML who is?

`, + }) + + app.register(fastifyCors, { + origin: [/\.nodeauth.dev/, 'https://nodeauth.dev'], + credentials: true, + }); + app.register(fastifyCookie, { secret: process.env.COOKIE_SIGNATURE, }) diff --git a/src/mail/index.js b/src/mail/index.js new file mode 100644 index 0000000..2df6d8c --- /dev/null +++ b/src/mail/index.js @@ -0,0 +1,37 @@ +import nodemailer from 'nodemailer'; + +let mail + +export async function mailInit() { + let testAccount = await nodemailer.createTestAccount(); + + mail = nodemailer.createTransport({ + host: "smtp.ethereal.email", + port: 587, + secure: false, + auth: { + user: testAccount.user, + pass: testAccount.pass, + } + }) +} + +export async function sendEmail({ + from = "brad@example.com", + to = "brad@example.com", + subject, + html, +}) { + try { + const info = await mail.sendMail({ + from, + to, + subject, + html, + }) + console.log('info', info); + + } catch (e) { + console.error(e); + } +} \ No newline at end of file diff --git a/ui/index.html b/ui/index.html index a99d03c..9685e4b 100644 --- a/ui/index.html +++ b/ui/index.html @@ -56,6 +56,7 @@ const res = await fetch('https://api.nodeauth.dev/api/register', { method: 'POST', body: JSON.stringify(values), + credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' }, }); console.log('values', values); @@ -81,6 +82,7 @@ const res = await fetch('https://api.nodeauth.dev/api/authorize', { method: 'POST', body: JSON.stringify(values), + credentials: 'include', headers: { 'Content-type': 'application/json; charset=UTF-8' }, }); console.log('values', values);