Skip to content

Instantly share code, notes, and snippets.

@BryanPinsker
Created January 20, 2022 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryanPinsker/308738bf37ce407b90dfd24974a6ad07 to your computer and use it in GitHub Desktop.
Save BryanPinsker/308738bf37ce407b90dfd24974a6ad07 to your computer and use it in GitHub Desktop.
Widget authentication code examples
window.AnswersWidget.onLoaded(() => {
window.AnswersWidget.addAuthenticationFn(() => {
// This request should include your own method of authentication (e.g. cookie, Authorization header)
return axios.get(`${YOUR_SERVER_URL}/auth-token`).then((response) => response.data);
});
});
// This example uses express and axios
// Add a route like the following to your express routes
app.get('/auth-token', async function(req, res, next) {
const YOUR_ANSWERS_SUBDOMAIN = 'write your answers subdomain';
const dto = {
keyId: 'Your Wix Answers API key',
secret: 'Your Wix Answers API secret',
widgetId: 'your widget id',
// You should verify the users identity and get their email and name using your own method of authentication
email: 'usersemail@example.com',
emailVerified: true, // Boolean, indicate if the authenticated user's email is verified or not.
firstName: 'The users first name', // optional
lastName: 'The users last name', // optional
};
const requestUrl = `https://${YOUR_ANSWERS_SUBDOMAIN}.wixanswers.com/api/v1/widgets/${dto.widgetId}/auth`;
const token = await axios.post(requestUrl, dto).then((res) => {
return res.data.token;
});
res.send(token);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment