Register user with json, http-functions and wix-users-backend

Hello,

I am trying to register a user with credentials piped in from an external mobile app using json and wix-http-functions. The website and mobile app need to share user information, so that the two are virtually seamless. When a user registers on the mobile app, the app will send the website data and register the user on the website as well.

Here is the code I currently have, however my contacts and such are not being updated.

-Backend-
import { serverError, created } from ‘wix-http-functions’;
import wixUsers from ‘wix-users-backend’;

export function post_registerNewUser(newUser) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
}
return newUser.body.text()
.then((body) => {
console.log(body);
const email = newUser.email;
const password = newUser.password;
const firstName = newUser.firstName;
const lastName = newUser.lastName;
const phone = newUser.phone;
const customerID = newUser.customerNumber;

wixUsers.register(email, password, {
contactInfo: {
“firstName”: firstName,
“lastName”: lastName,
“phone”: phone,
“customerNumber”: customerNumber
}
})
wixUsers.register(body)
.then((result) => {
let resultStatus = result.status;
return created(resultStatus);
})
. catch ((error) => {
options.body = {
“error”: error
};
return serverError(options);
})
})
}

Any help here would be greatly appreciated,

James

Hello James,

If you want to update a contact use the createContact() method found here. Syntax-wise the above code looks fine. Let me know if this is what you wanted or if it still does not work.

Goodluck,
Majd

Thank you for your reply Majd. I will be trying your suggestion. Just out of curiosity, what is the difference between a contact and a user?

@jamesschulze This article will explain it better: Contacts vs. Subscribers | Help Center | Wix.com

@mqumseya I am trying to send an email address, password and some additional information to my site’s backend using postman…
[
{
“newUser”: {
“email”: “test@fakemail.com”,
“password”: “password”,
“contactInfo”: {
“firstName”: “John”,
“lastName”: “Doe”,
“phone”: “1234567890”,
“customerNumber”: “000001”
}
}
}
]
I am getting a 500 error in response. I have waited and tried again, as suggested by one of the wix articles. Is this really a wix server error or am I not doing something right?

@jamesschulze It is not necessarily a problem with wix servers, it is most likely an authentication/verification issue or the url/function is misspelled.

Can I get the url to your site so that I can do some testing?

Thanks
Majd

@jamesschulze As @mqumseya mention, it’s not necessarily a problem with Wix servers, in most of the cases it’s not (probably, they will fix the problem before you will notice).

In most of the cases, the 500 error response it because of unhandled error in your code. When an error is thrown, the code is never getting to the return statement so the server returns nothing.
To send the error to the client, wrap your code with try / catch and in the catch part, send a response with the error message to the client.

Also, I know you used **import** wixUsers **from** 'wix-users-backend'; but please double check it because if you import from `wix-users’ the an error will be thrown.

Hi James,

Did you manage to register a user with http function?
I’m facing the same issue recently and can’t get it done.
I recieve 200 from the server but the register() function does nothing.

Hope you succeded and will be able to give me some guidelines.

Thanks

The code is good. I think the only issue here is instead of .text() in the below line

return newUser.body.text() 

You should be using .json() like below

return newUser.body.json() 

It is due to the .text() that the values being passed to the wixUsers.register function comes up as null.

I just ran into the same problem and after a couple of hours of testing I managed to figure out the issue.

@mqumseya how would you solve the follow-on login() problem in this setup? Both the login() and the register() return a “sessionToken” that typically need to be applied in the frontend using wixUsers.applySessionToken() method, however on the mobile side this can obviously not be done. How can you log that user in that was created previously using the http-functions method?

I am having an issue with registering a user. We create new users when our customer orders one of our products from an external site. So I am creating a register function to register the user account. When I execute the code below I get a 500 error

export function post_registeruser(request) {

return request.body.json()
.then((body) => {

const email = body.email;
const password = body.password;
const firstName = body.firstName;

const lastName = body.lastName;
const phone = body.phone;

return wixUsersBackend.register(email, password, {
contactInfo: {
“firstName” : firstName,
“lastName” : lastName,
}
})
.then((result) => {
let resultStatus = result.status;
return created(resultStatus);
})
. catch ((error) => {
let options = {
“headers” : {
“Content-Type” : “application/json”
},
“body” :{
“error” : error
}
}
return serverError(options);
})
})
}