wix-users-backend

wix-users-backend

The wix-users-backend module contains functionality for working with your site's users from backend code.

There are three types of users:

  • Visitor - A user who is not logged into your site.
  • Member - A user who is logged into your site.
  • Admin - The owner of the site.

To use the Users API, import wixUsers from the wix-users-backend module:

ICAgaW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycy1iYWNrZW5kJzs=
   import wixUsers from 'wix-users-backend';

Contents

currentUser Gets the current user viewing the site.
approveByEmail( ) Approve a member whose status is "Pending" using an email address.
approveByToken( ) Approve a member whose status is "Pending" using an approval token.
blockByEmail( ) Blocks a member from logging into the site.
register( ) Registers a new site member.
RegistrationResult An object that contains information about the results of a site registration.
currentUser

currentUser

Gets the current user viewing the site.

Syntax

get currentUser(): User

Description

Gets a User object containing information about the user currently viewing the site.

Type

User

Examples

Get the current user's information

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKCnVzZXIuZ2V0RW1haWwoKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAvLyAidXNlckBzb21ldGhpbmcuY29tIgogIH0gKTsKCnVzZXIuZ2V0Um9sZXMoKQogIC50aGVuKCAocm9sZXMpID0+IHsKICAgIGxldCBmaXJzdFJvbGUgPSByb2xlc1swXTsKICAgIGxldCByb2xlTmFtZSA9IGZpcnN0Um9sZS5uYW1lOyAgICAgICAgICAgICAgICAvLyAiUm9sZSBOYW1lIgogICAgbGV0IHJvbGVEZXNjcmlwdGlvbiA9IGZpcnN0Um9sZS5kZXNjcmlwdGlvbjsgIC8vICJSb2xlIERlc2NyaXB0aW9uIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );
approveByEmail( )

approveByEmail( )

Approve a member whose status is "Pending" using an email address.

function approveByEmail(email: String): Promise<String>

Description

The approveByEmail() function returns a Promise that resolves to a session token when the specified user is approved.

A member's status is "Pending" after you've called the register() function when the site's membership policy is set to manual approval. To learn more about setting you site's membership approval policy, see Editing Your Member Signup Settings.

Pass the member's email address in the email parameter.

Parameters

email String Email address of the user to approve.

Return Value

Returns a Promise

On fulfillment String A session token for logging in the approved user in client-side code.
On rejection String Error message.

Examples

Approve a pending member using an email address

This example contains a backend function which approves a pending user using an email address and returns a session token to be used in client-side code to log in the user who was just approved.

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycy1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBteUJhY2tlbmRGdW5jdGlvbihlbWFpbCkgewogIHdpeFVzZXJzLmFwcHJvdmVCeUVtYWlsKGVtYWlsKQogICAgLnRoZW4oIChzZXNzaW9uVG9rZW4pID0+IHsKICAgICAgcmV0dXJuIHtzZXNzaW9uVG9rZW4sICJhcHByb3ZlZCI6IHRydWV9OwogICAgfSApOwogICAgLmNhdGNoKCAoZXJyb3IpID0+IHsKICAgICAgcmV0dXJuIHsiYXBwcm92ZWQiOiBmYWxzZSwgInJlYXNvbiI6IGVycm9yfTsKICAgIH0gKTsKICB9Cn0K
import wixUsers from 'wix-users-backend';

export function myBackendFunction(email) {
  wixUsers.approveByEmail(email)
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } );
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
  }
}
approveByToken( )

approveByToken( )

Approve a member whose status is "Pending" using an approval token.

function approveByToken(token: String): Promise<String>

Description

The approveByToken() function returns a Promise that resolves to a session token when the specified user is approved.

A member's status is "Pending" after you've called the register() function when the site's membership policy is set to manual approval. To learn more about setting you site's membership approval policy, see Editing Your Member Signup Settings.

Use the approvalToken parameter of the RegistrationResult object returned from the register() function when calling approveByToken.

Parameters

token String Approval token returned by the register() function.

Return Value

Returns a Promise

On fulfillment String A session token for logging in the approved user in client-side code.
On rejection String Error message.

Examples

Approve a pending member using an approval token

This example contains a backend function which approves a pending user using an approval token and returns a session token to be used in client-side code to log in the user who was just approved.

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycy1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBteUJhY2tlbmRGdW5jdGlvbih0b2tlbikgewogIHdpeFVzZXJzLmFwcHJvdmVCeVRva2VuKHRva2VuKQogICAgLnRoZW4oIChzZXNzaW9uVG9rZW4pID0+IHsKICAgICAgcmV0dXJuIHtzZXNzaW9uVG9rZW4sICJhcHByb3ZlZCI6IHRydWV9OwogICAgfSApOwogICAgLmNhdGNoKCAoZXJyb3IpID0+IHsKICAgICAgcmV0dXJuIHsiYXBwcm92ZWQiOiBmYWxzZSwgInJlYXNvbiI6IGVycm9yfTsKICAgIH0gKTsKICB9Cn0K
import wixUsers from 'wix-users-backend';

export function myBackendFunction(token) {
  wixUsers.approveByToken(token)
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } );
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
  }
}

Register a user using a 3rd party for approval

This example demonstrates a common 3rd party approval flow. The backend code calls a 3rd party function that determines whether the user will be approved or not. If approved, the register() function is called, the registration is approved programmatically using the approveByToken() function, and a session token is returned to the calling client-side code. If rejected, the blockByEmail() function is called.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogYmFja2VuZCBjb2RlIC0gcmVnaXN0ZXIuanN3ICoKICoqKioqKioqKioqKioqK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VuKTsKICAgIGVsc2UgewogICAgICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgfQogIH0gKTsK
/*******************************
 * backend code - register.jsw *
 *******************************/
import wixUsers from 'wix-users-backend';
import wixCRM from 'wix-crm-backend';

export function doRegistration(email, password, firstName, lastName) {
  // call a 3rd party API to check if the user is approved
  return approveBy3rdParty(email, password)
    .then( (isApproved) => {
      // if approved by 3rd party
      if (isApproved) {
        // register the user
        return wixUsers.register(email, password, {
          contactInfo: {
            "firstName": firstName,
            "lastName": lastName
          }
        } )
          // user is now registered and pending approval
          // approve the user
          .then( (result) => wixUsers.approveByToken(result.approvalToken) )
          // user is now active, but not logged in
          // return the session token to log in the user client-side
          .then( (sessionToken) => {
            return {sessionToken, "approved": true};
          } );
      }
      // if not approved by 3rd party
      else {
        // block the user
        return wixUsers.blockByEmail(email)
          .then( () => {
            return {"approved": false};
          });
      }
    } )
}

/********************
 * client-side code *
 ********************/
import wixUsers from 'wix-users';
import {doRegistration} from 'backend/register';

// ...

let email = // the user's email addresses
let password = // the user's password
let firstName = // the user's first name
let lastName = // the user's last name

doRegistration(email, password, firstName, lastName)
  .then( (result) => {
    if (result.approved)
      // log the user in
      wixUsers.applySessionToken(result.sessionToken);
    else {
      console.log("Not approved!");
    }
  } );

Register a user sending an email for confirmation

This example demonstrates a common email verification flow. A user is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When a user goes to the verification page, the approval is granted using the approveByToken() function and the user is logged into the site.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogYmFja2VuZCBjb2RlIC0gcmVnaXN0ZXIuanN3ICoKICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCmltcG9ydCB3aXhVc2VycyBmcm9tICd3aXgtdXNlcnMtYmFja2VuZCc7CmltcG9ydCB3aXhDUk0gZnJvbSAnd2l4LWNybS1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBkb1JlZ2lzdHJhdGlvbihlbWFp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va2VuIGZyb20gdGhlIFVSTAogIGxldCB0b2tlbiA9IHdpeExvY2F0aW9uLnF1ZXJ5LnRva2VuOwoKICBkb0FwcG92YWwodG9rZW4pCiAgICAudGhlbiggKHJlc3VsdCkgPT4gewogICAgICBpZiAocmVzdWx0LmFwcHJvdmVkKQogICAgICAgIC8vIGxvZyB0aGUgdXNlciBpbgogICAgICAgIHdpeFVzZXJzLmFwcGx5U2Vzc2lvblRva2VuKHJlc3VsdC5zZXNzaW9uVG9rZW4pOwogICAgICBlbHNlIHsKICAgICAgICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgICB9CiAgICB9ICk7Cn0K
/*******************************
 * backend code - register.jsw *
 *******************************/
import wixUsers from 'wix-users-backend';
import wixCRM from 'wix-crm-backend';

export function doRegistration(email, password, firstName, lastName) {
  // register the user
  return wixUsers.register(email, password, {
    contactInfo: {
      "firstName": firstName,
      "lastName": lastName
    }
  } )
    .then( (results) => {
      // user is now registered and pending approval
      // send a registration verification email
      return wixUsers.emailUser('verify-registration', results.user.id,{
        "variables": {
          "name": firstName,
          "verifyLink": `http://domain.com/post-register?token=${approvalToken}`
        }
      } );
    } );
}

export function doApproval(token) {
  // approve the user
  return wixCRM.approveByToken(token)
    // user is now active, but not logged in
    // return the session token to log in the user client-side
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } );
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
}

/*********************************
 * client-side registration code *
 *********************************/
import wixUsers from 'wix-users';
import {doRegistration} from 'backend/register';

// ...

let email = // the user's email addresses
let password = // the user's password
let firstName = // the user's first name
let lastName = // the user's last name

doRegistration(email, password, firstName, lastName)
  .then( () => {
    console.log("Confirmation email sent.");
  } );

/**************************************
 * client-side post-registration code *
 **************************************/
import wixLocation from 'wix-location';
import {doApproval} from 'backend/register';

$w.onReady( () => {
  // get the token from the URL
  let token = wixLocation.query.token;

  doAppoval(token)
    .then( (result) => {
      if (result.approved)
        // log the user in
        wixUsers.applySessionToken(result.sessionToken);
      else {
        console.log("Not approved!");
      }
    } );
}
blockByEmail( )

blockByEmail( )

Blocks a member from logging into the site.

function blockByEmail(email: string): Promise<void>

Description

The blockByEmail() function returns a Promise that resolves when the specified user has been blocked.

Pass the member's email address in the email parameter.

Parameters

email String Email address of the user to approve.

Return Value

Returns a Promise

On fulfillment void When specified member is blocked.
On rejection String Error message.

Examples

Bloc a member using an email address

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycy1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBteUJhY2tlbmRGdW5jdGlvbihlbWFpbCkgewogIHdpeFVzZXJzLmJsb2NrQnlFbWFpbChlbWFpbCkKICAgIC50aGVuKCAoKSA9PiB7CiAgICAgIC8vIHVzZXIgaGFzIGJlZW4gYmxvY2tlZAogICAgfSApOwogIH0KfQo=
import wixUsers from 'wix-users-backend';

export function myBackendFunction(email) {
  wixUsers.blockByEmail(email)
    .then( () => {
      // user has been blocked
    } );
  }
}
register( )

register( )

Registers a new site member.

function register(email: String, password: String, [options: RegistrationOptions]): Promise<RegistrationResult>

Description

The register() function returns a Promise that resolves to a RegistrationResult object when the user is either registered or is pending registration.

If Site Members is configured for automatic approval, the register() function returns a status of "Active" and the user will be an active member of the site.

If Site Members is configured for manual approval, the register() function returns a status of "Pending" and the user will be a pending member of the site. In order to activate a pending user, the site owner can approve the user manually using the Contacts application or you can call the approveByToken() or approveByEmail() functions.

The sessionToken returned when the new member is "Active" must be applied using the applySessionToken() function in client-side code to complete the user registration by logging in the registered user.

Parameters

email String The email address to use when registering the user as a site member.
password String The password the newly added site member will use to log in.
options (optional) RegistrationOptions Registration options.

Return Value

Returns a Promise

On fulfillment RegistrationResult When the user has been registered.
On rejection String Error message.

Examples

Register a user using a 3rd party for approval

This example demonstrates a common 3rd party approval flow. The backend code calls a 3rd party function that determines whether the user will be approved or not. If approved, the register() function is called, the registration is approved programmatically, and a session token is returned to the calling client-side code. If rejected, the blockByEmail() function is called.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogYmFja2VuZCBjb2RlIC0gcmVnaXN0ZXIuanN3ICoKICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCmltcG9ydCB3aXhVc2VycyBmcm9tICd3aXgtdXNlcnMtYmFja2VuZCc7CmltcG9ydCB3aXhDUk0gZnJvbSAnd2l4LWNybS1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBkb1JlZ2lzdHJhdGlvbihlbWFpbCwgcGFzc3dvcmQsIGZpcnN0TmFtZSwgbGFzdE5hbWUpIHsKICAvLyBjYWxsIGEgM3JkIHBhcnR5IEFQSSB0byBjaGVjayBpZiB0aGUgdXNlciBpcyBhcHByb3ZlZAogIHJldHVybiBhcHByb3ZlQnkzcmRQYXJ0eShlbWFpbCwgcGFzc3dvcmQpCiAgICAudGhlbiggKGlzQXBwcm92ZWQpID0+IHsKICAgICAgLy8gaWYgYXBwcm92ZWQgYnkgM3JkIHBhcnR5CiAgICAgIGlmIChpc0FwcHJvdmVkKSB7CiAgICAgICAgLy8gcmVnaXN0ZXIgdGhlIHVzZXIKICAgICAgICByZXR1cm4gd2l4VXNlcnMucmVnaXN0ZXIoZW1haWwsIHBhc3N3b3JkLCB7CiAgICAgICAgICBjb250YWN0SW5mbzogewogICAgICAgICAgICAiZmlyc3ROYW1lIjogZmlyc3ROYW1lLAogICAgICAgICAgICAibGFzdE5hbWUiOiBsYXN0TmFtZQogICAgICAgICAgfQogICAgICAgIH0gKQogICAgICAgICAgLy8gdXNlciBpcyBub3cgcmVnaXN0ZXJlZCBhbmQgcGVuZGluZyBhcHByb3ZhbAogICAgICAgICAgLy8gYXBwcm92ZSB0aGUgdXNlcgogICAgICAgICAgLnRoZW4oIChyZXN1bHQpID0+IHdpeFVzZXJzLmFwcHJvdmVCeVRva2VuKHJlc3VsdC5hcHByb3ZhbFRva2VuKSApCiAgICAgICAgICAvLyB1c2VyIGlzIG5vdyBhY3RpdmUsIGJ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CiAgICAgIC8vIGxvZyB0aGUgdXNlciBpbgogICAgICB3aXhVc2Vycy5hcHBseVNlc3Npb25Ub2tlbihyZXN1bHQuc2Vzc2lvblRva2VuKTsKICAgIGVsc2UgewogICAgICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgfQogIH0gKTsK
/*******************************
 * backend code - register.jsw *
 *******************************/
import wixUsers from 'wix-users-backend';
import wixCRM from 'wix-crm-backend';

export function doRegistration(email, password, firstName, lastName) {
  // call a 3rd party API to check if the user is approved
  return approveBy3rdParty(email, password)
    .then( (isApproved) => {
      // if approved by 3rd party
      if (isApproved) {
        // register the user
        return wixUsers.register(email, password, {
          contactInfo: {
            "firstName": firstName,
            "lastName": lastName
          }
        } )
          // user is now registered and pending approval
          // approve the user
          .then( (result) => wixUsers.approveByToken(result.approvalToken) )
          // user is now active, but not logged in
          // return the session token to log in the user client-side
          .then( (sessionToken) => {
            return {sessionToken, "approved": true};
          } );
      }
      // if not approved by 3rd party
      else {
        // block the user
        return wixUsers.blockByEmail(email)
          .then( () => {
            return {"approved": false};
          });
      }
    } )
}

/********************
 * client-side code *
 ********************/
import wixUsers from 'wix-users';
import {doRegistration} from 'backend/register';

// ...

let email = // the user's email addresses
let password = // the user's password
let firstName = // the user's first name
let lastName = // the user's last name

doRegistration(email, password, firstName, lastName)
  .then( (result) => {
    if (result.approved)
      // log the user in
      wixUsers.applySessionToken(result.sessionToken);
    else {
      console.log("Not approved!");
    }
  } );

Register a user sending an email for confirmation

This example demonstrates a common email verification flow. A user is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When a user goes to the verification page, the approval is granted and the user is logged into the site.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogYmFja2VuZCBjb2RlIC0gcmVnaXN0ZXIuanN3ICoKICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCmltcG9ydCB3aXhVc2VycyBmcm9tICd3aXgtdXNlcnMtYmFja2VuZCc7CmltcG9ydCB3aX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dmFsfSBmcm9tICdiYWNrZW5kL3JlZ2lzdGVyJzsKCiR3Lm9uUmVhZHkoICgpID0+IHsKICAvLyBnZXQgdGhlIHRva2VuIGZyb20gdGhlIFVSTAogIGxldCB0b2tlbiA9IHdpeExvY2F0aW9uLnF1ZXJ5LnRva2VuOwoKICBkb0FwcG92YWwodG9rZW4pCiAgICAudGhlbiggKHJlc3VsdCkgPT4gewogICAgICBpZiAocmVzdWx0LmFwcHJvdmVkKQogICAgICAgIC8vIGxvZyB0aGUgdXNlciBpbgogICAgICAgIHdpeFVzZXJzLmFwcGx5U2Vzc2lvblRva2VuKHJlc3VsdC5zZXNzaW9uVG9rZW4pOwogICAgICBlbHNlIHsKICAgICAgICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgICB9CiAgICB9ICk7Cn0K
/*******************************
 * backend code - register.jsw *
 *******************************/
import wixUsers from 'wix-users-backend';
import wixCRM from 'wix-crm-backend';

export function doRegistration(email, password, firstName, lastName) {
  // register the user
  return wixUsers.register(email, password, {
    contactInfo: {
      "firstName": firstName,
      "lastName": lastName
    }
  } )
    .then( (results) => {
      // user is now registered and pending approval
      // send a registration verification email
      return wixUsers.emailUser('verify-registration', results.user.id,{
        "variables": {
          "name": firstName,
          "verifyLink": `http://domain.com/post-register?token=${approvalToken}`
        }
      } );
    } );
}

export function doApproval(token) {
  // approve the user
  return wixCRM.approveByToken(token)
    // user is now active, but not logged in
    // return the session token to log in the user client-side
    .then( (sessionToken) => {
      return {sessionToken, "approved": true};
    } );
    .catch( (error) => {
      return {"approved": false, "reason": error};
    } );
}

/*********************************
 * client-side registration code *
 *********************************/
import wixUsers from 'wix-users';
import {doRegistration} from 'backend/register';

// ...

let email = // the user's email addresses
let password = // the user's password
let firstName = // the user's first name
let lastName = // the user's last name

doRegistration(email, password, firstName, lastName)
  .then( () => {
    console.log("Confirmation email sent.");
  } );

/**************************************
 * client-side post-registration code *
 **************************************/
import wixLocation from 'wix-location';
import {doApproval} from 'backend/register';

$w.onReady( () => {
  // get the token from the URL
  let token = wixLocation.query.token;

  doAppoval(token)
    .then( (result) => {
      if (result.approved)
        // log the user in
        wixUsers.applySessionToken(result.sessionToken);
      else {
        console.log("Not approved!");
      }
    } );
}
RegistrationResult

RegistrationResult

An object that contains information about the results of a site registration.

Type

Object

Properties

status String Registration status. Either "Pending" or "Active".
sessionToken (optional) String A token used to log in the current user as a site member. Pass the token from your backend code to client-side code and use it when calling the applySessionToken() function. The token is only available when status is "Active".
approvalToken (optional) String A token for approving the user as a site member using the approveByToken() function. The token is safe to pass via email or from client-side code to backend code. The token is only available when status is "Pending".
user User The user that has been registered.