@@ -76,6 +76,7 @@ register.schema = {
7676
7777// the joi schema for register user via social login
7878registerSocialUser . 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,22 @@ 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 } ] } ) ;
134137 let user ;
135138 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 ( ) ;
137142 } else {
143+ entity . socialNetworkId = auth . sub ;
144+ entity . socialNetworkType = auth . sub . substring ( 0 , auth . sub . indexOf ( '|' ) ) ;
138145 entity . role = Role . CONSUMER ;
139146 user = yield User . create ( entity ) ;
140147 }
@@ -211,24 +218,26 @@ forgotPassword.schema = {
211218 */
212219function * forgotPassword ( entity ) {
213220 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 ) ;
219224 const html = `<p>${ text } </p>` ;
220225
221226 const user = yield User . findOne ( { email : entity . email } ) ;
222227 if ( ! user ) {
223228 throw new errors . NotFoundError ( 'user not found with the specified email' ) ;
224229 }
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+ }
225234
226235 user . resetPasswordCode = code ;
227236 const date = new Date ( ) ;
228237 user . resetPasswordExpiration = date . setSeconds ( date . getSeconds ( ) + config . RESET_CODE_EXPIRES ) ;
229238 yield user . save ( ) ;
230239
231- yield MailService . sendMessage ( user . email , html , text ) ;
240+ yield MailService . sendMessage ( user . email , html , text , subject ) ;
232241}
233242
234243// the joi schema for resetPassword
@@ -254,6 +263,10 @@ function* resetPassword(entity) {
254263 user . resetPasswordExpiration . getTime ( ) - new Date ( ) . getTime ( ) < 0 ) {
255264 throw new errors . HttpStatusError ( 400 , 'invalid code' ) ;
256265 }
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+ }
257270
258271 user . password = yield helper . hashString ( entity . password , config . SALT_WORK_FACTOR ) ;
259272 user . resetPasswordCode = null ;
0 commit comments