@@ -76,6 +76,7 @@ register.schema = {
76
76
77
77
// the joi schema for register user via social login
78
78
registerSocialUser . schema = {
79
+ auth : joi . object ( ) . required ( ) ,
79
80
entity : joi . object ( ) . keys ( {
80
81
name : joi . string ( ) . required ( ) ,
81
82
email : joi . string ( ) . email ( ) . required ( ) ,
@@ -125,16 +126,23 @@ function* register(entity) {
125
126
/**
126
127
* Register a user via social login
127
128
*
129
+ * @param {Object } auth the currently logged in user context
128
130
* @param {Object } entity the post entity from the client
129
131
*/
130
- function * registerSocialUser ( entity ) {
132
+ function * registerSocialUser ( auth , entity ) {
131
133
// 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 ) ;
134
138
let user ;
135
139
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 ( ) ;
137
143
} else {
144
+ entity . socialNetworkId = auth . sub ;
145
+ entity . socialNetworkType = auth . sub . substring ( 0 , auth . sub . indexOf ( '|' ) ) ;
138
146
entity . role = Role . CONSUMER ;
139
147
user = yield User . create ( entity ) ;
140
148
}
@@ -211,17 +219,19 @@ forgotPassword.schema = {
211
219
*/
212
220
function * forgotPassword ( entity ) {
213
221
const code = Math . floor ( Math . random ( ) * 100000 ) . toString ( 16 ) ;
214
- const subject = 'Reset your password' ;
222
+ const subject = config . RESET_PASSWORD_SUBJECT ;
215
223
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 ) ;
219
225
const html = `<p>${ text } </p>` ;
220
226
221
227
const user = yield User . findOne ( { email : entity . email } ) ;
222
228
if ( ! user ) {
223
229
throw new errors . NotFoundError ( 'user not found with the specified email' ) ;
224
230
}
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
+ }
225
235
226
236
user . resetPasswordCode = code ;
227
237
const date = new Date ( ) ;
@@ -254,6 +264,10 @@ function* resetPassword(entity) {
254
264
user . resetPasswordExpiration . getTime ( ) - new Date ( ) . getTime ( ) < 0 ) {
255
265
throw new errors . HttpStatusError ( 400 , 'invalid code' ) ;
256
266
}
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
+ }
257
271
258
272
user . password = yield helper . hashString ( entity . password , config . SALT_WORK_FACTOR ) ;
259
273
user . resetPasswordCode = null ;
0 commit comments