generateSessionToken( )
Creates a session token for a member authenticated by a 3rd party.
Description
The generateSessionToken()
function returns a Promise that resolves to a session token
used to log a member in to your site.
Note: This function replaces the deprecated
wix-users-backend.generateSessionToken()
. The deprecated function will continue to work, but it will not receive updates. To keep any existing code compatible with future changes, see the migration instructions.
Use generateSessionToken()
to bypass Wix member authentication
when using a 3rd-party service to authenticate your members.
For example, you can use generateSessionToken()
to provide Single Sign-On (SSO) for members
where they authenticate with a non-Wix entity to log in to your Wix site.
If the specified email address corresponds to an existing member, a session token for logging in that member is generated.
If there is no existing member with the specified email address, a new member is created and a session token for logging in that member is generated. The member is created with a random password.
Syntax
function generateSessionToken(email: string): Promise<string>
generateSessionToken Parameters
NAME
TYPE
DESCRIPTION
Login email address of the member to approve.
Returns
Fulfilled - Session token to apply in page code to log the authenticated member in.
Return Type:
Was this helpful?
1import { authentication } from 'wix-members-backend';23export function myGenerateSessionTokenFunction(email) {45 return authentication.generateSessionToken(email)6 .then((sessionToken) => {7 return sessionToken;8 })9 .catch((error) => {10 console.error(error);11 });12}1314/* Promise resolves to a session token:15 * "JWS.eyJraWQiOiJQSXpvZGJiQiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImM2OTE2N2FmLTY0ODgtNDYzNS1iYmU3LTg5YzFjZWY2MTEwN1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2MjI0MDMwOTM5MTEsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjIyNDAzMjEzOTExLFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyMjQwMzA5M30.xDMCeRG2DIDa4YR6_XuTf7KBRgHFb0qW7K6gsVMLXUM"16 */
This example contains a backend function that uses a 3rd-party service to authenticate a member. If the authentication is successful, a session token is returned to the page and used to log the member in.
1/****************************2 * Backend code - login.jsw *3 ***************************/45 import { authentication } from 'wix-members-backend';6 import { authBy3rdParty } from 'backend/authentications';78 export function getLoginToken(email, password) {910 return authBy3rdParty(email, password)11 .then((isAuthenticated) => {1213 // If authenticated, generate and return the session token14 if (isAuthenticated) {15 return authentication.generateSessionToken(email)16 .then((sessionToken) => {17 return {18 sessionToken: sessionToken,19 approved: true20 };21 });22 }2324 // If not authenticated, return non-approval25 return { approved: false };26 });27 }2829 /*************30 * Page code *31 ************/32import { getLoginToken } from 'backend/login';33import { authentication } from 'wix-members-frontend';3435// ...3637const email = $w('email').value;38const password = $w('password').value;3940// Call the backend function to get the session token41getLoginToken(email, password)42 .then((loginResult) => {43 if (loginResult.approved) {44 // If approved, log the member in using the returned session token45 authentication.applySessionToken(loginResult.sessionToken);46 } else {47 // If not approved, log a message48 console.log("Member not approved.");49 }50 });51