-
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 13, 2019
1 parent
0941dce
commit 5b0897e
Showing
25 changed files
with
731 additions
and
125 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 |
---|---|---|
|
@@ -37,3 +37,5 @@ coverage | |
.node_repl_history | ||
|
||
.env | ||
|
||
package-lock.json |
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,91 @@ | ||
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); | ||
const verifiedUser = await User.update({ | ||
verified: true | ||
}, { | ||
where: { | ||
email: user.user.email | ||
} | ||
}); | ||
if (verifiedUser) { | ||
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,23 @@ | ||
|
||
|
||
const environments = { | ||
development: { | ||
dbUrl: process.env.DATABASE_URL, | ||
}, | ||
staging: { | ||
dbUrl: process.env.DATABASE_URL, | ||
}, | ||
test: { | ||
dbUrl: process.env.DATABASE_URL, | ||
}, | ||
}; | ||
|
||
// Determine which environment we are in | ||
const currentEnvironment = typeof (process.env.NODE_ENV) === 'string' ? process.env.NODE_ENV.toLowerCase() : ''; | ||
|
||
// Check that the current environment is one the envs defined above, if not default to development | ||
const environment = typeof (environments[currentEnvironment]) === 'object' | ||
? environments[currentEnvironment] : environments.development; | ||
|
||
// Export the selected environment configuration object | ||
export default environment; |
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,35 @@ | ||
import Sequelize from 'sequelize'; | ||
import dbUrlParser from '../helpers/dbUrlParser'; | ||
import environment from '../config/environments'; | ||
|
||
const { | ||
dbUser, | ||
dbPassword, | ||
dbName, | ||
dbHost | ||
} = dbUrlParser(environment.dbUrl); | ||
|
||
const sequelize = new Sequelize( | ||
dbName, | ||
dbUser, | ||
dbPassword, | ||
{ | ||
dialect: 'postgres', | ||
host: dbHost, | ||
logging: false, | ||
}, | ||
); | ||
|
||
const models = { | ||
// User: sequelize.import('../models/user'), | ||
}; | ||
|
||
Object.keys(models).forEach((key) => { | ||
if ('associate' in models[key]) { | ||
models[key].associate(models); | ||
} | ||
}); | ||
|
||
export { sequelize }; | ||
|
||
export default models; |
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,7 @@ | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const env = (param, value) => process.env[param] || value; | ||
|
||
export default 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import dotenv from 'dotenv'; | ||
import env from './Env.helper'; | ||
|
||
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="${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,39 @@ | ||
|
||
import mailer from 'nodemailer'; | ||
import env from './Env.helper'; | ||
import mailTemplate from './MailTemplate.helper'; | ||
|
||
const transporter = mailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: env('AUTHOSHAVEN_USER'), | ||
pass: 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<${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; |
Oops, something went wrong.