Skip to content

Commit

Permalink
Merge 9ba5a4b into ab03bef
Browse files Browse the repository at this point in the history
  • Loading branch information
kevoese authored Jul 5, 2019
2 parents ab03bef + 9ba5a4b commit 7fa9ad3
Show file tree
Hide file tree
Showing 20 changed files with 478 additions and 241 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"consistent-return": 0,
"no-console": "off",
"no-param-reassign": 0,
"class-methods-use-this":"off",
"comma-dangle": 0,
"curly": ["error", "multi-line"],
"import/no-unresolved": [2, { "commonjs": true }],
Expand Down
26 changes: 26 additions & 0 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default {
createArticle: async (req, res) => {
const {
description, body, title, image
} = req.body;
try {
const article = await req.user.createArticle({
description,
body,
title,
images: image || 'defaultImgUrl',
});

return res.status(201).json({
message: 'Article created successful',
article,
});
} catch (e) {
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong',
error: e.message,
});
}
},
};
47 changes: 24 additions & 23 deletions controllers/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ export default {
lastName,
password,
email,
username
username,
});
user.response.token = getToken(user.id, user.email);
return res.status(201).json({
message: 'User Registration successful',
user: user.response()
user: user.response(),
});
} catch (e) {
console.log('message', e);
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong'
message: 'Something went wrong',
});
}
},
Expand All @@ -36,34 +36,35 @@ export default {
const { email, password } = req.body;
try {
const user = await db.User.findOne({
where: { email }
where: { email },
});
if (!user) {
return res.status(400).send({
error: 'Invalid email or password'
error: 'Invalid email or password',
});
}
const isPasswordValid = await user.passwordsMatch(password);
if (!isPasswordValid) {
return res.status(400).send({
error: 'Invalid email or password'
error: 'Invalid email or password',
});
}
return res.status(200).json({
message: 'User Login in successful',
user: user.response()
user: user.response(),
});
} catch (e) {
/* istanbul ignore next */
return res.status(500).json({
message: 'Something went wrong'
message: 'Something went wrong',
});
}
},
signOut: async (req, res) => {
const token = req.headers['x-access-token'];
await blackListThisToken(token);
return res.status(200).send({
message: 'Signed out successfully'
message: 'Signed out successfully',
});
},
resetPassword: async (req, res) => {
Expand All @@ -78,38 +79,38 @@ export default {
await sendMail({
email: user.email,
subject: 'Password Reset LInk',
content: resetPasswordMessage(user.email, passwordResetToken)
content: resetPasswordMessage(user.email, passwordResetToken),
});
return res.status(200).json({
message: 'Password reset successful. Check your email for password reset link!'
message: 'Password reset successful. Check your email for password reset link!',
});
}
throw new Error('User not found');
} catch (err) {
return res.status(400).json({
message: 'User does not exist'
message: 'User does not exist',
});
}
},

activate: async (req, res) => {
try {
const user = await db.User.findOne({
where: { emailVerificationToken: req.params.token, activated: false }
where: { emailVerificationToken: req.params.token, activated: false },
});
if (user) {
await user.update({ activated: true, emailVerificationToken: null });
return res.status(200).json({
message: 'Activation successful, You can now login',
user: user.response()
user: user.response(),
});
}
return res.status(400).json({
error: 'Invalid activation Link'
error: 'Invalid activation Link',
});
} catch (e) {
return res.status(400).json({
message: 'Bad request'
message: 'Bad request',
});
}
},
Expand All @@ -126,21 +127,21 @@ export default {
where: {
passwordResetToken: resetToken,
passwordResetExpire: {
[Op.gt]: new Date()
}
}
[Op.gt]: new Date(),
},
},
});
if (user) {
user.update({ password: passwordHash, resetToken: null, resetExpire: null });
return res.status(200).json({
message: 'Password has successfully been changed.'
message: 'Password has successfully been changed.',
});
}
throw new Error('Invalid operation');
} catch (err) {
return res.status(400).json({
message: 'Bad request'
message: 'Bad request',
});
}
}
},
};
48 changes: 37 additions & 11 deletions db/migrations/20190701141832-create-articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,60 @@ module.exports = {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
},
title: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT
type: Sequelize.TEXT,
},
slug: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
body: {
type: Sequelize.TEXT
type: Sequelize.TEXT,
},
images: {
type: Sequelize.STRING
type: Sequelize.STRING,
},
userId: {
type: Sequelize.INTEGER
type: Sequelize.INTEGER,
allowNull: false,
required: true,
references: {
model: 'Users',
key: 'id',
as: 'author',
},
},
tagId: {
type: Sequelize.INTEGER,
allowNull: true,
// references: {
// model: 'Tags',
// key: 'id',
// as: 'tag',
// },
},
categoryId: {
type: Sequelize.INTEGER,
allowNull: true,
required: true,
// references: {
// model: 'Categories',
// key: 'id',
// as: 'category',
// },
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
type: Sequelize.DATE,
},
}),
down: queryInterface => queryInterface.dropTable('articles')
down: queryInterface => queryInterface.dropTable('Articles'),
};
27 changes: 18 additions & 9 deletions db/models/Article.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import SequelizeSlugify from 'sequelize-slugify';

module.exports = (sequelize, DataTypes) => {
const Article = sequelize.define('Article', {
title: DataTypes.STRING,
description: DataTypes.TEXT,
slug: DataTypes.STRING,
body: DataTypes.TEXT,
images: DataTypes.STRING
}, {});
const Article = sequelize.define(
'Article',
{
title: DataTypes.STRING,
description: DataTypes.TEXT,
slug: DataTypes.STRING,
body: DataTypes.TEXT,
images: DataTypes.STRING,
},
{}
);

SequelizeSlugify.slugifyModel(Article, {
source: ['title'],
slugOptions: { lower: true },
overwrite: false,
column: 'slug'
column: 'slug',
});
Article.associate = models => Article.belongsTo(models.User);
Article.associate = (models) => {
Article.belongsTo(models.User, {
foreginKey: 'userId',
as: 'user',
});
};
return Article;
};
28 changes: 14 additions & 14 deletions db/models/User.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import bcrypt from 'bcryptjs';
import { getToken, randomString, hashPassword } from '../../utils';
import { getToken, randomString } from '../../utils';
import { sendMail } from '../../utils/mailer';
import { activationMessage } from '../../utils/mailer/mails';

Expand All @@ -19,30 +19,30 @@ module.exports = (sequelize, DataTypes) => {
emailVerificationToken: DataTypes.STRING,
activated: {
type: DataTypes.BOOLEAN,
default: false
}
default: false,
},
},
{
hooks: {
beforeCreate: async user => {
beforeCreate: async (user) => {
user.password = await bcrypt.hash(user.password, 10);
user.emailVerificationToken = randomString();
},
afterCreate: async user => {
afterCreate: async (user) => {
await sendMail({
email: user.email,
subject: 'Activate Account',
content: activationMessage(user.email, user.emailVerificationToken)
content: activationMessage(user.email, user.emailVerificationToken),
});
}
}
},
},
}
);
User.associate = models =>
User.hasMany(models.Article, {
foreignKey: 'userId',
cascade: true
});
User.associate = models => User.hasMany(models.Article, {
foreignKey: 'userId',
as: 'article',
cascade: true,
});

User.prototype.passwordsMatch = function match(password) {
return bcrypt.compare(password, this.password);
Expand All @@ -59,7 +59,7 @@ module.exports = (sequelize, DataTypes) => {
image: this.image,
firstName: this.firstName,
lastName: this.lastName,
id: this.id
id: this.id,
};
};
return User;
Expand Down
Loading

0 comments on commit 7fa9ad3

Please sign in to comment.