Skip to content

Commit

Permalink
Merge a53e4f2 into 723d2ea
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammuel09 committed Feb 13, 2019
2 parents 723d2ea + a53e4f2 commit 5dba263
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 45 deletions.
56 changes: 33 additions & 23 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,29 @@ class UserController {
fullName, userName, email, password
} = req.body;

const foundUser = await User.findOne({ where: { email } });
const foundUser = await User.findOne({
where: {
[Op.or]: [{ email }, { userName }]
}
});

if (foundUser) {
if (foundUser.isVerified === false) {
const initialToken = TokenManager.sign({ userEmail: foundUser.email }, '24h');
const sentEmail = await MailManager.sendVerificationEmail({
createdUser: foundUser,
token: initialToken
if (foundUser.userName === userName) {
return res.status(409).send({
status: 'failure',
data: {
statusCode: 409,
message: 'Username has already been taken'
}
});
if (sentEmail) {
return res.status(200).send({
status: 'success',
data: {
statusCode: 200,
message:
'You had started your registration process earlier. '
+ 'Kindly check your email to complete your registration'
}
});
}
}
if (foundUser.isVerified === true) {
return res.status(200).send({
status: 'success',

if (foundUser.email === email) {
return res.status(409).send({
status: 'failure',
data: {
message: "You have already been registered on Author's Haven. Kindly proceed to login"
statusCode: 409,
message: 'Email has already been taken'
}
});
}
Expand Down Expand Up @@ -299,9 +296,22 @@ class UserController {
const { email } = req.body;
const foundUser = await User.findOne({ where: { email } });

if (!foundUser) {
return res.status(404).send({
status: 'failure',
data: {
statusCode: 404,
message: 'This email address does not exist. Kindly sign up'
}
});
}

const token = TokenManager.sign({ userEmail: email }, '24h');

await MailManager.sendVerificationEmail({ createdUser: foundUser, token });
await MailManager.sendVerificationEmail({
createdUser: foundUser,
token
});

return res.status(200).send({
status: 'success',
Expand All @@ -311,7 +321,7 @@ class UserController {
}
});
} catch (err) {
res.status(400).send({
res.status(500).send({
status: 'failure',
data: {
error: err
Expand Down
8 changes: 6 additions & 2 deletions server/helpers/MailManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class MailManager {
* @memberof MailManager
*/
static sendMailNotification(message) {
return sgMail.send(message);
return process.env.NODE_ENV === 'test'
? Promise.resolve()
: sgMail.send(message);
}

/**
Expand All @@ -38,7 +40,9 @@ class MailManager {
subject: "Welcome to Author's Haven! Confirm your email",
html: verifyEmailTemplate(createdUser, token)
};
return sgMail.send(message);
return process.env.NODE_ENV === 'test'
? Promise.resolve()
: sgMail.send(message);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/helpers/emailTemplates/newArticleTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const newArticleTemplate = (follower, article, userName) => `<!DOCTYPE html>
article.slug
}>new article</a> .
</p>
<a class="reset-btn" href=https://neon-ah-frontend-staging.herokuapp.com//articles/${
<a class="reset-btn" href=https://neon-ah-frontend-staging.herokuapp.com/articles/${
article.slug
}>
Read Article
Expand Down
Loading

0 comments on commit 5dba263

Please sign in to comment.