Hey, I have an external application (forum website) which uses an external database for the threads, users, etc… and a Wix website which has a user login system for authentication. I would like the sync between the users table, or just use the Wix backend for login/register and maintain all the user’s data in the Wix backend. Is it feasible? Do you have any suggestions/hints on accomplishing this task?
You can expose your site’s API using Wix HTTP Functions & use the Wix Users Backend login() function to authenticate login from external platforms.
Wix Users Backend: https://www.wix.com/corvid/reference/wix-users-backend/login
HTTP Functions: https://www.wix.com/corvid/reference/wix-http-functions#top
But how do you get the user’s information? I just get a jwt token
@barvhaim You can query the PrivateMembersData collection from your backend using the login email.
@shantanukumar847
Also interested in this. Can you give an example, how to do that? Or give an good article about this process?
@russian-dima Hey, So this is how I would do it:
Scenario: Register + Authenticate Logins with Wix Users Backend.
First thing you need to do is create an endpoint for your site on your http-functions.js file.
//http-functions.js
import {response} from 'wix-http-functions';
import {registerFnc} from 'backend/registration.jsw';
export function post_userRegister(request) {
let start = Date.now();
return request.body.json()
.then( (body) => {
return registerFnc(body)
.then( (res) => {
/*Return Response*/
let end = Date.now();
let latency = end - Number(start);
let options = {
status: 200,
headers: {
"Content-Type": "application/json"
},
body: {
"latency": Number(latency),
"user_info": res
}
};
return response(options);
})
.catch( (error) => {
/*Return Response*/
let end = Date.now();
let latency = end - Number(start);
let options = {
status: 400,
headers: {
"Content-Type": "application/json"
},
body: {
"error_type": "Bad Request",
"error_message": {
"reason": error,
"url": "https://dev.mysite.io/documentation/error-codes?status=400"
},
"latency": latency
}
}
return response(options);
});
})
.catch( (error) => {
let end = Date.now();
let latency = end - Number(start);
/*Return Response*/
let options = {
status: 500,
headers: {
"Content-Type": "application/json"
},
body: {
"error_type": "Server Outage",
"error_message": {
"reason": "Our servers are currently unavailable, please try again later",
"url": "https://dev.mysite.io/documentation/error-codes?status=500"
},
"latency": latency
}
}
return response(options);
});
}
After this you need to create a registration function in a registration.jsw file.
//registration.jsw
import wixUsersBackend from 'wix-users-backend';
export function registerFnc(body) {
return wixUsersBackend.register(body.email, body.password, {
"contactInfo": {
"firstName": body.firstName,
"lastName": body.lastName
}
})
.then( (result) => {
return result;
})
.catch( (err) => {
return err;
});
}
The above code will allow you to register your users on your wix site from an external platform. Now if you want to authenticate logins you need to add the below function to your http-functions.js file
//Add this to http-functions.js
export function post_userAuth(request) {
let start = Date.now();
return request.body.json()
.then( (body) => {
return loginFnc(body)
.then( (res) => {
/*Return Response*/
let end = Date.now();
let latency = end - Number(start);
let options = {
status: 200,
headers: {
"Content-Type": "application/json"
},
body: {
"latency": Number(latency),
"user_info": res
}
};
return response(options);
})
.catch( (error) => {
/*Return Response*/
let end = Date.now();
let latency = end - Number(start);
let options = {
status: 400,
headers: {
"Content-Type": "application/json"
},
body: {
"error_type": "Bad Request",
"error_message": {
"reason": error,
"url": "https://dev.mysite.io/documentation/error-codes?status=400"
},
"latency": latency
}
}
return response(options);
});
})
.catch( (error) => {
let end = Date.now();
let latency = end - Number(start);
/*Return Response*/
let options = {
status: 500,
headers: {
"Content-Type": "application/json"
},
body: {
"error_type": "Server Outage",
"error_message": {
"reason": "Our servers are currently unavailable, please try again later",
"url": "https://dev.mysite.io/documentation/error-codes?status=500"
},
"latency": latency
}
}
return response(options);
});
}
Now add the below function to your registration.jsw file
//Add to registration.jsw
export function loginFnc(body) {
return wixUsersBackend.login(body.email, body.password)
.then( (result) => {
return result;
})
.catch( (err) => {
return err;
});
}
Note that the above code only logs in the user and return the token. If you want to query the Private Member Collection replace the above loginFnc() function with the one given below.
export function loginFnc(body) {
return wixUsersBackend.login(body.email, body.password)
.then( (result) => {
return wixData.query('Members/PrivateMembersData').eq('loginEmail', body.email).find(options).then( (res) => {
let Item = res.items[0];
let object = {
'db_info': Item,
'token_container': result
};
return object;
})
})
.catch( (err) => {
return err;
});
}
Remember to import the wix data module and set the options variable for the query.
import wixData from 'wix-data';
let options = {
"suppressAuth": true,
"suppressHooks": true
};
@shantanukumar847
Ufffff, much bigger then i thought
Thanks shan! I will take a closer look at it.
Will see what i can learn.
@shantanukumar847 thanks
@shantanukumar847 Thanks for this example. Is this the right approach for the following scenario?:
- Client registers for an event at a 3rd party site (Cvent or similar)
- Client registration details passed to Zapier as json
- Zapier webhook sends http response request to my Wix site (or should this be a post request?)
- Wix site takes json from webhook and then registers the user in the wix-user-backend
- Client can then log in to the Wix site with the same info they registered with at the 3rd party site.