-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(project) Send an account verification link via email
- Loading branch information
Mireille Niwemuhuza
authored and
Elie Mugenzi
committed
Jun 14, 2019
1 parent
0941dce
commit f2df6dd
Showing
22 changed files
with
633 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ LOCAL_DB_USER= | |
LOCAL_DB_PASSWORD= | ||
LOCAL_DB_NAME= | ||
TEST_DATABASE_URL= | ||
AUTHOSHAVEN_USER= | ||
AUTHOSHAVEN_PASS= | ||
BASE_URL= |
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 |
---|---|---|
|
@@ -37,3 +37,4 @@ coverage | |
.node_repl_history | ||
|
||
.env | ||
|
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
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,4 +1,89 @@ | ||
import db from '../../sequelize/models'; | ||
import TokenHelper from '../../helpers/Token.helper'; | ||
import Mailhelper from '../../helpers/SendMail.helper'; | ||
import HashHelper from '../../helpers/hashHelper'; | ||
|
||
export default { | ||
signup: () => {}, | ||
}; | ||
const { User } = db; | ||
|
||
/** | ||
* @author Elie Mugenzi | ||
* @class AuthController | ||
* @description this class performs the whole authentication | ||
*/ | ||
class AuthController { | ||
/** | ||
* | ||
* @param {Object} req - Request object | ||
* @param {Object} res - Response object | ||
* @returns {Object} - Response object | ||
*/ | ||
static async register(req, res) { | ||
const { | ||
firstName, lastName, email, password, username, dob, bio, gender | ||
} = req.body; | ||
if (Object.keys(req.body).length === 0) { | ||
return res.status(400).json({ | ||
status: 400, | ||
error: 'No data sent' | ||
}); | ||
} | ||
const newUser = await User.create({ | ||
firstName, | ||
lastName, | ||
email, | ||
password: HashHelper.hashPassword(password), | ||
username, | ||
dob, | ||
bio, | ||
gender, | ||
verified: false | ||
}); | ||
if (newUser) { | ||
const token = await TokenHelper.generateToken({ user: newUser }); | ||
Mailhelper.sendMail({ | ||
to: newUser.email, | ||
names: `${newUser.firstName} ${newUser.lastName}`, | ||
subject: 'Welcome to Authorshaven', | ||
message: 'Thank you for choosing Authorshaven', | ||
token | ||
}); | ||
|
||
res.status(201).json({ | ||
status: 201, | ||
message: 'We have sent an email to you to verify your account', | ||
token, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Verifies account | ||
* @param {Object} req - Request | ||
* @param {*} res - Response | ||
* @returns {Object} - Response | ||
*/ | ||
static async verifyAccount(req, res) { | ||
const { token } = req.query; | ||
try { | ||
const user = await TokenHelper.decodeToken(token); | ||
await User.update({ | ||
verified: true | ||
}, { | ||
where: { | ||
email: user.user.email | ||
} | ||
}); | ||
res.status(202).json({ | ||
status: 202, | ||
message: 'Account is now verified!' | ||
}); | ||
} catch (err) { | ||
res.status(400).json({ | ||
status: 400, | ||
error: 'Invalid Request' | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default AuthController; |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const mailTemplate = ({ | ||
to, | ||
token, | ||
names | ||
}) => { | ||
const template = ` | ||
<div style="background:#e5eeff;width:100%;padding:20px 0;"> | ||
<div style="max-width:760px;margin:0 auto;background:#ffffff"> | ||
<div style="background:#266cef;padding:10px;color:#ffffff;text-align:center;font-size:34px"> | ||
Authors Haven - Team Tesla | ||
</div> | ||
<div style="padding:0;"> | ||
</div> | ||
<div style="padding:20px;text-align:left;"> | ||
<p> | ||
Well ${names}, congratulations for choosing AuthorsHaven. | ||
To verify that ${to} is your email, could you please click this link below to verify your AuthorsHaven's account? | ||
<br/> | ||
<a href="${process.env.BASE_URL}/api/auth/verify/?token=${token}">Click here to verify your account</a> | ||
<br/> | ||
Here there is the link below where you can visit Andela and get more information about what's Andela | ||
</p> | ||
<a href="https://andela.com">Visit Andela's website</a> | ||
</div> | ||
<br> | ||
<div style="padding:20px;text-align:left;"> | ||
<b>Andela, Team @Tesla - Cohort 5</b> | ||
</div> | ||
</div> | ||
<div style="padding:35px 10px;text-align:center;"> | ||
Copyright, 2019<br> | ||
Andela, Team Tesla | ||
</div> | ||
</div> | ||
`; | ||
return template; | ||
}; | ||
|
||
export default mailTemplate; |
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,38 @@ | ||
|
||
import mailer from 'nodemailer'; | ||
import mailTemplate from './MailTemplate.helper'; | ||
|
||
const transporter = mailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.AUTHOSHAVEN_USER, | ||
pass: process.env.AUTHOSHAVEN_PASS | ||
} | ||
}); | ||
/** | ||
* @author Elie Mugenzi | ||
* @class MailHelper | ||
* @description A helper class for sending emails | ||
*/ | ||
class MailHelper { | ||
/** | ||
* Send mail | ||
* @param {Object} param0 - Object which contains email information | ||
* @returns {Object} Results after sending mail | ||
*/ | ||
static async sendMail({ | ||
to, names, subject, message, token | ||
}) { | ||
const msg = { | ||
from: `Authors Haven<${process.env.AUTHOSHAVEN_USER}>`, | ||
to, | ||
subject, | ||
text: message, | ||
html: mailTemplate({ to, token, names }) | ||
}; | ||
const result = await transporter.sendMail(msg); | ||
return result; | ||
} | ||
} | ||
|
||
export default MailHelper; |
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 |
---|---|---|
|
@@ -17,6 +17,4 @@ class Tokenizer { | |
return user; | ||
} | ||
} | ||
|
||
|
||
export default Tokenizer; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import bcrypt from 'bcrypt'; | ||
|
||
/** | ||
* @class HashHelper | ||
*/ | ||
class HashHelper { | ||
/** | ||
* Hashes password | ||
* @param {String} password - Password to hash | ||
* @returns {String} - hashed Password | ||
*/ | ||
static hashPassword(password) { | ||
return bcrypt.hashSync(password, 8); | ||
} | ||
|
||
/** | ||
* Compares Passwords | ||
* @param {String} password - Password provided by a user | ||
* @param {String} passwordToCompare - Password from Database | ||
* @returns {Boolean} -True if they're equal, otherwise false | ||
*/ | ||
static comparePassword(password, passwordToCompare) { | ||
return bcrypt.compareSync(password, passwordToCompare); | ||
} | ||
} | ||
|
||
export default HashHelper; |
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
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,17 @@ | ||
const validategender = (req, res, next) => { | ||
const { gender } = req.body; | ||
if (gender) { | ||
if (gender === 'M' || gender === 'F') { | ||
next(); | ||
} else { | ||
res.status(400).json({ | ||
status: 400, | ||
error: 'Gender should be represented by either "M" or "F" ' | ||
}); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}; | ||
|
||
export default validategender; |
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,57 @@ | ||
|
||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
firstName: { | ||
type: Sequelize.STRING | ||
}, | ||
lastName: { | ||
type: Sequelize.STRING | ||
}, | ||
username: { | ||
type: Sequelize.STRING | ||
}, | ||
email: { | ||
type: Sequelize.STRING | ||
}, | ||
password: { | ||
type: Sequelize.STRING | ||
}, | ||
bio: { | ||
type: Sequelize.TEXT | ||
}, | ||
image: { | ||
type: Sequelize.TEXT | ||
}, | ||
dateOfBirth: { | ||
type: Sequelize.DATE | ||
}, | ||
gender: { | ||
type: Sequelize.STRING | ||
}, | ||
provider: { | ||
type: Sequelize.STRING | ||
}, | ||
socialId: { | ||
type: Sequelize.STRING | ||
}, | ||
verified: { | ||
type: Sequelize.BOOLEAN | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Users') | ||
}; |
Oops, something went wrong.