Search.../

approveByToken( )

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

Description

The approveByToken() function returns a Promise that resolves to a session token when the specified user is approved. Tokens must be approved within 30 hours of token creation.

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 your 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().

Syntax

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

approveByToken Parameters

NAME
TYPE
DESCRIPTION
token
string

Approval token returned by the register() function.

Returns

Fulfilled - A session token for logging in the approved user in client-side code. Rejected - Error message.

Return Type:

Promise<string>

Was this helpful?

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.

Copy Code
1import wixUsersBackend from 'wix-users-backend';
2
3export function myBackendFunction(token) {
4 return wixUsersBackend.approveByToken(token)
5 .then( (sessionToken) => {
6 return {sessionToken, "approved": true};
7 } )
8 .catch( (error) => {
9 return {"approved": false, "reason": error};
10 } );
11}
12
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.

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