Skip to content

Commit

Permalink
(freeCodeCamp#157)add a 'you signed up with your google account' email
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldblanco committed Dec 14, 2017
1 parent 06a0322 commit b25a9fa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
3 changes: 2 additions & 1 deletion common/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const pageIdentifiers = {
CUSTOMER_UPDATED: 'customer-update',
DONATION_RECEIVED: 'donation-received',
DONATION_RECEIPT: 'donation-receipt',
PASSWORD_RESET: 'password-reset'
PASSWORD_RESET: 'password-reset',
PASSWORD_RESET_GOOGLE: 'password-reset-google'
}

export const pageTypes = {
Expand Down
1 change: 1 addition & 0 deletions server/config/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ export default async function sendEmail(
path: '/v3/mail/send',
body: mail.toJSON()
})

return sg.API(request)
}
22 changes: 9 additions & 13 deletions server/controllers/users/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import config from '../../config'
import {
BadRequestError,
NotFoundError,
UnauthorizedError,
ValidationError
UnauthorizedError
// ValidationError
} from '../../lib/errors'
import mailer from '../../lib/mail/mail-helpers'
import User from '../../models/user'
Expand All @@ -26,23 +26,19 @@ export const forgot = async function(req, res) {
// Lookup user by email
if (!req.body.email) throw new BadRequestError('Email is required')
const user = await User.findOne({email: req.body.email}).select('-salt -password')
if (!user) {
throw new ValidationError({email: 'No account with that email has been found'})
} else if (user.provider !== 'local') {
return res.status(400).send({
message: 'It seems like you signed up using your ' + user.provider + ' account'
})
} else {
if (user && user.provider !== 'local') {
if (user.provider === 'google') await mailer.sendPasswordGoogle(user)
} else if (user && user.provider === 'local') {
user.resetPasswordToken = token
user.resetPasswordExpires = Date.now() + 3600000 // 1 hour

const port = process.env.NODE_ENV === 'production' ? '' : `:${config.port}`
const url = `${config.protocol}://${config.host}${port}/users/reset-password/${token}`

await mailer.sendPasswordReset(user, url)
await user.save()
}

const port = process.env.NODE_ENV === 'production' ? '' : `:${config.port}`
const url = `${config.protocol}://${config.host}${port}/users/reset-password/${token}`

await mailer.sendPasswordReset(user, url)
res.send({message: 'Password reset email sent'})
}

Expand Down
11 changes: 11 additions & 0 deletions server/lib/mail/mail-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ export default {
)
},

async sendPasswordGoogle(user) {
const {firstName, lastName, email} = user
const fullName = `${firstName} ${lastName}`
await this.send(
email,
fullName,
pageIdentifiers.PASSWORD_RESET_GOOGLE,
{firstName, lastName, fullName}
)
},

async sendThanks(donation) {
const {donor} = donation
const {firstName, lastName, email} = donor
Expand Down
11 changes: 11 additions & 0 deletions server/lib/seed/seed-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ export const pages = [{
<p>Thanks for your donations to:</p>
<p><span class="ql-placeholder-content" data-id="receipt" data-label="Donation Receipt" data-required="true"></span></p>
`
}, {
identifier: pageIdentifiers.PASSWORD_RESET_GOOGLE,
title: 'Password Reset Google',
type: pageTypes.EMAIL,
subject: '<p><span class="ql-placeholder-content" data-id="organization" data-label="Foodbank Name"></span> Password Reset</p>',
body: `<p>Dear <span class="ql-placeholder-content" data-id="fullName" data-label="User Full Name"></span>,</p>
<p>You have requested to have your password reset for your account at <span class="ql-placeholder-content" data-id="organization" data-label="Foodbank Name"></span></p>
<p>you appear to have signed up with google, try logging in with google. If you have any problems give us a call to the <span class="ql-placeholder-content" data-id="supportNumber" data-label="Support Number"></span></p>
<strong>If you didn't make this request, you can ignore this email.</strong>
<p>The <span class="ql-placeholder-content" data-id="organization" data-label="Foodbank Name"></span> support team</p>
`
}]

const commonFields = [
Expand Down

0 comments on commit b25a9fa

Please sign in to comment.