wix-users

wix-users

The wix-users module contains functionality for working with your site's users from client-side 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 module:

ICAgaW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7
   import wixUsers from 'wix-users';

Contents

User A site user.
currentUser Gets the current user viewing the site.
applySessionToken( ) Logs the current user into the site using the given session token.
emailUser( ) Sends a Triggered Email to the currently logged-in site member.
logout( ) Logs the current user out of the site.
onLogin( ) Sets the function that runs when a user logs in.
promptForgotPassword( ) Prompts the current site visitor with a password reset.
promptLogin( ) Prompts the current site visitor to log in as a site member.
register( ) Registers a new site member.
LoginOptions An object used by the promptLogin() function to determine how the login dialog box appears.
RegistrationOptions An object that contains information about a site registration.
RegistrationResult An object that contains information about the results of a site registration.
TriggeredEmailOptions An object used when sending a Triggered Email.
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

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VyID0gd2l4VXNlcnMuY3VycmVudFVzZXI7CgpsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNWNtZS02ZmVtLTQ4NWotZGpyZS00ODQ0YzQ5IgpsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKbGV0IHVzZXJSb2xlID0gdXNlci5yb2xlOyAgICAgICAvLyAiTWVtYmVyIgoKdXNlci5nZXRFbWFpbCgpCiAgLnRoZW4oIChlbWFpbCkgPT4gewogICAgbGV0IHVzZXJFbWFpbCA9IGVtYWlsOyAgICAgIC8vICJ1c2VyQHNvbWV0aGluZy5jb20iCiAgfSApOwo=
import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

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

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

applySessionToken( )

Logs the current user into the site using the given session token.

function applySessionToken(sessionToken: String): Promise<void>

Description

The applySessionToken() function returns a Promise that resolves when the given session token is applied and the current user is logged into the site.

You receive a session token from the following functions called from backend code:

Pass the returned session token to your client-side code and apply it by calling applySessionToken() to complete the process started by one of the above functions.

Parameters

sessionToken String The session token to apply.

Return Value

Returns a Promise

On fulfillment void When the token as been applied.

Examples

Log in the current user by applying a session token

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLmFwcGx5U2Vzc2lvblRva2VuKHNlc3Npb25Ub2tlbikKICAudGhlbiggKCkgPT4gewogICAgY29uc29sZS5sb2coIlVzZXIgbG9nZ2VkIGluLiIpOwogIH0gKTsK
import wixUsers from 'wix-users';

// ...

wixUsers.applySessionToken(sessionToken)
  .then( () => {
    console.log("User logged in.");
  } );

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 is 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+IHdpeFVzZXJzLmFwcHJvdmVCeVRva2VuKHJlc3VsdC5hcHByb3ZhbFRva2VuKSApCiAgICAgICAgICAvLyB1c2VyIGlzIG5vdyBhY3RpdmUsIGJ1dCBub3QgbG9nZ2VkIGluCiAgICAgICAgICAvLyByZXR1cm4gdGhlIHNlc3Npb24gdG9rZW4gdG8gbG9nIGluIHRoZSB1c2VyIGNsaWVudC1zaWRlCiAgICAgICAgICAudGhlbiggKHNlc3Npb25Ub2tlbikgPT4gewogICAgICAgICAgICByZXR1cm4ge3Nlc3Npb25Ub2tlbiwgImFwcHJvdmVkIjogdHJ1ZX07CiAgICAgICAgICB9ICk7CiAgICAgIH0KICAgICAgLy8gaWYgbm90IGFwcHJvdmVkIGJ5IDNyZCBwYXJ0eQogICAgICBlbHNlIHsKICAgICAgICAvLyBibG9jayB0aGUgdXNlcgogICAgICAgIHJldHVybiB3aXhVc2Vycy5ibG9ja0J5RW1haWwoZW1haWwpCiAgICAgICAgICAudGhlbiggKCkgPT4gewogICAgICAgICAgICByZXR1cm4geyJhcHByb3ZlZCI6IGZhbHNlfTsKICAgICAgICAgIH0pOwogICAgICB9CiAgICB9ICkKfQoKLyoqKioqKioqKioqKioqKioqKioqCiAqIGNsaWVudC1zaWRlIGNvZGUgKgogKioqKioqKioqKioqKioqKioqKiovCmltcG9ydCB3aXhVc2VycyBmcm9tICd3aXgtdXNlcnMnOwppbXBvcnQge2RvUmVnaXN0cmF0aW9ufSBmcm9tICdiYWNrZW5kL3JlZ2lzdGVyJzsKCi8vIC4uLgoKbGV0IGVtYWlsID0gLy8gdGhlIHVzZXIncyBlbWFpbCBhZGRyZXNzZXMKbGV0IHBhc3N3b3JkID0gLy8gdGhlIHVzZXIncyBwYXNzd29yZApsZXQgZmlyc3ROYW1lID0gLy8gdGhlIHVzZXIncyBmaXJzdCBuYW1lCmxldCBsYXN0TmFtZSA9IC8vIHRoZSB1c2VyJ3MgbGFzdCBuYW1lCgpkb1JlZ2lzdHJhdGlvbihlbWFpbCwgcGFzc3dvcmQsIGZpcnN0TmFtZSwgbGFzdE5hbWUpCiAgLnRoZW4oIChyZXN1bHQpID0+IHsKICAgIGlmIChyZXN1bHQuYXBwcm92ZWQpCiAgICAgIC8vIGxvZyB0aGUgdXNlciBpbgogICAgICB3aXhVc2Vycy5hcHBseVNlc3Npb25Ub2tlbihyZXN1bHQuc2Vzc2lvblRva2VuKTsKICAgIGVsc2UgewogICAgICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgfQogIH0gKTsK
/*******************************
 * 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.

LyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogYmFja2VuZCBjb2RlIC0gcmVnaXN0ZXIuanN3ICoKICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCmltcG9ydCB3aXhVc2VycyBmcm9tICd3aXgtdXNlcnMtYmFja2VuZCc7CmltcG9ydCB3aXhDUk0gZnJvbSAnd2l4LWNybS1iYWNrZW5kJzsKCmV4cG9ydCBmdW5jdGlvbiBkb1JlZ2lzdHJhdGlvbihlbWFpbCwgcGFzc3dvcmQsIGZpcnN0TmFtZSwgbGFzdE5hbWUpIHsKICAvLyByZWdpc3RlciB0aGUgdXNlcgogIHJldHVybiB3aXhVc2Vycy5yZWdpc3RlcihlbWFpbCwgcGFzc3dvcmQsIHsKICAgIGNvbnRhY3RJbmZvOiB7CiAgICAgICJmaXJzdE5hbWUiOiBmaXJzdE5hbWUsCiAgICAgICJsYXN0TmFtZSI6IGxhc3ROYW1lCiAgICB9CiAgfSApCiAgICAudGhlbiggKHJlc3VsdHMpID0+IHsKICAgICAg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gICBjb25zb2xlLmxvZygiTm90IGFwcHJvdmVkISIpOwogICAgICB9CiAgICB9ICk7Cn0K
/*******************************
 * 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!");
      }
    } );
}
emailUser( )

emailUser( )

Sends a Triggered Email to the currently logged-in site member.

function emailUser(emailId: String, toUser: String, [options: TriggeredEmailOptions]): Promise<void>

Description

To learn more about Triggered Emails, see:

Before using the emailUser() function, you need to set up at least one Triggered Email.

Specify which email to send by passing the email's ID in the emailId parameter.

Specify which member the email is sent to by passing the member's user ID in the toUser parameter. You can only send the email to the currently logged-in member. You can get that member's ID using the id property of the currentUser.

If the specified Triggered Email contains variables, you can pass values for those variables using the optional options parameter. You pass a TriggeredEmailOptions object which contains the values to be inserted into your email in place of the variables defined in that email. The values passed must be strings. If the object you pass to the options parameter does not contain a key:value pair for a variable in your Triggered Email, the fallback value defined when creating your Triggered Email is inserted in place of the variable.

Note that Triggered Emails generates a code snippet for each of your email templates. The generated code includes the email's ID and keys for all the email's variable names. You can copy and paste the snippet into your code. Then, you need to define values for the toUser property and for each variable key. To learn how to use the generated snippet in your code, see How to Send a Triggered Email with Code.

Parameters

emailId String The Email ID of the Triggered Email to send.
toUser String The User ID of the currently signed-in member.
options (optional) TriggeredEmailOptions Variable values to insert into the email.

Return Value

Returns a Promise

On fulfillment void When the email is sent.
On rejection String Error message.

Examples

Send a Triggered Email to the currently logged-in member

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VySWQgPSB3aXhVc2Vycy5jdXJyZW50VXNlci5pZDsKCndpeFVzZXJzLmVtYWlsVXNlcigiZW1haWxJRCIsIHVzZXJJZCkKICAudGhlbiggKCkgPT4gewogICAgY29uc29sZS5sb2coIlRyaWdnZXJlZCBlbWFpbCBzZW50Iik7CiAgfSApCiAgLmNhdGNoKCAoZXJyKSA9PiB7CiAgICBjb25zb2xlLmxvZyhlcnIpOwogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let userId = wixUsers.currentUser.id;

wixUsers.emailUser("emailID", userId)
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);
  } );

Send a Triggered Email to the currently logged-in member with variable values

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VySWQgPSB3aXhVc2Vycy5jdXJyZW50VXNlci5pZDsKbGV0IHZhbHVlMSA9IC8vIHZhbHVlIGZvciB2YXJpYWJsZTEKCndpeFVzZXJzLmVtYWlsVXNlcigiZW1haWxJRCIsIHVzZXJJZCwgewogICAgInZhcmlhYmxlcyI6IHsKICAgICAgInZhcmlhYmxlMSI6IHZhbHVlMSwKICAgICAgInZhcmlhYmxlMiI6ICJ2YWx1ZSBmb3IgdmFyaWFibGUyIgogICAgfQogIH0gKQogIC50aGVuKCAoKSA9PiB7CiAgICBjb25zb2xlLmxvZygiVHJpZ2dlcmVkIGVtYWlsIHNlbnQiKTsKICB9ICkKICAuY2F0Y2goIChlcnIpID0+IHsKICAgIGNvbnNvbGUubG9nKGVycik7CiAgfSApOwo=
import wixUsers from 'wix-users';

// ...

let userId = wixUsers.currentUser.id;
let value1 = // value for variable1

wixUsers.emailUser("emailID", userId, {
    "variables": {
      "variable1": value1,
      "variable2": "value for variable2"
    }
  } )
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);
  } );
logout( )

logout( )

Logs the current user out of the site.

function logout(): void

Description

The logout() function logs the current user out of the site.

Examples

Log out the current user

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLmxvZ291dCgpOwo=
import wixUsers from 'wix-users';

// ...

wixUsers.logout();
onLogin( )

onLogin( )

Sets the function that runs when a user logs in.

function onLogin(handler: LoginHandler): void
callback LoginHandler(): void

Description

Use the onLogin() function for code you want to run after a user successfully logs into your site.

Usually, you want to call the onLogin() function in the Site tab of the code panel so that the onLogin() event handler runs no matter which page on your site a user uses to log in.

Parameters

handler function(user) The name of the function or the function expression to run when a user has logged in.
user User The user that has logged in.

Examples

Run code when a user logs in

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLm9uTG9naW4oICh1c2VyKSA9PiB7CiAgbGV0IHVzZXJJZCA9IHVzZXIuaWQ7ICAgICAgICAgICAvLyAicjVjbWUtNmZlbS00ODVqLWRqcmUtNDg0NGM0OSIKICBsZXQgaXNMb2dnZWRJbiA9IHVzZXIubG9nZ2VkSW47IC8vIHRydWUKICBsZXQgdXNlclJvbGUgPSB1c2VyLnJvbGU7ICAgICAgIC8vICJNZW1iZXIiCn0gKTsK
import wixUsers from 'wix-users';

// ...

wixUsers.onLogin( (user) => {
  let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
  let isLoggedIn = user.loggedIn; // true
  let userRole = user.role;       // "Member"
} );
promptForgotPassword( )

promptForgotPassword( )

Prompts the current site visitor with a password reset.

function promptForgotPassword([language: String]): Promise<void>

Description

The promptForgotPassword() function returns a Promise that resolves when the user has sumbitted the forgot password form.

The promptForgotPassword() function cannot be called before the page is ready.

Parameters

language (optional) String The language of the reset password form. Defaults to "English" if not passed or the given language is not one of the languages found in the Permissions tab of the Page Settings panel in the Editor.

Return Value

Returns a Promise

On fulfillment void
On rejection String Message that the dialog was canceled, user is already logged in, or any other reason the password reset failed.

Examples

Prompt the user with a password reset

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLnByb21wdEZvcmdvdFBhc3N3b3JkKCk7Cg==
import wixUsers from 'wix-users';

// ...

wixUsers.promptForgotPassword();

Prompt the current user to login with given language

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLnByb21wdEZvcmdvdFBhc3N3b3JkKCkKICAudGhlbiggKCApID0+IHsKICAgIGNvbnNvbGUubG9nKCJQYXNzd29yZCByZXNldCBzdWJtaXR0ZWQiKTsKICB9ICkKICAuY2F0Y2goIChlcnIpID0+IHsKICAgIGxldCBlcnJvck1zZyA9IGVycjsgLy8gIlRoZSB1c2VyIGNsb3NlZCB0aGUgZm9yZ290IHBhc3N3b3JkIGRpYWxvZyIKICB9ICk7Cg==
import wixUsers from 'wix-users';

// ...

wixUsers.promptForgotPassword()
  .then( ( ) => {
    console.log("Password reset submitted");
  } )
  .catch( (err) => {
    let errorMsg = err; // "The user closed the forgot password dialog"
  } );

See Also

promptLogin( )

promptLogin( )

promptLogin( )

Prompts the current site visitor to log in as a site member.

function promptLogin([options: LoginOptions]): Promise<User>

Description

The promptLogin() function returns a Promise that resolves to the newly logged in user when the login has completed.

The promptLogin() function cannot be called before the page is ready.

Parameters

options LoginOptions The options that determine how the login dialog box appears.

Return Value

Returns a Promise

On fulfillment User Information about the newly logged in user.
On rejection String Message that the dialog was canceled, or any other reason the user failed to log in.

Examples

Prompt the current user to login

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCndpeFVzZXJzLnByb21wdExvZ2luKCkKICAudGhlbiggKHVzZXIpID0+IHsKICAgIGxldCB1c2VySWQgPSB1c2VyLmlkOyAgICAgICAgICAgLy8gInI1bWUtNmZlbS00NWpmLWRqaGUtNDg0MzQ5IgogICAgbGV0IGlzTG9nZ2VkSW4gPSB1c2VyLmxvZ2dlZEluOyAvLyB0cnVlCiAgICBsZXQgdXNlclJvbGUgPSB1c2VyLnJvbGU7ICAgICAgIC8vICJtZW1iZXIiCiAgICByZXR1cm4gdXNlci5nZXRFbWFpbCgpOwogIH0gKQogIC50aGVuKCAoZW1haWwpID0+IHsKICAgIGxldCB1c2VyRW1haWwgPSBlbWFpbDsgICAgICAgICAgLy8gInVzZXJAc29tZXRoaW5nLmNvbSIKICB9ICkKICAuY2F0Y2goIChlcnIpID0+IHsKICAgIGxldCBlcnJvck1zZyA9IGVycjsgICAgICAgICAgLy8gIlRoZSB1c2VyIGNsb3NlZCB0aGUgbG9naW4gZGlhbG9nIgogIH0gKTsK
import wixUsers from 'wix-users';

// ...

wixUsers.promptLogin()
  .then( (user) => {
    let userId = user.id;           // "r5me-6fem-45jf-djhe-484349"
    let isLoggedIn = user.loggedIn; // true
    let userRole = user.role;       // "member"
    return user.getEmail();
  } )
  .then( (email) => {
    let userEmail = email;          // "user@something.com"
  } )
  .catch( (err) => {
    let errorMsg = err;          // "The user closed the login dialog"
  } );

Prompt the current user to login with given options

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCBvcHRpb25zID0geyJtb2RlIjogImxvZ2luIiwgImxhbmciOiAiZXMifTsKCndpeFVzZXJzLnByb21wdExvZ2luKG9wdGlvbnMpCiAgLnRoZW4oICh1c2VyKSA9PiB7CiAgICBsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNW1lLTZmZW0tNDVqZi1kamhlLTQ4NDM0OSIKICAgIGxldCBpc0xvZ2dlZEluID0gdXNlci5sb2dnZWRJbjsgLy8gdHJ1ZQogICAgbGV0IHVzZXJSb2xlID0gdXNlci5yb2xlOyAgICAgICAvLyAibWVtYmVyIgogICAgcmV0dXJuIHVzZXIuZ2V0RW1haWwoKTsKICB9ICkKICAudGhlbiggKGVtYWlsKSA9PiB7CiAgICBsZXQgdXNlckVtYWlsID0gZW1haWw7ICAgICAgICAgIC8vICJ1c2VyQHNvbWV0aGluZy5jb20iCiAgfSApCiAgLmNhdGNoKCAoZXJyKSA9PiB7CiAgICBsZXQgZXJyb3JNc2cgPSBlcnI7ICAgICAgICAgICAgIC8vICJUaGUgdXNlciBjbG9zZWQgdGhlIGxvZ2luIGRpYWxvZyIKICB9ICk7Cg==
import wixUsers from 'wix-users';

// ...

let options = {"mode": "login", "lang": "es"};

wixUsers.promptLogin(options)
  .then( (user) => {
    let userId = user.id;           // "r5me-6fem-45jf-djhe-484349"
    let isLoggedIn = user.loggedIn; // true
    let userRole = user.role;       // "member"
    return user.getEmail();
  } )
  .then( (email) => {
    let userEmail = email;          // "user@something.com"
  } )
  .catch( (err) => {
    let errorMsg = err;             // "The user closed the login dialog"
  } );

See Also

promptForgotPassword( )

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 becomes 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 becomes 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.

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 as a site member with manual approval

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCBlbWFpbCA9IC8vIHRoZSB1c2VyJ3MgZW1haWwgYWRkcmVzc2VzCmxldCBwYXNzd29yZCA9IC8vIHRoZSB1c2VyJ3MgcGFzc3dvcmQKCndpeFVzZXJzLnJlZ2lzdGVyKGVtYWlsLCBwYXNzd29yZCkKICAudGhlbiggKHJlc3VsdCkgPT4gewogICAgbGV0IHN0YXR1cyA9IHJlc3VsdC5zdGF0dXM7IC8vICJQZW5kaW5nIgogICAgbGV0IGFwcHJvdmFsVG9rZW4gPSByZXN1bHQuYXBwcm92YWxUb2tlbjsKICAgIGxldCB1c2VyID0gcmVzdWx0LnVzZXI7CiAgfSApOwo=
import wixUsers from 'wix-users';

// ...

let email = // the user's email addresses
let password = // the user's password

wixUsers.register(email, password)
  .then( (result) => {
    let status = result.status; // "Pending"
    let approvalToken = result.approvalToken;
    let user = result.user;
  } );

Register a user as a site member with auto approval

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCBlbWFpbCA9IC8vIHRoZSB1c2VyJ3MgZW1haWwgYWRkcmVzc2VzCmxldCBwYXNzd29yZCA9IC8vIHRoZSB1c2VyJ3MgcGFzc3dvcmQKCndpeFVzZXJzLnJlZ2lzdGVyKGVtYWlsLCBwYXNzd29yZCkKICAudGhlbiggKHJlc3VsdCkgPT4gewogICAgbGV0IHN0YXR1cyA9IHJlc3VsdC5zdGF0dXM7IC8vICJBY3RpdmUiCiAgICBsZXQgdXNlciA9IHJlc3VsdC51c2VyOwogIH0gKTsK
import wixUsers from 'wix-users';

// ...

let email = // the user's email addresses
let password = // the user's password

wixUsers.register(email, password)
  .then( (result) => {
    let status = result.status; // "Active"
    let user = result.user;
  } );

Register a user as a site member with registration options

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCBlbWFpbCA9IC8vIHRoZSB1c2VyJ3MgZW1haWwgYWRkcmVzc2VzCmxldCBwYXNzd29yZCA9IC8vIHRoZSB1c2VyJ3MgcGFzc3dvcmQKbGV0IGZpcnN0TmFtZSA9IC8vIHRoZSB1c2VyJ3MgZmlyc3QgbmFtZQpsZXQgbGFzdE5hbWUgPSAvLyB0aGUgdXNlcidzIGxhc3QgbmFtZQpsZXQgcGhvbmUgPSAgLy8gdGhlIHVzZXIncyBwaG9uZSBudW1iZXIKbGV0IG5pY2tuYW1lID0gLy8gdGhlIHVzZXIncyBuaWNrbmFtZQoKd2l4VXNlcnMucmVnaXN0ZXIoZW1haWwsIHBhc3N3b3JkLCB7CiAgICBjb250YWN0SW5mbzogewogICAgICAgICJmaXJzdE5hbWUiOiBmaXJzdE5hbWUsCiAgICAgICAgImxhc3ROYW1lIjogbGFzdE5hbWUsCiAgICAgICAgInBob25lIjogcGhvbmUsCiAgICAgICAgIm5pY2tuYW1lIjogbmlja25hbWUKICAgIH0KICB9ICkKICAudGhlbiggKHJlc3VsdCkgPT4gewogICAgbGV0IHJlc3VsdFN0YXR1cyA9IHJlc3VsdC5zdGF0dXM7CiAgfSApOwo=
import wixUsers from 'wix-users';

// ...

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
let phone =  // the user's phone number
let nickname = // the user's nickname

wixUsers.register(email, password, {
    contactInfo: {
        "firstName": firstName,
        "lastName": lastName,
        "phone": phone,
        "nickname": nickname
    }
  } )
  .then( (result) => {
    let resultStatus = result.status;
  } );
LoginOptions

LoginOptions

An object used by the promptLogin() function to determine how the login dialog box appears.

Type

Object

Properties

mode (optional) String What type of login experience to present: "login" or "signup". Defaults to the option chosen in the Member Signup Settings panel in the Editor.
lang (optional) String The two letter language code of the language to show the login form in. Defaults to "en" if the property doesn't exist or the given language is not one of the languages found in the Permissions tab of the Page Settings panel in the Editor.

Examples

Prompt the current user to login

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCBvcHRpb25zID0geyJtb2RlIjogImxvZ2luIiwgImxhbmciOiAiZXMifTsKCndpeFVzZXJzLnByb21wdExvZ2luKG9wdGlvbnMpCiAgLnRoZW4oICh1c2VyKSA9PiB7CiAgICBsZXQgdXNlcklkID0gdXNlci5pZDsgICAgICAgICAgIC8vICJyNW1lLTZmZW0tNDVqZi1kamhlLTQ4NDM0OSIKICAgIGxldCBpc0xvZ2dlZEluID0gdXNlci5sb2dnZWRJbjsgLy8gdHJ1ZQogICAgbGV0IHVzZXJSb2xlID0gdXNlci5yb2xlOyAgICAgICAvLyAibWVtYmVyIgogICAgcmV0dXJuIHVzZXIuZ2V0RW1haWwoKTsKICB9ICkKICAudGhlbiggKGVtYWlsKSA9PiB7CiAgICBsZXQgdXNlckVtYWlsID0gZW1haWw7ICAgICAgICAgIC8vICJ1c2VyQHNvbWV0aGluZy5jb20iCiAgfSApCiAgLmNhdGNoKCAoZXJyKSA9PiB7CiAgICBsZXQgZXJyb3JNc2cgPSBlcnI7ICAgICAgICAgICAgIC8vICJUaGUgdXNlciBjbG9zZWQgdGhlIGxvZ2luIGRpYWxvZyIKICB9ICk7Cg==
import wixUsers from 'wix-users';

// ...

let options = {"mode": "login", "lang": "es"};

wixUsers.promptLogin(options)
  .then( (user) => {
    let userId = user.id;           // "r5me-6fem-45jf-djhe-484349"
    let isLoggedIn = user.loggedIn; // true
    let userRole = user.role;       // "member"
    return user.getEmail();
  } )
  .then( (email) => {
    let userEmail = email;          // "user@something.com"
  } )
  .catch( (err) => {
    let errorMsg = err;             // "The user closed the login dialog"
  } );

See Also

promptLogin( )

RegistrationOptions

RegistrationOptions

An object that contains information about a site registration.

Type

Object

Properties

contactInfo ContactInfo Contact information.

See Also

register

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".
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.

See Also

[register](#register

TriggeredEmailOptions

TriggeredEmailOptions

An object used when sending a Triggered Email.

Type

Object

Properties

variables Object An object with key:value pairs where each key is a variable in the email template created in Triggered Emails and its corresponding value is the value to insert into the template in place of variable. The values must be strings.

Examples

Send a Triggered Email to the currently logged-in member

aW1wb3J0IHdpeFVzZXJzIGZyb20gJ3dpeC11c2Vycyc7CgovLyAuLi4KCmxldCB1c2VySWQgPSB3aXhVc2Vycy5jdXJyZW50VXNlci5pZDsKbGV0IHZhbHVlMSA9IC8vIHZhbHVlIGZvciB2YXJpYWJsZTEKCndpeFVzZXJzLmVtYWlsVXNlcigiZW1haWxJRCIsIHVzZXJJZCwgewogICAgInZhcmlhYmxlcyI6IHsKICAgICAgInZhcmlhYmxlMSI6IHZhbHVlMSwKICAgICAgInZhcmlhYmxlMiI6ICJ2YWx1ZSBmb3IgdmFyaWFibGUyIgogICAgfQogIH0gKQogIC50aGVuKCAoKSA9PiB7CiAgICBjb25zb2xlLmxvZygiVHJpZ2dlcmVkIGVtYWlsIHNlbnQiKTsKICB9ICkKICAuY2F0Y2goIChlcnIpID0+IHsKICAgIGNvbnNvbGUubG9nKGVycik7CiAgfSApOwo=
import wixUsers from 'wix-users';

// ...

let userId = wixUsers.currentUser.id;
let value1 = // value for variable1

wixUsers.emailUser("emailID", userId, {
    "variables": {
      "variable1": value1,
      "variable2": "value for variable2"
    }
  } )
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);
  } );

See Also

emailUser( )