Skip to content
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

feat: passwordless auth #63

Open
wants to merge 2 commits 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
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
50 changes: 25 additions & 25 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
module.exports = {
'env': {
'browser': true,
'es2020': true,
'node': true,
env: {
browser: true,
es2020: true,
node: true,
"jest/globals": true,
},
'extends': [
'eslint:recommended',
'plugin:react/recommended',
'google',
'prettier',
'plugin:jest/recommended',
'plugin:jest/style'
extends: [
"eslint:recommended",
"plugin:react/recommended",
"google",
"prettier",
"plugin:jest/recommended",
"plugin:jest/style",
],
'parser': '@typescript-eslint/parser',
'parserOptions': {
'ecmaFeatures': {
'jsx': true,
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaFeatures: {
jsx: true,
},
'ecmaVersion': 11,
'sourceType': 'module',
ecmaVersion: 11,
sourceType: "module",
},
'plugins': [
'jest',
'react',
'@typescript-eslint',
'prettier',
],
'rules': {
plugins: ["jest", "react", "@typescript-eslint", "prettier"],
rules: {
"react/react-in-jsx-scope": "off",
"require-jsdoc": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"prettier/prettier": "error",
"jest/no-export": "off"
"jest/no-export": "off",
},
settings: {
react: {
version: "detect",
},
},
};
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,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do jeito que fazemos hoje, estamos mandando email tanto pelo mailhog quanto pelo serviço original (e.g. gmail) caso as env keys do gmail estejam listadas em .env .
Acho melhor ser exclusivo - ou gmail ou mailhog

}

/**
* 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,
};
}
24 changes: 24 additions & 0 deletions hooks/useSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Wraps NextAuth useSession but overriding the typing to include our custom fields.

import {
Session as NextAuthSession,
useSession as nextAuthUseSession,
} from "next-auth/client";

export type User = {
email: string;
firstName: string;
lastName: string;
};

export interface Session extends NextAuthSession {
user: User;
}

// Tricking typescript to believe that we know what we are doing.
const useSession = (): [Session | null, boolean] => {
const [session, loading] = nextAuthUseSession();
return [(session as unknown) as Session | null, loading];
};

export default useSession;
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ["./configureTestEnvironmentVariables.js"]
};
preset: "ts-jest",
testEnvironment: "node",
setupFiles: ["./configureTestEnvironmentVariables.js"],
};
1 change: 1 addition & 0 deletions next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/* eslint-disable spaced-comment */
/// <reference types="next" />
/// <reference types="next/types/global" />
8 changes: 4 additions & 4 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
env: {
PAGARME_ENC_KEY: process.env.PAGARME_ENC_KEY,
ROLLOUT_API_KEY: process.env.ROLLOUT_API_KEY
ROLLOUT_API_KEY: process.env.ROLLOUT_API_KEY,
},
serverRuntimeConfig: {
PROJECT_ROOT: __dirname
}
}
PROJECT_ROOT: __dirname,
},
};
Loading