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.

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

See New Members to learn about verifying emails and approving members.

Notes:

  • The member data in the resolved Promise includes custom fields from your site's contacts only if they are added to your site members in your 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.

  • 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 wix-members-frontend.

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 { Permissions, webMethod } from 'wix-web-module';
2import { authentication } from 'wix-members-backend';
3
4/* Sample options value:
5 * {
6 * contactInfo: {
7 * firstName: 'Javier',
8 * lastName: 'Doe',
9 * hobby: 'Basketball,
10 * "favorite-meal": 'Pasta'
11 * },
12 * privacyStatus: 'PUBLIC'
13 * }
14 */
15
16export const myRegisterMemberFunction = webMethod(Permissions.Anyone, (email, password, options) => {
17 return authentication.register(email, password, options)
18 .then((registrationResult) => {
19 return registrationResult;
20 })
21 .catch((error) => {
22 console.error(error);
23 })
24});
25
26/* Promise resolves to:
27 * {
28 * "member": {
29 * "_id": "efaaf13f-934e-4449-b0c2-304030767671",
30 * "createdDate": "2021-08-01T12:28:42Z",
31 * "updatedDate": "2021-08-01T12:28:41.847Z",
32 * "status": "UNKNOWN",
33 * "contactId": "efaaf13f-934e-4449-b0c2-304030767671",
34 * "profile": {
35 * "nickname": "Javier Doe",
36 * "slug": "javierdoe"
37 * },
38 * "privacyStatus": "UNKNOWN",
39 * "activityStatus": "UNKNOWN"
40 * },
41 * "status": "PENDING",
42 * "approvalToken": "JWS.eyJraWQiOiJSc012MmV3MiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImVmYWFmMTNmLTkzNGUtNDQ0OS1iMGMyLTMwNDAzMDc2NzY3MVwiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIn0iLCJpYXQiOjE2Mjc4MjA5MjEsImV4cCI6MTYyNzkyODkyMX0.zOuE8ZXRBQT4tPPFqvseE8xKm6kHrmHG3Lrndz7l7Ng"
43 * }
44 */
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.web.js *
3 *******************************/
4import { Permissions, webMethod } from 'wix-web-module';
5import { authentication } from 'wix-members-backend';
6import { approveBy3rdParty } from 'backend/some-backend-module';
7
8export const doRegistration = webMethod(Permissions.Anyone, async (email, password, firstName, lastName) => {
9
10 // Call a 3rd-party API to check if the member is approved.
11 const isApproved = await approveBy3rdParty(email, password);
12
13 // If member is approved by 3rd party, register and approve with the Wix site
14 if (isApproved === true) {
15 const options = {
16 contactInfo: {
17 firstName: firstName,
18 lastName: lastName
19 }
20 };
21
22 // Register the member
23 const registration = await authentication.register(email, password, options);
24 const approvalToken = registration.approvalToken;
25 console.log('Member is now registered with the site and pending approval');
26
27 // Approve the member and get session token, to be used to log in the member client-side
28 const sessionToken = await authentication.approveByToken(approvalToken);
29 console.log('Member is now approved, but not logged in');
30
31 return {
32 approved: true,
33 sessionToken: sessionToken
34 };
35
36 } else {
37 // If not approved by the 3rd party
38
39 await authentication.blockByEmail(email);
40 console.log('Member not approved by 3rd-party SSO. Blocking from Wix site.');
41
42 return { approved: false };
43 }
44});
45
46
47/*************
48 * Page code *
49 *************/
50import { authentication } from 'wix-members-frontend';
51import { doRegistration } from 'backend/register.web';
52
53// ...
54
55$w('#register').onClick(() => {
56
57 const email = $w('#email').value;
58 const password = $w('#password').value;
59 const firstName = $w('#firstName').value;
60 const lastName = $w('#lastName').value;
61
62 doRegistration(email, password, firstName, lastName)
63 .then((result) => {
64 if (result.approved) {
65 // Log the member in
66 console.log('Logging in...');
67 authentication.applySessionToken(result.sessionToken);
68 } else {
69 console.log('Not approved!');
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.web.js *
3 *******************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { authentication } from 'wix-members-backend';
7import { triggeredEmails } from 'wix-crm-backend';
8
9export const doRegistration = webMethod(Permissions.Anyone, async (email, password, firstName, lastName) => {
10 const registrationOptions = {
11 contactInfo: {
12 firstName: firstName,
13 lastName: lastName
14 }
15 };
16 const registration = await authentication.register(email, password, registrationOptions);
17 console.log('Member is now registered with the site and pending approval');
18
19 const emailOptions = {
20 variables: {
21 name: firstName,
22 verifyLink: `http://yourdomain.com/post-register?token=${registration.approvalToken}`
23 }
24 };
25 triggeredEmails.emailMember('verifyRegistration', registration.member.id, emailOptions);
26 console.log('Confirmation email sent');
27});
28
29export const doApproval = webMethod(Permissions.Anyone, async (token) => {
30 try {
31 const sessionToken = await authentication.approveByToken(token);
32 console.log('Member approved');
33 return {
34 approved: true,
35 sessionToken: sessionToken
36 };
37 } catch (error) {
38 console.log('Member not approved');
39 return {
40 approved: false,
41 reason: error
42 };
43 }
44});
45
46/****************************
47 * Page code - registration *
48 ****************************/
49import { doRegistration } from 'backend/register.web';
50
51// ...
52
53const email = $w('#email').value;
54const password = $w('#password').value;
55const firstName = $w('#firstName').value;
56const lastName = $w('#lastName').value;
57
58doRegistration(email, password, firstName, lastName)
59 .then(() => {
60 console.log('Confirmation email sent.');
61 });
62
63/*********************************
64 * Page code - post-registration *
65 *********************************/
66import wixLocationFrontend from 'wix-location-frontend';
67import { authentication } from 'wix-members-frontend';
68import { doApproval } from 'backend/register.web';
69
70$w.onReady(async () => {
71 const token = wixLocationFrontend.query.token;
72
73 const approval = await doApproval(token);
74
75 if (approval.approved === true) {
76 authentication.applySessionToken(approval.sessionToken);
77 console.log('Member approved & logged in');
78 } else {
79 console.log('Member not approved');
80 }
81});
82