Set userStatus to EMAIL_CONFIRMATION_PENDING

Hey, I’m working on update email structure, what I’ve found that if I use :
user.email=newEmail
Backendless.UserService.update(user);
the user status stays enabled,
How can I set the user status to EMAIL_CONFIRMATION_PENDING so the resendEmailConfirmation work and then validating the new email

Note: I can remove the old user and registering him again and then the status will become EMAIL_CONFIRMATION_PENDING but I’m searching for a better solution

Hi, @adel_kasno

You can change userStatus using business logic with Coderunner API key.

Also, I think you have a better way to solve that issue.
If a user wants to change the email he definitely knows what email he entering, so I don’t know any reason for confirming a new email address. A good choice is confirming action by sending a message to a phone, or enter a password user changes address to new.

You might look at other well-known sites on how it works.

Regards, Dima.

I should confirm it the same reason for confirming the email address when registering

about changing userService any code example?
I found this to disable user status
await Backendless.UserService.disableUser(‘USER_OBJECT_ID’)
but how about setting it to EMAIL_CONFIRMATION_PENDING

Edit:
I do have many options(SMS, email verification code, delete old user and registering him) but if I can set userStatus to EMAIL_CONFIRMATION_PENDING that’s will be the best solution by code in business logic

Hello @adel_kasno,

The problem can be solved by using the following code in afterUpdate event handler:

Backendless.ServerCode.User.afterUpdate(async function(req, res) {
email = (res.result['email']);

//The following code change the status to EMAIL_CONFIRMATION_PENDING
emailConfiramtionUrl = (await Backendless.Request['post']( (function ( url ) {
  if( !url ) {
    throw new Error( 'Url must be specified.' )
  }
  if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
    return 'https://' + url
  }
  return url
})( `http://api.backendless.com/<appId>/<rest-api-key>/users/createEmailConfirmationURL/${email}`).send());


//The following code resend email confirmation
resendEmailConfirmation = (await Backendless.Request['post']( (function ( url ) {
  if( !url ) {
    throw new Error( 'Url must be specified.' )
  }
  if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
    return 'https://' + url
  }
  return url
})( `http://api.backendless.com/<appId>/<rest-api-key>/users/resendconfirmation/${email}` ) ).send());
}

I know that it looks ugly, so I will discuss it with the team how we can improve the sdk

@sergey.kuk
I don’t know how to debug the event handler, and I’m not able to know if the event invokes after invoking await Backendless.UserService.update(user);

I end up with this code:

 Backendless.ServerCode.User.afterUpdate(async function(req, res) {
 email = (res.result['email']);
 //The following code change the status to EMAIL_CONFIRMATION_PENDING
 emailConfiramtionUrl = (await Backendless.Request['post']( (function ( url ) {
 if( !url ) {
   throw new Error( 'Url must be specified.' )
  }
 if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
   return 'https://' + url
  }
 return url
 })( `http://api.backendless.com/<appId>/<rest-api 
 key>/users/createEmailConfirmationURL/${email}`).send()))

//The following code resend email confirmation
resendEmailConfirmation = (await Backendless.Request['post']( (function ( url ) {
  if( !url ) {
   throw new Error( 'Url must be specified.' )
 }
  if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
    return 'https://' + url
  }
  return url
})( `http://api.backendless.com/<appId>/<rest-api-key>/users/resendconfirmation/${email}` ).send()))
 },true)

and it didn’t work, any help?

@adel_kasno please replace <appId> with your application id and <rest-api key> with your rest API key

yeah, in my code I already replaced them
@sergey.kuk

This is the request json for the business logic api service:
{
“firstName”: “ades”,
“lastName”: “kann”,
“bio”: “”,
“birthday”: “2006-09-04T00:00:00”,
“gender”: true,
“user”: “BC9CAEF9-9896-487E-9FEA-C33E59DA1825”,
“email”: “adellkanso@gmail.com
}
and this is the response json of the same api service in business logic:
{
“___jsonclass”: “person”,
“birthday”: 1157328000000,
“lastName”: “kann”,
“gender”: true,
“ownerId”: “BC9CAEF9-9896-487E-9FEA-C33E59DA1825”,
“firstName”: “ades”,
“rateCount”: null,
“phone”: “+96170458013”,
“canceledRides”: 0,
“acomplishedRides”: 0,
“___class”: “person”,
“updated”: 1600786783000,
“objectId”: “B91869A1-AB2A-4190-8BEB-2863356B3F47”,
“email”: “adelkanso1@gmail.com
}
Note:
I’m using Backendless.UserService.update(user) inside this service, and i’m updating the user with the new email

@adel_kasno please provide your application id, I will check your application.

App id 5FB0EA72-A363-4451-FFA5-A56F031D6600

Api service personbusiness/editPerson

@adel_kasno thank you for information. I have checked your application and now I understand why it does not work. The reason is that event handlers does not work if some action triggered from business logic. So to create function:

async resendConfirmation(email){
emailConfiramtionUrl = (await Backendless.Request['post']( (function ( url ) {
  if( !url ) {
    throw new Error( 'Url must be specified.' )
  }
  if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
    return 'https://' + url
  }
  return url
})( `http://api.backendless.com/<app-id>/<api-key>/users/createEmailConfirmationURL/${email}`).send()))
  
//The following code resend email confirmation
resendEmailConfirmation = (await Backendless.Request['post']( (function ( url ) {
  if( !url ) {
    throw new Error( 'Url must be specified.' )
  }
  if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
    return 'https://' + url
  }
  return url
})( `http://api.backendless.com/<app-id>/<api-key>/users/resendconfirmation/${email}` ).send()))
},true)
}

and call it at the end of the editPerson method

error occured: 400 - (intermediate value)(intermediate value)(intermediate value)(…).send is not a function
this my code:

  async resendConfirmation(email){
  emailConfiramtionUrl = (await Backendless.Request['post']( (function ( url ) {
    if( !url ) {
      throw new Error( 'Url must be specified.' ) 
    }
    if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
      return 'https://' + url
    }
    return url
   })( `http://api.backendless.com/<app-id>/<api- 
   key>/users/createEmailConfirmationURL/`+email+``).send()))
    
  //The following code resend email confirmation
  resendEmailConfirmation = (await Backendless.Request['post']( (function ( url ) { 
    if( !url ) {
      throw new Error( 'Url must be specified.' )
    }
    if( !url.startsWith( 'http://' ) && !url.startsWith( 'https://' ) ) {
      return 'https://' + url
    }
    return url
  })( `http://api.backendless.com/<app-id>/<api-key>/users/resendconfirmation/`+email+`` ).send()))
}

And tthat’s how I’m calling it:

this.resendConfirmation(userFound.email);

Hello @adel_kasno

I’ve refactored the method a little:

async resendConfirmation(email){
  const appId = 'YOUR_APP_ID'
  const apiKey = 'YOUR_API_KEY'
  const baseUrl = `http://api.backendless.com/${appId}/${apiKey}`

  const emailConfirmationUrl = await Backendless.Request.post(`${baseUrl}/users/createEmailConfirmationURL/${ email }`)
  const resendEmailConfirmation = await Backendless.Request.post(`${baseUrl}/users/resendconfirmation/${ email }`)
}

could you please try this one

Regards, Vlad

@vladimir-upirov @sergey.kuk Thank you guys.
It works fine, but there is a problem that in my user table, the identity is the phone number I posted a topic for it:


So the verification email not received, code:

    const appId = 'YOUR_API_KEY'
    const apiKey = 'YOUR_API_KEY'
    const baseUrl = `http://api.backendless.com/${appId}/${apiKey}`
    const emailConfirmationUrl = await 
    Backendless.Request.post(`${baseUrl}/users/createEmailConfirmationURL/${ phone }`)
    const resendEmailConfirmation = await 
    Backendless.Request.post(`${baseUrl}/users/resendconfirmation/${ phone }`)

Result: when editing email, the user status become EMAIL_CONFIRMATION_PENDING, the email confirmation not sent to the specific user