Skip to content

Commit

Permalink
feat: passwordless auth proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
bamorim committed Feb 22, 2021
1 parent 42ac39a commit 0e90d79
Show file tree
Hide file tree
Showing 8 changed files with 4,750 additions and 19,014 deletions.
3 changes: 2 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ PAGARME_ENC_KEY=PAGARME_TEST_ENVIRONMENT_ECRYPTION_KEY
ROLLOUT_API_KEY=ROLLOUT_API_KEY
HEROKU_APP_NAME=reditus-local-SOME_RANDOM_NUMBER
[email protected]
MAILER_PASS=anything
MAILER_PASS=anything
MAILER_SERVICE=mailhog
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ services:
environment:
DATABASE_URL: 'postgres://postgres:postgres@postgres:5432/postgres?sslmode=disable'

email:
image: mailhog/mailhog
ports:
- "1025:1025"
- "8025:8025"

start_dependencies:
image: dadarek/wait-for-dependencies
depends_on:
Expand Down
53 changes: 32 additions & 21 deletions helpers/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { serverRuntimeConfig } = getConfig();

const email: string = process.env.MAILER_EMAIL || "";
const pass: string = process.env.MAILER_PASS || "";
const service: string = process.env.MAILER_SERVICE || "mailhog";

/**
* Validates current environment and configs.
Expand All @@ -20,13 +21,6 @@ function hasValidConfigs(): boolean {
return false;
}

if (["reditus-next-production", "reditus-next-staging"].indexOf(env) == -1) {
console.log(
`Emails are only sent in production and staging. Currently in ${process.env.HEROKU_APP_NAME}`
);
return false;
}

if (!email || !pass) {
console.log("Mailing information not set. Credentials were not provided.");
return false;
Expand Down Expand Up @@ -60,13 +54,7 @@ export async function mailError(userEmail: string, error: any) {
return;
}

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: email,
pass: pass,
},
});
const transporter = buildMailTransport();

const mailOptions = {
from: email,
Expand Down Expand Up @@ -107,13 +95,7 @@ export default async function mail(to: string, userName: string) {
{ name: userName },
(err: any, html: string) => {
if (err) throw err;
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: email,
pass: pass,
},
});
const transporter = buildMailTransport();

const mailOptions = {
from: email,
Expand All @@ -137,3 +119,32 @@ export default async function mail(to: string, userName: string) {
console.log(`Error while sending error email: ${err}`);
}
}

function buildMailTransport() {
const auth = {
user: email,
pass: pass,
};

return nodemailer.createTransport({
...getServiceOptions(service),
auth,
});
}

/**
* Get the nodemailer options based on configured service.
*
* This is used to switch between gmail for production and SMTP (to mailhog) in local development.
*
* @param {string} service The service being used currently.
* @return {object} The options for nodemailer
*/
function getServiceOptions(service: string) {
if (service !== "mailhog") return { service };

return {
host: "localhost",
port: 1025,
};
}
Loading

0 comments on commit 0e90d79

Please sign in to comment.