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.
1import { authentication } from 'wix-members-backend';23// Sample token value:4// 'JWS.eyJraWQiOiJSc012MmV3MiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImEyMWE1MmU4LWViMzUtNDExYS04OTNkLWFlNTgxM2I1YjY2Y1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIn0iLCJpYXQiOjE2Mjc3NzkyNjYsImV4cCI6MTYyNzg4NzI2Nn0.53pZSaPInXrAvWpTOxsZxqxBHQIof1j6Gbkqg92l82o'56export function myApproveByTokenFunction(token) {78 return authentication.approveByToken(token)9 .then((sessionToken) => {10 return {11 sessionToken: sessionToken,12 approved: true13 };14 })15 .catch( (error) => {16 return {17 approved: false,18 reason: error19 };20 });21}2223/* Promise resolves to:24 * "JWS.eyJraWQiOiJQSXpvZGJiQiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImEyMWE1MmU4LWViMzUtNDExYS04OTNkLWFlNTgxM2I1YjY2Y1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2Mjc3Nzk0MTg5NzUsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjI3Nzc5NTM4OTc1LFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyNzc3OTQxOH0.srXs33K5gT5KaZp4fTZ9xRkVasayOTox6IK2ZG3tKrA"25 */
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.jsw *3 *******************************/4import { authentication } from 'wix-members-backend';5import { approveBy3rdParty } from 'backend/some-backend-module';67export async function doRegistration(email, password, firstName, lastName) {89 // Call a 3rd-party API to check if the member is approved.10 const isApproved = await approveBy3rdParty(email, password);1112 // If member is approved by 3rd party, register and approve with the Wix site13 if (isApproved === true) {14 const options = {15 contactInfo: {16 firstName: firstName,17 lastName: lastName18 }19 };2021 // Register the member22 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');2526 // Approve the member and get session token, to be used to log in the member client-side27 const sessionToken = await authentication.approveByToken(approvalToken);28 console.log('Member is now approved, but not logged in');2930 return {31 approved: true,32 sessionToken: sessionToken33 };3435 } else {36 // If not approved by the 3rd party3738 await authentication.blockByEmail(email);39 console.log('Member not approved by 3rd-party SSO. Blocking from Wix site.');4041 return { approved: false };42 }43}444546/*************47 * Page code *48 *************/49import { authentication } from 'wix-members-frontend';50import { doRegistration } from 'backend/register';5152// ...5354$w('#register').onClick(() => {5556 const email = $w('#email').value;57 const password = $w('#password').value;58 const firstName = $w('#firstName').value;59 const lastName = $w('#lastName').value;6061 doRegistration(email, password, firstName, lastName)62 .then((result) => {63 if (result.approved) {64 // Log the member in65 console.log('Logging in...');66 authentication.applySessionToken(result.sessionToken);67 } else {68 console.log('Not approved!');69 }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.jsw *3 *******************************/45import { authentication } from 'wix-members-backend';6import { triggeredEmails } from 'wix-crm-backend';78// To be called from the registration page code9export async function doRegistration(email, password, firstName, lastName) {10 // Register the member11 const registrationOptions = {12 contactInfo: {13 firstName: firstName,14 lastName: lastName15 }16 };17 const registration = await authentication.register(email, password, registrationOptions);18 console.log('Member is now registered with the site and pending approval');1920 // Send a registration confirmation email21 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}3031// To be called from the post-registration page code32export async function doApproval(token) {33 try {34 // Approve the member35 const sessionToken = await authentication.approveByToken(token);36 console.log('Member approved');37 return {38 approved: true,39 sessionToken: sessionToken40 };41 } catch (error) {42 // If an error is encountered and the member can't be approved43 console.log('Member not approved');44 return {45 approved: false,46 reason: error47 };48 }49}5051/****************************52 * Page code - registration *53 ****************************/54import { doRegistration } from 'backend/register';5556// ...5758const email = $w('#email').value;59const password = $w('#password').value;60const firstName = $w('#firstName').value;61const lastName = $w('#lastName').value;6263doRegistration(email, password, firstName, lastName)64 .then(() => {65 console.log('Confirmation email sent.');66 });6768/*********************************69 * Page code - post-registration *70 *********************************/71import wixLocationFrontend from 'wix-location-frontend';72import { authentication } from 'wix-members-frontend';73import { doApproval } from 'backend/register';7475$w.onReady(async () => {76 // Get the token from the URL77 const token = wixLocationFrontend.query.token;7879 // Send token to backend code80 const approval = await doApproval(token);8182 if (approval.approved === true) {83 // Log the member in84 authentication.applySessionToken(approval.sessionToken);85 console.log('Member approved & logged in');86 } else {87 console.log('Member not approved');88 }89});90