-
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.
- Loading branch information
Showing
22 changed files
with
485 additions
and
91 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-env"] | ||
} | ||
"presets": ["@babel/preset-env"], | ||
"plugins": ["@babel/plugin-transform-runtime"] | ||
} |
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,2 +1,3 @@ | ||
DATABASE_URL_TEST = postgresql://neon:''@localhost:5432/haven_test | ||
DATABASE_URL_DEV = postgresql://neon:''@localhost:5432/haven_dev | ||
JWT_SECRETE = <...> |
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,4 @@ | ||
|
||
build/ | ||
server/migrations/ | ||
server/test/ |
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,8 +1,11 @@ | ||
sudo: required | ||
language: node_js | ||
node_js: | ||
- "stable" | ||
- 'stable' | ||
cache: | ||
directories: | ||
- node_modules | ||
script: | ||
- npm test | ||
- npm test | ||
after_success: | ||
- npm run coverage | ||
- npm run coverage |
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,18 +1,21 @@ | ||
import express from 'express'; | ||
import swaggerUi from 'swagger-ui-express'; | ||
import YAML from 'yamljs'; | ||
import routes from './server/routes'; | ||
|
||
const swaggerDocument = YAML.load('swagger.yaml'); | ||
const app = express(); | ||
app.use(express.json()); | ||
|
||
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | ||
app.get('/', (req, res) => { | ||
res.status(200).json('Welcome to Todos'); | ||
}); | ||
|
||
app.use('/api/v1', routes); | ||
|
||
const port = process.env.PORT || 3000; | ||
|
||
app.listen(port, () => { | ||
// eslint-disable-next-line | ||
console.log(`Server started on ${port}`); | ||
}); | ||
|
||
export default app; |
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
Empty file.
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,87 @@ | ||
import TokenManager from '../helpers/TokenManager'; | ||
import MailManager from '../helpers/MailManager'; | ||
import db from '../models'; | ||
import PasswordManager from '../helpers/PasswordManager'; | ||
|
||
const { Users } = db; | ||
|
||
/** | ||
* @class UserController | ||
*/ | ||
class UserController { | ||
/** | ||
* | ||
* @description Method to send password reset link. | ||
* @static | ||
* @param {*} req Express Request object | ||
* @param {*} res Express Response object | ||
* @returns {object} Json response | ||
* @memberof UserController | ||
*/ | ||
static async forgotPassword(req, res) { | ||
try { | ||
const { email } = req.body; | ||
const user = await Users.findOne({ where: { email } }); | ||
if (!user) { | ||
res.status(404).send({ status: 'failure', message: 'User not found' }); | ||
return; | ||
} | ||
|
||
const token = TokenManager.sign({ email }, '24h'); | ||
await MailManager.sendPasswordResetLink({ user, token }); | ||
|
||
res | ||
.status(200) | ||
.send({ status: 'success', message: 'Kindly check your mail to reset your password' }); | ||
} catch (error) { | ||
res.status(500).send({ error }); | ||
} | ||
} | ||
|
||
/** | ||
* @description Method to reset user's password. | ||
* @static | ||
* @param {*} req Express Request object | ||
* @param {*} res Express Response object | ||
* @returns {object} Json response | ||
* @memberof UserController | ||
*/ | ||
static async passwordReset(req, res) { | ||
try { | ||
const { token } = req.params; | ||
|
||
const { email } = TokenManager.verify(token); | ||
|
||
const { newPassword, confirmPassword } = req.body; | ||
const isPasswordEqual = newPassword === confirmPassword; | ||
|
||
if (!isPasswordEqual) { | ||
return res.status(400).send({ status: 'failure', message: 'Password does not match' }); | ||
} | ||
|
||
const user = await Users.findOne({ where: { email } }); | ||
|
||
if (!user) { | ||
res.status(404).send({ status: 'failure', message: 'User not found' }); | ||
return; | ||
} | ||
|
||
await Users.update( | ||
{ password: PasswordManager.hashPassword(newPassword) }, | ||
{ where: { email } } | ||
); | ||
|
||
res.status(200).send({ | ||
status: 'success', | ||
message: 'Password has been successfully updated. Kindly login.' | ||
}); | ||
} catch (error) { | ||
if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') { | ||
return res.status(401).send({ status: 'failure', message: error.name }); | ||
} | ||
res.status(500).send({ error }); | ||
} | ||
} | ||
} | ||
|
||
export default UserController; |
Empty file.
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,34 @@ | ||
import sgMail from '@sendgrid/mail'; | ||
import env from 'dotenv'; | ||
import passwordResetEmailTemplate from './emailTemplates/resetPasswordTemplate'; | ||
|
||
env.config(); | ||
|
||
sgMail.setApiKey(process.env.SENDGRID_API_KEY); | ||
|
||
/** | ||
* | ||
* @description This class manages all things mailing. | ||
* @class MailManager | ||
*/ | ||
class MailManager { | ||
/** | ||
* @description Utility funcion to send password reset link | ||
* @returns {Promise} promise object that represents mail success | ||
* @static | ||
* @param {string} email Email address if the user | ||
* @memberof MailManager | ||
*/ | ||
static sendPasswordResetLink({ user, token }) { | ||
const message = { | ||
to: `${user.email}`, | ||
from: 'notification@neon-ah.com', | ||
subject: 'Password Reset Link', | ||
html: passwordResetEmailTemplate(user, token) | ||
}; | ||
|
||
return sgMail.send(message); | ||
} | ||
} | ||
|
||
export default MailManager; |
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,19 @@ | ||
import bcrypt from 'bcryptjs'; | ||
|
||
/** | ||
* @class PasswordManager | ||
*/ | ||
class PasswordManager { | ||
/** | ||
* @description Helper method to hash passwords | ||
* @static | ||
* @param {string} password | ||
* @returns {string} A string representing the hashed password | ||
* @memberof PasswordManager | ||
*/ | ||
static hashPassword(password) { | ||
return bcrypt.hashSync(password, 10); | ||
} | ||
} | ||
|
||
export default PasswordManager; |
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,40 @@ | ||
import jwt from 'jsonwebtoken'; | ||
import env from 'dotenv'; | ||
|
||
env.config(); | ||
|
||
const secret = process.env.JWT_SECRET; | ||
|
||
/** | ||
* | ||
* | ||
* @class TokenManager | ||
*/ | ||
class TokenManager { | ||
/** | ||
* | ||
* @description Encodes a passed token and returns a jwt token | ||
* @static | ||
* @param {*} payload | ||
* @param {string} [ttl='2h'] | ||
* @returns {string} Jwt token | ||
* @memberof Tokenize | ||
*/ | ||
static sign(payload, ttl = '2h') { | ||
return jwt.sign(payload, secret, { expiresIn: ttl }); | ||
} | ||
|
||
/** | ||
* | ||
* @description Verifies a passed token and returns a decoded payload | ||
* @static | ||
* @param {string} token | ||
* @returns {object} Payload | ||
* @memberof Tokenize | ||
*/ | ||
static verify(token) { | ||
return jwt.verify(token, secret); | ||
} | ||
} | ||
|
||
export default TokenManager; |
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,37 @@ | ||
const passwordResetEmailTemplate = (user, token) => ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
<link href="https://fonts.googleapis.com/css?family=Hind:400,500" rel="stylesheet"> | ||
<style> | ||
* {font-family: 'Hind';} | ||
.container {width: 80%;margin: 0 auto;background: #fbfbfb;padding: 30px;} | ||
.username {font-size: 1.2rem;} | ||
.message{font-size: 1.2rem;} | ||
.reset-btn {display: inline-block;background: #2fb5ee;padding: 10px; color: #fff !important;text-decoration: none;font-size: 1rem;} | ||
.logo {width: 165px; padding-bottom: 20px; border-bottom: 1px solid #2fb5ee;} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<img class="logo" | ||
src="https://res.cloudinary.com/jesseinit/image/upload/v1547303525/Logo.png" | ||
alt="logo" | ||
/> | ||
<h3 class="username"">Hi ${user.lastName},</h3> | ||
<p class="message"> | ||
You recently requested to reset your password for your Authors Haven's account. Use the | ||
button below to reset it. This password reset is only valid for the next 24 hours. | ||
</p> | ||
<a class="reset-btn" href="http://localhost:3000/reset/${token}"> | ||
Reset your password | ||
</a> | ||
</div> | ||
</body> | ||
</html> | ||
`; | ||
|
||
export default passwordResetEmailTemplate; |
Oops, something went wrong.