@@ -48,7 +48,7 @@ export class UserApi extends Api {
4848 */
4949 public async create ( ctx : Context ) {
5050
51- const newUser : UserDocument = assignIn < UserDocument > ( pick ( ctx . request . body , 'username' , 'password' , 'email' ) , {
51+ const postedUser : UserDocument = assignIn < UserDocument > ( pick ( ctx . request . body , 'username' , 'password' , 'email' ) , {
5252 is_local : true ,
5353 name : ctx . request . body . name || ctx . request . body . username ,
5454 } ) ;
@@ -58,43 +58,43 @@ export class UserApi extends Api {
5858 const skipEmailConfirmation = testMode && ctx . request . body . skipEmailConfirmation ;
5959
6060 // TODO make sure newUser.email is sane (comes from user directly)
61- let user = await state . models . User . findOne ( {
61+ const existingUser = await state . models . User . findOne ( {
6262 $or : [
63- { emails : newUser . email } ,
64- { validated_emails : newUser . email } ,
63+ { emails : postedUser . email } ,
64+ { validated_emails : postedUser . email } ,
6565 ] ,
6666 } ) . exec ( ) ;
6767
68- if ( user ) {
69- throw new ApiError ( 'User with email <%s> already exists.' , newUser . email ) . warn ( ) . status ( 409 ) ;
68+ if ( existingUser ) {
69+ throw new ApiError ( 'User with email <%s> already exists.' , postedUser . email ) . warn ( ) . status ( 409 ) ;
7070 }
7171 const confirmUserEmail = config . vpdb . email . confirmUserEmail && ! skipEmailConfirmation ;
72- user = await UserUtil . createUser ( ctx , newUser , confirmUserEmail ) ;
72+ const createdUser = await UserUtil . createUser ( ctx , postedUser , confirmUserEmail ) ;
7373
7474 if ( process . env . SQREEN_ENABLED ) {
75- require ( 'sqreen' ) . signup_track ( { email : user . email } ) ;
75+ require ( 'sqreen' ) . signup_track ( { email : createdUser . email } ) ;
7676 }
7777
78- await LogUserUtil . success ( ctx , user , 'registration' , {
78+ await LogUserUtil . success ( ctx , createdUser , 'registration' , {
7979 provider : 'local' ,
80- email : newUser . email ,
81- username : newUser . username ,
80+ email : createdUser . email ,
81+ username : createdUser . username ,
8282 } ) ;
8383
84- // return result now and send email afterwards
85- if ( testMode && ctx . request . body . returnEmailToken ) {
86- this . success ( ctx , assign ( state . serializers . User . detailed ( ctx , user ) , { email_token : ( user . email_status as any ) . toObject ( ) . token } ) , 201 ) ;
87-
88- } else {
89- this . success ( ctx , state . serializers . User . detailed ( ctx , user ) , 201 ) ;
90- }
91-
9284 this . noAwait ( async ( ) => {
9385 // user validated and created. time to send the activation email.
9486 if ( config . vpdb . email . confirmUserEmail ) {
95- await mailer . registrationConfirmation ( ctx . state , user ) ;
87+ await mailer . registrationConfirmation ( ctx . state , createdUser ) ;
9688 }
9789 } ) ;
90+
91+ // return result
92+ if ( testMode && ctx . request . body . returnEmailToken ) {
93+ this . success ( ctx , assign ( state . serializers . User . detailed ( ctx , createdUser ) , { email_token : ( createdUser . email_status as any ) . toObject ( ) . token } ) , 201 ) ;
94+
95+ } else {
96+ this . success ( ctx , state . serializers . User . detailed ( ctx , createdUser ) , 201 ) ;
97+ }
9898 }
9999
100100 /**
0 commit comments