-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #165412916] Send confirmation email when email is updated
- Loading branch information
Showing
6 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import notification from './notification'; | ||
import signup from './signup'; | ||
import resetPassword from './resetPassword'; | ||
import updateEmail from './updateEmail'; | ||
|
||
export { notification }; | ||
export { notification, updateEmail }; | ||
export { signup, resetPassword }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import dotenv from 'dotenv'; | ||
import { generate as generateToken } from '../../tokens'; | ||
|
||
dotenv.config(); | ||
|
||
export default (data) => { | ||
const message = {}; | ||
const token = generateToken({ email: data.email }, { expiresIn: '1h' }); | ||
const appUrl = process.env.APP_URL_FRONTEND; | ||
const link = `${appUrl}/reset-password/${token}`; | ||
message.subject = 'Reset your password - Authors Haven'; | ||
message.html = `Hello</br>, | ||
<p>You are receiving this because you (or someone else) have requested the reset of your current email,<br> | ||
Click on the link bellow to confirm the process<br> | ||
<br><br><br> | ||
<a href='${link}' style="margin:35px 0;padding:15px 35px;background:#266cef;color:#ffffff;clear:both;border-radius:30px;text-decoration:none" target='_blank'>Confirm email</a></p>`; | ||
return message; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import status from '../config/status'; | ||
import * as helper from '../helpers'; | ||
import { User } from '../queries'; | ||
|
||
export default async (req, res, next) => { | ||
let [isEmailUsed, isSameOldEmail] = [false, false]; | ||
req.changeEmail = { newEmail: '', message: '' }; | ||
|
||
if (req.body.password) { | ||
req.body.password = helper.password.hash(req.body.password); | ||
} | ||
if (req.body.email && req.user.role !== 'admin') { | ||
const findUser = await User.findOne({ email: req.body.email }); | ||
if (!findUser.errors && Object.keys(findUser).length > 0) { | ||
isEmailUsed = req.user.id !== findUser.id; | ||
isSameOldEmail = req.body.email === findUser.email; | ||
} | ||
if (!isSameOldEmail) { | ||
req.changeEmail.newEmail = req.body.email; | ||
req.changeEmail.message = 'Check your email to confirm your new email'; | ||
} | ||
delete req.body.email; | ||
} | ||
|
||
return ( | ||
(isEmailUsed && res.status(status.EXIST).json({ errors: { email: 'email already used' } })) | ||
|| next() | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters