Skip to content

Commit

Permalink
Merge ef2734e into 0750e40
Browse files Browse the repository at this point in the history
  • Loading branch information
tejiri4 committed Jan 18, 2019
2 parents 0750e40 + ef2734e commit 42a3078
Show file tree
Hide file tree
Showing 52 changed files with 532 additions and 163 deletions.
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

build/
server/migrations
server/seeders
server/test
server/test/*
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ package-lock.json

# personal files
swagger.1.yaml

#code formatter
.prettierrc

.vscode
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"license": "MIT",
"dependencies": {
"@sendgrid/mail": "^6.3.1",
"babel-polyfill": "^6.26.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
Expand All @@ -52,7 +53,7 @@
"slug": "^0.9.3",
"swagger-ui-express": "^4.0.2",
"underscore": "^1.9.1",
"babel-polyfill": "^6.26.0",
"uuid": "^3.3.2",
"yamljs": "^0.3.0"
},
"devDependencies": {
Expand Down
60 changes: 60 additions & 0 deletions server/controllers/CommentController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import uuidv4 from 'uuid/v4';
import db from '../models';
import response from '../helpers/response';
import TokenManager from '../helpers/TokenManager';

const id = uuidv4();

const { Article, Comment } = db;

/**
* @class UserController
*/
class CommentController {
/**
*
* @description Allow user to comment on an article
* @static
* @param {*} req Express Request object
* @param {*} res Express Response object
* @returns {object} Json response
* @memberof CommentController
*/
static async createComment(req, res) {
try {
const token = req.headers.authorization.split(' ')[1];
const { userId } = TokenManager.verify(token);
const { articleId, content } = req.body;
if (!content) {
return response(res, 400, 'failure', 'Enter a comment', null, null);
}
await Article.findAndCountAll({
where: {
id: articleId
}
});
await Comment.create({
id,
content,
userId,
articleId
});
const getComment = await Comment.findAll({
where: {
articleId
}
});
const allComment = [];
getComment.map(comment => allComment.push(comment.dataValues));
if (getComment) {
return response(res, 201, 'success', 'Comment created', null, allComment);
}
} catch (error) {
if (error.name === 'SequelizeDatabaseError') {
return response(res, 500, 'failure', 'Article not found', null, null);
}
}
}
}

export default CommentController;
56 changes: 24 additions & 32 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import MailManager from '../helpers/MailManager';
import db from '../models';
import PasswordManager from '../helpers/PasswordManager';

const {
User

} = db;
const { User } = db;

/**
* @class UserController
Expand All @@ -23,9 +20,7 @@ class UserController {
*/
static async forgotPassword(req, res) {
try {
const {
email
} = req.body;
const { email } = req.body;

const user = await User.findOne({
where: {
Expand All @@ -40,20 +35,21 @@ class UserController {
return;
}

const token = TokenManager.sign({
email
}, '24h');
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'
});
res.status(200).send({
status: 'success',
message: 'Kindly check your mail to reset your password'
});
} catch (error) {
res.status(500).send({
error
Expand All @@ -71,18 +67,11 @@ class UserController {
*/
static async passwordReset(req, res) {
try {
const {
token
} = req.params;
const { token } = req.params;

const {
email
} = TokenManager.verify(token);
const { email } = TokenManager.verify(token);

const {
newPassword,
confirmPassword
} = req.body;
const { newPassword, confirmPassword } = req.body;
const isPasswordEqual = newPassword === confirmPassword;

if (!isPasswordEqual) {
Expand All @@ -106,13 +95,16 @@ class UserController {
return;
}

await User.update({
password: PasswordManager.hashPassword(newPassword)
}, {
where: {
email
await User.update(
{
password: PasswordManager.hashPassword(newPassword)
},
{
where: {
email
}
}
});
);

res.status(200).send({
status: 'success',
Expand Down
2 changes: 1 addition & 1 deletion server/helpers/TokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TokenManager {
* @returns {string} Jwt token
* @memberof Tokenize
*/
static sign(payload, ttl = '2h') {
static sign(payload, ttl = '200h') {
return jwt.sign(payload, secret, { expiresIn: ttl });
}

Expand Down
23 changes: 23 additions & 0 deletions server/helpers/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
*
* @description Method to send response in a generic format.
* @param {*} res Express Response object
* @param {number} code HTTP response status code
* @param {string} status 'success' || 'failure'
* @param {string} message Message to user
* @param {object} error (optional) Error object
* @param {object} payload (optional) Payload data to return with the response
* @returns {object} Json response
*/

export default (res, code, status, message, error, payload) => {
res.status(code).json({
status,
data: {
statusCode: code,
message,
error,
payload
}
});
};
Empty file removed server/middlewares/.gitkeep
Empty file.
61 changes: 61 additions & 0 deletions server/middlewares/AuthMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import TokenManager from '../helpers/TokenManager';
/**
* @class AuthMiddleware
* @description class contains function for implementing Authentication middleware
*/
class AuthMiddleware {
/**
* @static
* @description a middleware function checking if a user is authenticated
* @param {object} req HTTP request object
* @param {object} res HTTP response object
* @param {function} next next middleware function
* @returns {object} returns error message if user is not authenticated
*/
static checkIfUserIsAuthenticated(req, res, next) {
try {
const { authorization } = req.headers;
if (!authorization) {
return res.status(401).send({
success: false,
status: 401,
error: {
message: 'You are not logged in, Token is needed!'
}
});
}

const token = authorization.split(' ')[1];
const decoded = TokenManager.verify(token);

if (decoded) {
req.user = decoded;
return next();
}
} catch (error) {
const { name } = error;
if (name === 'TokenExpiredError' || name === 'JsonWebTokenError') {
return res.status(401).send({
success: 'failure',
data: {
error: {
message: 'Token is invalid, You need to log in again'
}
}
});
}

return res.status(500).send({
success: 'failure',
data: {
error: {
message: 'An error occured on the server',
error
}
}
});
}
}
}

export default AuthMiddleware;
12 changes: 7 additions & 5 deletions server/migrations/20190111125231-create-user.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuidv4 from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: uuidv4()
},
fullName: {
type: Sequelize.STRING,
Expand Down Expand Up @@ -39,12 +41,12 @@ export default {
defaultValue: true
},
roleId: {
type: Sequelize.INTEGER,
type: Sequelize.UUID,
allowNull: false,
defaultValue: 1,
defaultValue: '3ceb546e-054d-4c1d-8860-e27c209d4ae3',
},
authTypeId: {
type: Sequelize.INTEGER,
type: Sequelize.UUID,
allowNull: false
},
createdAt: {
Expand Down
6 changes: 4 additions & 2 deletions server/migrations/20190111125359-create-role.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuidv4 from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Roles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: uuidv4()
},
type: {
type: Sequelize.STRING,
Expand Down
6 changes: 4 additions & 2 deletions server/migrations/20190111125457-create-notificationType.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuidv4 from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('NotificationTypes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: uuidv4()
},
type: {
type: Sequelize.STRING,
Expand Down
6 changes: 4 additions & 2 deletions server/migrations/20190111125509-create-authType.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuidv4 from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('AuthTypes', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: uuidv4()
},
type: {
type: Sequelize.STRING,
Expand Down
8 changes: 5 additions & 3 deletions server/migrations/20190111130213-create-article.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuidv4 from 'uuid/v4';

export default {
up: (queryInterface, Sequelize) => queryInterface.createTable('Articles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: uuidv4()
},
slug: {
type: Sequelize.STRING,
Expand Down Expand Up @@ -35,7 +37,7 @@ export default {
defaultValue: false
},
userId: {
type: Sequelize.INTEGER
type: Sequelize.UUID
},
createdAt: {
allowNull: false,
Expand Down
Loading

0 comments on commit 42a3078

Please sign in to comment.