Search.../

register( )

Registers a new site member.

Description

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

The specified password must be between 4 and 100 ASCII printable characters.

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.

Notes:

  • When member registration is set to auto approve, calling the client-side register() function is equally as secure as calling it from backend code. However, when member approval is set to manual, calling the backend version of the register() function allows you to construct more secure approval flows.

  • A confirmation email is sent to new members when they sign up using an email address that's already in your site's Contact List.

Syntax

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

register Parameters

NAME
TYPE
DESCRIPTION
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.

Returns

Fulfilled - When the user has been registered. Rejected - Error message.

Return Type:

Promise<RegistrationResult>
NAME
TYPE
DESCRIPTION
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". The token expires 30 hours after it is created.

user

The user that has been registered.

Was this helpful?

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.

Copy Code
1/*******************************
2 * Backend code - register.jsw *
3 *******************************/
4import wixUsersBackend from 'wix-users-backend';
5import { approveBy3rdParty } from 'some-backend-module';
6
7export function doRegistration(email, password, firstName, lastName) {
8 // call a 3rd party API to check if the user is approved
9 return approveBy3rdParty(email, password)
10 .then((isApproved) => {
11 // if approved by 3rd party
12 if (isApproved) {
13 // register the user
14 return wixUsersBackend.register(email, password, {
15 "contactInfo": {
16 "firstName": firstName,
17 "lastName": lastName
18 }
19 })
20 // user is now registered and pending approval
21 // approve the user
22 .then((result) => wixUsersBackend.approveByToken(result.approvalToken))
23 // user is now active, but not logged in
24 // return the session token to log in the user client-side
25 .then((sessionToken) => {
26 return { sessionToken, "approved": true };
27 });
28 }
29 // if not approved by 3rd party
30 else {
31 return { "approved": false };
32 }
33 })
34}
35
36/********************
37 * Client-side code *
38 ********************/
39import wixUsers from 'wix-users';
40import { doRegistration } from 'backend/register';
41
42// ...
43
44let email = // the user's email addresses
45 let password = // the user's password
46 let firstName = // the user's first name
47 let lastName = // the user's last name
48
49 doRegistration(email, password, firstName, lastName)
50 .then((result) => {
51 if (result.approved)
52 // log the user in
53 wixUsers.applySessionToken(result.sessionToken);
54 else {
55 console.log("Not approved!");
56 }
57 });
58
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.

The code is split between three locations:

  • A backend web module named register.jsw.
  • The page code for the page where users register.
  • The page code for the page where users confirm their registration.

Copy Code
1/*******************************
2 * Backend code - register.jsw *
3 *******************************/
4import wixUsersBackend from 'wix-users-backend';
5
6export function doRegistration(email, password, firstName, lastName) {
7 // register the user
8 return wixUsersBackend.register(email, password, {
9 "contactInfo": {
10 "firstName": firstName,
11 "lastName": lastName
12 }
13 })
14 .then((results) => {
15 // user is now registered and pending approval
16 // send a registration verification email
17 wixUsersBackend.emailUser('verifyRegistration', results.user.id, {
18 "variables": {
19 "name": firstName,
20 "verifyLink": `http://yourdomain.com/post-register?token=${results.approvalToken}`
21 }
22 });
23 });
24}
25
26export function doApproval(token) {
27 // approve the user
28 return wixUsersBackend.approveByToken(token)
29 // user is now active, but not logged in
30 // return the session token to log in the user client-side
31 .then((sessionToken) => {
32 return { sessionToken, "approved": true };
33 })
34 .catch((error) => {
35 return { "approved": false, "reason": error };
36 });
37}
38
39/*********************************
40 * Client-side registration code *
41 *********************************/
42import { doRegistration } from 'backend/register';
43
44export function button_click(event) {
45 let email = // the user's email address
46 let password = // the user's password
47 let firstName = // the user's first name
48 let lastName = // the user's last name
49
50 doRegistration(email, password, firstName, lastName)
51 .then(() => {
52 console.log("Confirmation email sent.");
53 });
54}
55
56/**************************************
57 * Client-side post-registration code *
58 **************************************/
59import wixLocation from 'wix-location';
60import wixUsersBackend from 'wix-users';
61import { doApproval } from 'backend/register';
62
63$w.onReady(() => {
64 // get the token from the URL
65 let token = wixLocation.query.token;
66
67 doApproval(token)
68 .then((result) => {
69 if (result.approved) {
70 // log the user in
71 wixUsersBackend.applySessionToken(result.sessionToken);
72 console.log("Approved");
73 }
74 else {
75 console.log("Not approved!");
76 }
77 });
78});
79