Search.../

register( )

Registers a new site member.

Description

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

Note: The member data in the resolved promise will only include custom fields from your site's contacts if they are added to your site Members in your site's dashboard.

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

Email verification

You may want to check that a new member is using their own email address, not using someone else's or a fake email address.

You can require members to verify their email address once they've signed up.

You can configure these setting manually in your site's dashboard, or you can enable email verification through code. This example includes email verification through code.

Automatic vs. Manual Approval

The register() function behaves differently depending on your site's member signup settings. Details are outlined in the following table:

Signup SettingResult
Everyone who signs up
(automatic approval)
  • Member status is "ACTIVE".
  • The member can log in to the site, or you can log the member in by passing the returned sessionToken to applySessionToken() from wix-members-frontend.
  • You can have more control of who can become a site member using custom site registration and Velo code. If you are implementing code to automatically approve emails in a collection, make sure to set the collection Permissions to private and to never expose the collection in your frontend code.
Only people who I approve manually
(manual approval)
  • Member status is "PENDING".
  • The member must be approved. You can approve the member by passing the returned approvalToken to approveByToken() or calling approveByEmail() from wix-members-backend. You can also approve the member in your site's dashboard.

When your site's member signup settings are set to automatic approval, calling register() from wix-members-frontend (in page code) is as secure as calling register() from wix-members-backend in backend code, unless you are implementing custom site registration using Velo forms. However, when registration is set to manual approval, calling register() from wix-members-backend allows you to build more secure approval flows by keeping tokens hidden from the frontend.

Note: Registering a member in the backend always creates a new contact, even if a contact with the specified email already exists. To avoid this behavior, use register() from the frontend wix-members-frontend module.

Syntax

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

register Parameters

NAME
TYPE
DESCRIPTION
email
string

Email address the new member will use to log in.

password
string

Password to assign to the new site member. Must be 4 to 100 ASCII characters.

options
Optional
RegistrationOptions

Registration options.

Returns

Fulfilled - When the member is registered. Rejected - Error message.

Return Type:

Promise<RegistrationResult>
NAME
TYPE
DESCRIPTION
status
string

Registration status.

One of:

  • "PENDING": The member must be approved before they can log in to the site.
  • "ACTIVE": The member is approved and can log in to the site.
sessionToken
string

Token for logging in the current visitor as a site member with the applySessionToken() function from wix-members-frontend.

sessionToken is only returned when approval is automatic and the returned status is "ACTIVE". See Automatic vs. Manual Approval.

approvalToken
string

Token for approving the member with the approveByToken() function. approvalToken is safe to pass via email or from page code to backend code.

approvalToken is only returned when manual approval is required and the returned status is "PENDING". See Automatic vs. Manual Approval.

member
Member

The registered member.

Was this helpful?

Register a member

This example contains custom fields: "Hobby" and "Favorite Meal".

Copy Code
1import { authentication } from 'wix-members-backend';
2
3/* Sample options value:
4 * {
5 * contactInfo: {
6 * firstName: 'Javier',
7 * lastName: 'Doe',
8 * hobby: 'Basketball,
9 * "favorite-meal": 'Pasta'
10 * },
11 * privacyStatus: 'PUBLIC'
12 * }
13 */
14
15export function myRegisterMemberFunction(email, password, options) {
16 return authentication.register(email, password, options)
17 .then((registrationResult) => {
18 return registrationResult;
19 })
20 .catch((error) => {
21 console.error(error);
22 })
23}
24
25/* Promise resolves to:
26 * {
27 * "member": {
28 * "_id": "efaaf13f-934e-4449-b0c2-304030767671",
29 * "createdDate": "2021-08-01T12:28:42Z",
30 * "updatedDate": "2021-08-01T12:28:41.847Z",
31 * "status": "UNKNOWN",
32 * "contactId": "efaaf13f-934e-4449-b0c2-304030767671",
33 * "profile": {
34 * "nickname": "Javier Doe",
35 * "slug": "javierdoe"
36 * },
37 * "privacyStatus": "UNKNOWN",
38 * "activityStatus": "UNKNOWN"
39 * },
40 * "status": "PENDING",
41 * "approvalToken": "JWS.eyJraWQiOiJSc012MmV3MiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImVmYWFmMTNmLTkzNGUtNDQ0OS1iMGMyLTMwNDAzMDc2NzY3MVwiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIn0iLCJpYXQiOjE2Mjc4MjA5MjEsImV4cCI6MTYyNzkyODkyMX0.zOuE8ZXRBQT4tPPFqvseE8xKm6kHrmHG3Lrndz7l7Ng"
42 * }
43 */
Register a member using a 3rd party for approval

This example demonstrates a common 3rd-party approval flow. The backend code calls a 3rd-party service that determines whether the member is approved. If approved, the register() function is called from backend code, the registration is approved, and a session token is returned to the calling page code. If rejected, the blockByEmail() function is called.

Copy Code
1/*******************************
2 * Backend code - register.jsw *
3 *******************************/
4import { authentication } from 'wix-members-backend';
5import { approveBy3rdParty } from 'backend/some-backend-module';
6
7export async function doRegistration(email, password, firstName, lastName) {
8
9 // Call a 3rd-party API to check if the member is approved.
10 const isApproved = await approveBy3rdParty(email, password);
11
12 // If member is approved by 3rd party, register and approve with the Wix site
13 if (isApproved === true) {
14 const options = {
15 contactInfo: {
16 firstName: firstName,
17 lastName: lastName
18 }
19 };
20
21 // Register the member
22 const registration = await authentication.register(email, password, options);
23 const approvalToken = registration.approvalToken;
24 console.log('Member is now registered with the site and pending approval');
25
26 // Approve the member and get session token, to be used to log in the member client-side
27 const sessionToken = await authentication.approveByToken(approvalToken);
28 console.log('Member is now approved, but not logged in');
29
30 return {
31 approved: true,
32 sessionToken: sessionToken
33 };
34
35 } else {
36 // If not approved by the 3rd party
37
38 await authentication.blockByEmail(email);
39 console.log('Member not approved by 3rd-party SSO. Blocking from Wix site.');
40
41 return { approved: false };
42 }
43}
44
45
46/*************
47 * Page code *
48 *************/
49import { authentication } from 'wix-members-frontend';
50import { doRegistration } from 'backend/register';
51
52// ...
53
54$w('#register').onClick(() => {
55
56 const email = $w('#email').value;
57 const password = $w('#password').value;
58 const firstName = $w('#firstName').value;
59 const lastName = $w('#lastName').value;
60
61 doRegistration(email, password, firstName, lastName)
62 .then((result) => {
63 if (result.approved) {
64 // Log the member in
65 console.log('Logging in...');
66 authentication.applySessionToken(result.sessionToken);
67 } else {
68 console.log('Not approved!');
69 }
70 });
71});
72
Register a member, sending an email for confirmation

This example demonstrates a common email verification flow. A member is initially registered but not yet approved. At registration, a verification email is sent with a link to a verification page. When the member goes to the verification page, the approval is granted and the member 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 members register.
  • The page code for the page where members confirm their registration.

Copy Code
1/*******************************
2 * Backend code - register.jsw *
3 *******************************/
4
5import { authentication } from 'wix-members-backend';
6import { triggeredEmails } from 'wix-crm-backend';
7
8// To be called from the registration page code
9export async function doRegistration(email, password, firstName, lastName) {
10 // Register the member
11 const registrationOptions = {
12 contactInfo: {
13 firstName: firstName,
14 lastName: lastName
15 }
16 };
17 const registration = await authentication.register(email, password, registrationOptions);
18 console.log('Member is now registered with the site and pending approval');
19
20 // Send a registration confirmation email
21 const emailOptions = {
22 variables: {
23 name: firstName,
24 verifyLink: `http://yourdomain.com/post-register?token=${registration.approvalToken}`
25 }
26 };
27 triggeredEmails.emailMember('verifyRegistration', registration.member.id, emailOptions);
28 console.log('Confirmation email sent');
29}
30
31// To be called from the post-registration page code
32export async function doApproval(token) {
33 try {
34 // Approve the member
35 const sessionToken = await authentication.approveByToken(token);
36 console.log('Member approved');
37 return {
38 approved: true,
39 sessionToken: sessionToken
40 };
41 } catch (error) {
42 // If an error is encountered and the member can't be approved
43 console.log('Member not approved');
44 return {
45 approved: false,
46 reason: error
47 };
48 }
49}
50
51/****************************
52 * Page code - registration *
53 ****************************/
54import { doRegistration } from 'backend/register';
55
56// ...
57
58const email = $w('#email').value;
59const password = $w('#password').value;
60const firstName = $w('#firstName').value;
61const lastName = $w('#lastName').value;
62
63doRegistration(email, password, firstName, lastName)
64 .then(() => {
65 console.log('Confirmation email sent.');
66 });
67
68/*********************************
69 * Page code - post-registration *
70 *********************************/
71import wixLocationFrontend from 'wix-location-frontend';
72import { authentication } from 'wix-members-frontend';
73import { doApproval } from 'backend/register';
74
75$w.onReady(async () => {
76 // Get the token from the URL
77 const token = wixLocationFrontend.query.token;
78
79 // Send token to backend code
80 const approval = await doApproval(token);
81
82 if (approval.approved === true) {
83 // Log the member in
84 authentication.applySessionToken(approval.sessionToken);
85 console.log('Member approved & logged in');
86 } else {
87 console.log('Member not approved');
88 }
89});
90