Skip to content

Commit

Permalink
Merge 58f5661 into fc01be0
Browse files Browse the repository at this point in the history
  • Loading branch information
Makwe-O committed Feb 15, 2019
2 parents fc01be0 + 58f5661 commit 781eaaa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
56 changes: 43 additions & 13 deletions server/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import response from '../helpers/response';

import passwordResetEmailTemplate from '../helpers/emailTemplates/resetPasswordTemplate';

const { User, Sequelize } = db;
const { User, Article, Follow, Sequelize } = db;
const { Op } = Sequelize;

/**
Expand Down Expand Up @@ -133,12 +133,16 @@ class UserController {
}
});
} catch (error) {
if (error.name === 'TokenExpiredError' || error.name === 'JsonWebTokenError') {
if (
error.name === 'TokenExpiredError' ||
error.name === 'JsonWebTokenError'
) {
return res.status(401).send({
status: 'failure',
data: {
statusCode: '401',
message: 'Sorry! Link has expired. Kindly re-initiate password reset.'
message:
'Sorry! Link has expired. Kindly re-initiate password reset.'
}
});
}
Expand All @@ -161,9 +165,7 @@ class UserController {
*/
static async signUp(req, res) {
try {
const {
fullName, userName, email, password
} = req.body;
const { fullName, userName, email, password } = req.body;

const foundUser = await User.findOne({
where: {
Expand Down Expand Up @@ -436,9 +438,7 @@ class UserController {
}
};

const {
id, displayName, emails, photos, provider
} = profile;
const { id, displayName, emails, photos, provider } = profile;

if (!emails) {
const userWithNoEmail = { hasNoEmail: true };
Expand Down Expand Up @@ -478,7 +478,9 @@ class UserController {
*/
static handleSocialAuth(req, res) {
if (req.user.hasNoEmail) {
return res.redirect(`${process.env.FRONTEND_URL}/auth/social?error=${400}`);
return res.redirect(
`${process.env.FRONTEND_URL}/auth/social?error=${400}`
);
}
const payload = {
userId: req.user.id,
Expand All @@ -487,7 +489,9 @@ class UserController {
roleId: req.user.roleId
};
const token = TokenManager.sign(payload);
return res.redirect(`${process.env.FRONTEND_URL}/auth/social?token=${token}`);
return res.redirect(
`${process.env.FRONTEND_URL}/auth/social?token=${token}`
);
}

/**
Expand All @@ -503,15 +507,41 @@ class UserController {
const { userName } = req.params;
const userProfile = await User.findOne({
where: { userName: userName.toLowerCase() },
attributes: ['id', 'fullName', 'userName', 'img', 'bio']
attributes: ['id', 'fullName', 'userName', 'img', 'bio', 'email'],
include: [
{
model: Article,
as: 'articles'
},
{
model: User,
through: 'Follow',
as: 'following',
attributes: ['id', 'fullName', 'userName', 'img', 'bio']
},
{
model: User,
through: 'Follow',
as: 'followers',
attributes: ['id', 'fullName', 'userName', 'img', 'bio']
}
]
});

if (!userProfile) {
response(res, 404, 'failure', 'User not found');
return;
}
response(res, 200, 'success', 'User retrieved successfully', null, userProfile);
response(
res,
200,
'success',
'User retrieved successfully',
null,
userProfile
);
} catch (error) {
console.log(error);
response(res, 500, 'failure', 'An error occured on the server');
}
}
Expand Down
6 changes: 3 additions & 3 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default (sequelize, DataTypes) => {
},
{}
);
User.associate = (models) => {
User.associate = models => {
const {
Article,
AuthType,
Expand Down Expand Up @@ -149,12 +149,12 @@ export default (sequelize, DataTypes) => {
});
User.belongsToMany(User, {
through: 'Follow',
as: 'followers',
as: 'following',
foreignKey: 'followersId'
});
User.belongsToMany(User, {
through: 'Follow',
as: 'following',
as: 'followers',
foreignKey: 'userId'
});
User.hasMany(ReadingStats, {
Expand Down

0 comments on commit 781eaaa

Please sign in to comment.