Adding CORS to fastify app and sending email on load.

This commit is contained in:
Bradley Shellnut 2021-04-06 15:29:07 -07:00
parent 91091e457d
commit 98298d07d2
5 changed files with 103 additions and 4 deletions

51
package-lock.json generated
View file

@ -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",

View file

@ -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"
}
}

View file

@ -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*/ `<h2>New HTML who is?</h2>`,
})
app.register(fastifyCors, {
origin: [/\.nodeauth.dev/, 'https://nodeauth.dev'],
credentials: true,
});
app.register(fastifyCookie, {
secret: process.env.COOKIE_SIGNATURE,
})

37
src/mail/index.js Normal file
View file

@ -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);
}
}

View file

@ -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);