@@ -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,22 @@ 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 } ] } ) ;
134
137
let user ;
135
138
if ( existingUser ) {
136
- user = existingUser ;
139
+ // update social network type
140
+ existingUser . socialNetworkType = auth . sub . substring ( 0 , auth . sub . indexOf ( '|' ) ) ;
141
+ user = yield existingUser . save ( ) ;
137
142
} else {
143
+ entity . socialNetworkId = auth . sub ;
144
+ entity . socialNetworkType = auth . sub . substring ( 0 , auth . sub . indexOf ( '|' ) ) ;
138
145
entity . role = Role . CONSUMER ;
139
146
user = yield User . create ( entity ) ;
140
147
}
@@ -211,24 +218,26 @@ forgotPassword.schema = {
211
218
*/
212
219
function * forgotPassword ( entity ) {
213
220
const code = Math . floor ( Math . random ( ) * 100000 ) . toString ( 16 ) ;
214
- // print out code for debug purpose
215
- logger . debug ( `reset password code is ${ 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
- `The verify code is ${ code } \n -- example.com` ;
221
+ const subject = config . RESET_PASSWORD_SUBJECT ;
222
+ const link = config . RESET_PASSWORD_LINK . replace ( ':token' , code ) ;
223
+ const text = config . RESET_PASSWORD_TEMPLATE . replace ( ':link' , link ) ;
219
224
const html = `<p>${ text } </p>` ;
220
225
221
226
const user = yield User . findOne ( { email : entity . email } ) ;
222
227
if ( ! user ) {
223
228
throw new errors . NotFoundError ( 'user not found with the specified email' ) ;
224
229
}
230
+ // check if the user is social network user, and if yes than don't allow forgot password
231
+ if ( user . socialNetworkId ) {
232
+ throw new errors . ValidationError ( 'social network user cannot reset password' , httpStatus . BAD_REQUEST ) ;
233
+ }
225
234
226
235
user . resetPasswordCode = code ;
227
236
const date = new Date ( ) ;
228
237
user . resetPasswordExpiration = date . setSeconds ( date . getSeconds ( ) + config . RESET_CODE_EXPIRES ) ;
229
238
yield user . save ( ) ;
230
239
231
- yield MailService . sendMessage ( user . email , html , text ) ;
240
+ yield MailService . sendMessage ( user . email , html , text , subject ) ;
232
241
}
233
242
234
243
// the joi schema for resetPassword
@@ -254,6 +263,10 @@ function* resetPassword(entity) {
254
263
user . resetPasswordExpiration . getTime ( ) - new Date ( ) . getTime ( ) < 0 ) {
255
264
throw new errors . HttpStatusError ( 400 , 'invalid code' ) ;
256
265
}
266
+ // check if the user is social network user, and if yes than don't allow forgot password
267
+ if ( user . socialNetworkId ) {
268
+ throw new errors . ValidationError ( 'social network user cannot reset password' , httpStatus . BAD_REQUEST ) ;
269
+ }
257
270
258
271
user . password = yield helper . hashString ( entity . password , config . SALT_WORK_FACTOR ) ;
259
272
user . resetPasswordCode = null ;
0 commit comments