Skip to content

Commit

Permalink
Send email with login URL/token. Ref async-labs#20
Browse files Browse the repository at this point in the history
  • Loading branch information
delgermurun committed May 14, 2019
1 parent 6fbb696 commit 43e7f4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
26 changes: 21 additions & 5 deletions api/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as passport from 'passport';
import { OAuth2Strategy as Strategy } from 'passport-google-oauth';
import * as passwordless from 'passwordless';

import sendEmail from './aws-ses';
import logger from './logs';
import getEmailTemplate from './models/EmailTemplate';
import Invitation from './models/Invitation';
import User, { IUserDocument } from './models/User';
import PasswordlessMongoStore from './passwordless';
Expand All @@ -14,12 +16,26 @@ const URL_APP = dev ? 'http://localhost:3000' : PRODUCTION_URL_APP;
function setupPasswordless({ server, ROOT_URL }) {
passwordless.init(new PasswordlessMongoStore());

passwordless.addDelivery((tokenToSend, uidToSend, recipient, callback, req) => {
const text = `Hello!\nAccess your account here:
${ROOT_URL}/auth/logged_in?token=${tokenToSend}&uid=${encodeURIComponent(uidToSend)}`;
passwordless.addDelivery(async (tokenToSend, uidToSend, recipient, callback) => {
try {
const template = await getEmailTemplate('login', {
loginURL: `${ROOT_URL}/auth/logged_in?token=${tokenToSend}&uid=${encodeURIComponent(
uidToSend,
)}`,
});

await sendEmail({
from: `Kelly from async-await.com <${process.env.EMAIL_SUPPORT_FROM_ADDRESS}>`,
to: [recipient],
subject: template.subject,
body: template.message,
});

logger.debug(text, recipient, req.body);
callback();
callback();
} catch (err) {
logger.error('Email sending error:', err);
callback(err);
}
});

server.use(passwordless.sessionSupport());
Expand Down
6 changes: 6 additions & 0 deletions api/server/models/EmailTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ async function insertTemplates() {
<p>View it at <a href="<%= discussionLink %>"><%= discussionLink %></a>.</p>
`,
},
{
name: 'login',
subject: 'Login url for Async SaaS',
message: `<p>Hello!<p>
<p>Access your account here: <a href="<%= loginURL %>"><%= loginURL %></a>.</p>`,
},
];

for (const t of templates) {
Expand Down
2 changes: 1 addition & 1 deletion app/components/common/LoginButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LoginButton extends React.PureComponent<
{ next?: string; invitationToken?: string },
{ email: string }
> {
public state = { email: '[email protected]' };
public state = { email: '' };

public render() {
const { next, invitationToken } = this.props;
Expand Down

0 comments on commit 43e7f4c

Please sign in to comment.