approveByToken( )
Approves a pending member using an approval token.
Description
The approveByToken()
function returns a Promise that resolves to a session token
when the specified member is approved. Tokens must be approved within 30 hours of token creation.
A new member's status is "PENDING"
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 returned from the
register()
function when calling approveByToken()
.
Syntax
function approveByToken(token: string): Promise<string>
approveByToken Parameters
NAME
TYPE
DESCRIPTION
Returns
Return Type:
Related Content:
Was this helpful?
This example contains a backend function that approves a pending member using an approval token. It returns a session token to be used in page code to log in the member who was just approved.
12import { Permissions, webMethod } from 'wix-web-module';3import { authentication } from 'wix-members-backend';45// Sample token value:6// 'JWS.eyJraWQiOiJSc012MmV3MiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImEyMWE1MmU4LWViMzUtNDExYS04OTNkLWFlNTgxM2I1YjY2Y1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIn0iLCJpYXQiOjE2Mjc3NzkyNjYsImV4cCI6MTYyNzg4NzI2Nn0.53pZSaPInXrAvWpTOxsZxqxBHQIof1j6Gbkqg92l82o'78export const myApproveByTokenFunction = webMethod(Permissions.Anyone, (token) => {9 return authentication.approveByToken(token)10 .then((sessionToken) => {11 return {12 sessionToken: sessionToken,13 approved: true14 };15 })16 .catch((error) => {17 return {18 approved: false,19 reason: error20 };21 });22});2324/* Promise resolves to:25 * "JWS.eyJraWQiOiJQSXpvZGJiQiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImEyMWE1MmU4LWViMzUtNDExYS04OTNkLWFlNTgxM2I1YjY2Y1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2Mjc3Nzk0MTg5NzUsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjI3Nzc5NTM4OTc1LFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyNzc3OTQxOH0.srXs33K5gT5KaZp4fTZ9xRkVasayOTox6IK2ZG3tKrA"26 */27
This example demonstrates a common 3rd-party approval flow.
The backend code calls a 3rd-party service
that determines whether the member will be approved.
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 page code.
If rejected, the blockByEmail()
function is called.
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';78export const doRegistration = webMethod(Permissions.Anyone, async (email, password, firstName, lastName) => {910 // Call a 3rd-party API to check if the member is approved.11 const isApproved = await approveBy3rdParty(email, password);1213 // If member is approved by 3rd party, register and approve with the Wix site14 if (isApproved === true) {15 const options = {16 contactInfo: {17 firstName: firstName,18 lastName: lastName19 }20 };2122 // Register the member23 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');2627 // Approve the member and get session token, to be used to log in the member client-side28 const sessionToken = await authentication.approveByToken(approvalToken);29 console.log('Member is now approved, but not logged in');3031 return {32 approved: true,33 sessionToken: sessionToken34 };3536 } else {37 // If not approved by the 3rd party3839 await authentication.blockByEmail(email);40 console.log('Member not approved by 3rd-party SSO. Blocking from Wix site.');4142 return { approved: false };43 }44});454647/*************48 * Page code *49 *************/50import { authentication } from 'wix-members-frontend';51import { doRegistration } from 'backend/register.web';5253// ...5455$w('#register').onClick(() => {5657 const email = $w('#email').value;58 const password = $w('#password').value;59 const firstName = $w('#firstName').value;60 const lastName = $w('#lastName').value;6162 doRegistration(email, password, firstName, lastName)63 .then((result) => {64 if (result.approved) {65 // Log the member in66 console.log('Logging in...');67 authentication.applySessionToken(result.sessionToken);68 } else {69 console.log('Not approved!');70 }71 });72});
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 a member goes to the verification page, the approval is granted and the member is logged in to 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.
1/*******************************2 * Backend code - register.web.js *3 *******************************/45import { Permissions, webMethod } from 'wix-web-module';6import { authentication } from 'wix-members-backend';7import { triggeredEmails } from 'wix-crm-backend';89export const doRegistration = webMethod(Permissions.Anyone, async (email, password, firstName, lastName) => {10 const registrationOptions = {11 contactInfo: {12 firstName: firstName,13 lastName: lastName14 }15 };16 const registration = await authentication.register(email, password, registrationOptions);17 console.log('Member is now registered with the site and pending approval');1819 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});2829export 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: sessionToken36 };37 } catch (error) {38 console.log('Member not approved');39 return {40 approved: false,41 reason: error42 };43 }44});4546/****************************47 * Page code - registration *48 ****************************/49import { doRegistration } from 'backend/register.web';5051// ...5253const email = $w('#email').value;54const password = $w('#password').value;55const firstName = $w('#firstName').value;56const lastName = $w('#lastName').value;5758doRegistration(email, password, firstName, lastName)59 .then(() => {60 console.log('Confirmation email sent.');61 });6263/*********************************64 * Page code - post-registration *65 *********************************/66import wixLocationFrontend from 'wix-location-frontend';67import { authentication } from 'wix-members-frontend';68import { doApproval } from 'backend/register.web';6970$w.onReady(async () => {71 const token = wixLocationFrontend.query.token;7273 const approval = await doApproval(token);7475 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