API Update: Additions to wix-users-backend

We are excited to announce the following two additions to the wix-users-backend API.

emailUser( )
The backend emailUser( ) function can be used to send a triggered email to any of your site’s members. It works just like the frontend emailUser( ), without the restriction of only being able to send emails to the currently logged in user.

To learn more, see emailUser( ) in the API reference.


generateSessionToken( )
The generateSessionToken( ) function is used to bypass Wix member authentication so that you can use a 3rd party service to authenticate your users. That means you can provide Single Sign-On (SSO) for users where they are authenticated by a non-Wix entity to login to your Wix site.

To learn more, see generateSessionToken( ) in the API reference.

2 Likes

Sam, this might be what I need to have my users login ONLY with Google. Just 2 things:

  1. in the API-doc example code there´s this:
import {authBy3rdParty} from 'backend/authentications';

But I cannot find this backend function anywhere. What did I miss?

2)does this SSO REPLACE the Wix interface (the Wix login dialog) when a page is requested that is restricted (e.g. because Wix looks for a backend file called “login.jsw”), or is it just to make your own page, but, if a user somehow accesses a resticted page WITHOUT having logged on using a custom login page, the standard Wix login dialog is still displayed?

Your link emailUser( ) directs to an example where everything is clear, but the most important thing is missing.

let userId = // get user ID

How to get the user ID, if it is not the current user? I have tried the _owner field, but the email function does not work.
wixUsers.emailUser(“emailID”, user._owner)
Any idea how to get the user ID of a non logged in member?

@yisrael-wix Can anyone shed some light on this, it’s exactly what i’m looking for and am struggling to implement this. Is there a working example with PETER’s question about the user ID? thank you

Hi Stephen,
Finally this turned to be working:
wixUsers.emailUser(“emailID”,user._owner)
Peter

Hi @Peter, that’s great news!
I wondered if you would be able to provide the whole code as I still seem to be struggling… I have this as part of some code on a repeater, but it doesn’t seem to work

import wixUsersBackend from 'wix-users-backend';


$w("#sendReminderButton").onClick((event) => {
 const userId = itemData._owner
 let value1 = $w("#customerNameText").text;
 let value2 = $w("#courseTitleText").text;
 let value3 = $w("#totalPriceText").text;
 let value4 = $w("#amountDueText").value;
 let value5 = $w("#dateText").text;
                        wixUsers.emailUser("outstandingBalanceTemplate", userId, {
 "variables": {
 "name": value1,
 "courseTitle": value2,
 "totalPrice": value3,
 "outstandingBalance": value4,
 "paymentDate": value5,
                                }
                            })
                            .then(() => {
                                $w('#checkImage').show();
                                console.log("Triggered email sent");
                            })
                            .catch((err) => {
                                console.log(err);
                                $w('#sendReminderButton').label = "Failed!"
                            });
                        console.log(userId)
                        console.log(itemData.fullName)


Thank you!

@stephenmccall
You should check if the “itemData._owner” is a correct value.
Use console.log(itemData._owner); and check if it is really the ID of the user you want to send the email.
Otherwise the wixUsers.emailUser(“emailID”,user._owner) works correct in my code.
Good luck

Thanks Peter, I think it’s all good. Are you using a backend module for this?

Yes, backend

Thanks Peter, I’ve spent way too much time on this and just about have it working on a variable template through Sendgrid, seems as if it should have been so simple but I must have been missing something obvious.

Thanks for your help

Giri, did you ever find a solution for your first question?

@stephenmccall The way I solved it was to create a separate database which stored the userID / contactID for the users.

Calling wixUsers.generateSessionToken(‘someone@example.com’) in backend jsw always return error “Unauthorized to perform this action with context ApiContext”. Does anyone know how to authorize that API call? Thank you!