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

sendVerificationEmail and setup email #59

Merged
merged 10 commits into from
Feb 16, 2017
Merged

sendVerificationEmail and setup email #59

merged 10 commits into from
Feb 16, 2017

Conversation

pradel
Copy link
Member

@pradel pradel commented Feb 15, 2017

#40
Setup email part.
Add siteUrl in config to construct absolute urls for email.
Add sendVerificationEmail.
Add sendResetPasswordEmail.

To overwrite the email templates:

AccountsServer.config({
 siteUrl: 'https://my-app.com',
 email: // a valid email config object passed to emailjs
 // https://github.com/eleith/emailjs#example-usage---text-only-emails
});

AccountsServer.emailTemplates.from = 'my-app <[email protected]>';
AccountsServer.emailTemplates.verifyEmail.subject = (user) => `Verify your account email ${user.profile.lastname}`;
// Same for AccountsServer.emailTemplates.verifyEmail.text = (user, url)

@davidyaha davidyaha self-requested a review February 15, 2017 15:06
Copy link
Member

@davidyaha davidyaha left a comment

Choose a reason for hiding this comment

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

This PR looks good, I think you should implement the verify-email route as well for the PR to be complete.

if (!address || !includes(user.emails.map((email: string) => email.address), address)) {
throw new AccountsError('No such email address for user');
}
const token = await this.db.addEmailVerificationToken(userId, address);
Copy link
Member

Choose a reason for hiding this comment

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

The db should layer should probably not be responsible of creating the token as this will be the same logic for every store. The only caveat with moving that logic to here is that the token should be unique and so we need the db to allow search by email verification token to make sure the "new token" is valid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now there is this function that generate the token https://github.com/js-accounts/accounts/blob/master/packages/server/src/tokens.js#L5 and in verifyEmail function there is this call to the bdd (who must take the token as parameter btw) to find the user with the token

throw new AccountsError('No such email address for user');
}
const token = await this.db.addEmailVerificationToken(userId, address);
const resetPasswordUrl = `${this._options.siteUrl}/reset-password/${token}`;
Copy link
Member

Choose a reason for hiding this comment

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

Why this has reset-password route?

@pradel
Copy link
Member Author

pradel commented Feb 15, 2017

@davidyaha Now the token is generated in the function, is it better?

@@ -302,6 +321,57 @@ export class AccountsServer {
const res = await this.db.setProfile(userId, { ...user.profile, ...profile });
return res;
}

async sendVerificationEmail(userId: string, address: string): Promise<void> {
const user = await this.db.findUserById(userId);
Copy link
Member

Choose a reason for hiding this comment

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

You should probably extract the code from this line to L338 to another function because it is repeating on the sendResetPasswordEmail method

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not exactly the same things for sendVerificationEmail this will return the first unverified email.
For sendResetPasswordEmail it will return the first email.

Copy link
Member

@TimMikeladze TimMikeladze left a comment

Choose a reason for hiding this comment

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

This looks good. Will you be adding the checks for expired tokens in this PR or are those todos for the future?

const emailRecord = find(user.emails, (e: Object) => e.address === tokenRecord.address);
if (!emailRecord) {
throw new AccountsError('Verify email link is for unknown address');
}
await this.db.verifyEmail(user.id, emailRecord);
}

async resetPassword(token: string, newPassword: string): Promise<void> {
const user = await this.db.findUserByResetPasswordToken(token);
Copy link
Member

Choose a reason for hiding this comment

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

Could be named findByResetPasswordToken. For example theres no "User" in the findByEmail name.

Copy link
Member Author

Choose a reason for hiding this comment

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

All our find are with Usereven findUserByEmail

Copy link
Member

Choose a reason for hiding this comment

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

oops nevermind then 😄


sendMail(mail: Object): Promise<Object> {
return new Promise((resolve, reject) => { // eslint-disable-line flowtype/require-parameter-type
// If no configuration for email just warm the user
Copy link
Member

Choose a reason for hiding this comment

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

Typo, "warn" not "warm"

resolve();
return;
}
this.server.send(mail, (err: Object, message: Object) => {
Copy link
Member

Choose a reason for hiding this comment

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

Would be cool if user could set a function in the config to handle sending emails. At the moment I think this only supports smtp, but what about the use case when you want to do an api call to mailgun to send the email?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah good idea 👍

@pradel
Copy link
Member Author

pradel commented Feb 15, 2017

@TimMikeladze If you can review the README

@TimMikeladze
Copy link
Member

@pradel Looks good

@pradel pradel merged commit 32ae3e6 into master Feb 16, 2017
@pradel pradel deleted the start-send-email branch February 21, 2017 17:50
@ibelarouci
Copy link

you must modify the resolver file of mutaion function
sendVerificationEmail
sendResetEmail and other functions to return a Boolean (true value) if the operation is succeed
by adding a return true in block Try or updating the last return
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants