Skip to content

Commit ee4f12b

Browse files
author
riteshsangwan
committed
fix some minor issues
1 parent d2f0ce9 commit ee4f12b

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

config/default.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ module.exports = {
3131
SMTP_USERNAME: process.env.SMTP_USERNAME,
3232
SMTP_PASSWORD: process.env.SMTP_PASSWORD,
3333
},
34+
RESET_PASSWORD_SUBJECT: 'Reset your password',
35+
RESET_PASSWORD_TEMPLATE: 'You received this email because you send a reset password request to us, ' +
36+
'if you never registered, please ignore. ' +
37+
'To reset your password <a href=":link">click here</a><br><br> -- Dsp Server Team',
38+
// this is a frontend url, the user is redirected to this url from user's mailbox
3439
RESET_PASSWORD_LINK: 'http://localhost:3000/reset-password?token=:token',
3540
};

controllers/UserController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function* register(req, res) {
5050
* @param res the response
5151
*/
5252
function* registerSocialUser(req, res) {
53-
res.json(yield UserService.registerSocialUser(req.body));
53+
res.json(yield UserService.registerSocialUser(req.auth, req.body));
5454
}
5555

5656
/**

enum.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
* Contains app enums and constants
88
*/
99
const SocialType = {
10-
GOOGLE: 'Google',
11-
FACEBOOK: 'Facebook',
10+
GOOGLE: 'google-oauth2',
11+
FACEBOOK: 'facebook',
1212
};
1313

1414
const DroneStatus = {

services/UserService.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ register.schema = {
7676

7777
// the joi schema for register user via social login
7878
registerSocialUser.schema = {
79+
auth: joi.object().required(),
7980
entity: joi.object().keys({
8081
name: joi.string().required(),
8182
email: joi.string().email().required(),
@@ -125,16 +126,23 @@ function* register(entity) {
125126
/**
126127
* Register a user via social login
127128
*
129+
* @param {Object} auth the currently logged in user context
128130
* @param {Object} entity the post entity from the client
129131
*/
130-
function* registerSocialUser(entity) {
132+
function* registerSocialUser(auth, entity) {
131133
// make sure the email is unique
132-
const existingUser = yield User.findOne({email: entity.email});
133-
134+
// we don't need to check here for social network type, as social network id itself
135+
// embed the social network type
136+
const existingUser = yield User.findOne({ $or: [{email: entity.email}, {socialNetworkId: auth.sub}] });
137+
console.log(auth);
134138
let user;
135139
if (existingUser) {
136-
user = existingUser;
140+
// update social network type
141+
existingUser.socialNetworkType = auth.sub.substring(0, auth.sub.indexOf('|'));
142+
user = yield existingUser.save();
137143
} else {
144+
entity.socialNetworkId = auth.sub;
145+
entity.socialNetworkType = auth.sub.substring(0, auth.sub.indexOf('|'));
138146
entity.role = Role.CONSUMER;
139147
user = yield User.create(entity);
140148
}
@@ -211,17 +219,19 @@ forgotPassword.schema = {
211219
*/
212220
function* forgotPassword(entity) {
213221
const code = Math.floor(Math.random() * 100000).toString(16);
214-
const subject = 'Reset your password';
222+
const subject = config.RESET_PASSWORD_SUBJECT;
215223
const link = config.RESET_PASSWORD_LINK.replace(':token', code);
216-
const text = 'You received this email because you send a reset password request to us, ' +
217-
'if you never registered, please ignore. ' +
218-
`To reset your password <a href="${link}">click here</a><br><br> -- Dsp Server Team`;
224+
const text = config.RESET_PASSWORD_TEMPLATE.replace(':link', link);
219225
const html = `<p>${text}</p>`;
220226

221227
const user = yield User.findOne({email: entity.email});
222228
if (!user) {
223229
throw new errors.NotFoundError('user not found with the specified email');
224230
}
231+
// check if the user is social network user, and if yes than don't allow forgot password
232+
if (user.socialNetworkId) {
233+
throw new errors.ValidationError('social network user cannot reset password', httpStatus.BAD_REQUEST);
234+
}
225235

226236
user.resetPasswordCode = code;
227237
const date = new Date();
@@ -254,6 +264,10 @@ function* resetPassword(entity) {
254264
user.resetPasswordExpiration.getTime() - new Date().getTime() < 0) {
255265
throw new errors.HttpStatusError(400, 'invalid code');
256266
}
267+
// check if the user is social network user, and if yes than don't allow forgot password
268+
if (user.socialNetworkId) {
269+
throw new errors.ValidationError('social network user cannot reset password', httpStatus.BAD_REQUEST);
270+
}
257271

258272
user.password = yield helper.hashString(entity.password, config.SALT_WORK_FACTOR);
259273
user.resetPasswordCode = null;

0 commit comments

Comments
 (0)