Search.../

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
email
string

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:

Promise<string>

Was this helpful?

Generate a session token

Copy Code
1import { authentication } from 'wix-members-backend';
2
3export function myGenerateSessionTokenFunction(email) {
4
5 return authentication.generateSessionToken(email)
6 .then((sessionToken) => {
7 return sessionToken;
8 })
9 .catch((error) => {
10 console.error(error);
11 });
12}
13
14/* Promise resolves to a session token:
15 * "JWS.eyJraWQiOiJQSXpvZGJiQiIsImFsZyI6IkhTMjU2In0.eyJkYXRhIjoie1wiaWRcIjpcImM2OTE2N2FmLTY0ODgtNDYzNS1iYmU3LTg5YzFjZWY2MTEwN1wiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2MjI0MDMwOTM5MTEsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjIyNDAzMjEzOTExLFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyMjQwMzA5M30.xDMCeRG2DIDa4YR6_XuTf7KBRgHFb0qW7K6gsVMLXUM"
16 */
Log a member in after 3rd-party authentication

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.

Copy Code
1/****************************
2 * Backend code - login.jsw *
3 ***************************/
4
5 import { authentication } from 'wix-members-backend';
6 import { authBy3rdParty } from 'backend/authentications';
7
8 export function getLoginToken(email, password) {
9
10 return authBy3rdParty(email, password)
11 .then((isAuthenticated) => {
12
13 // If authenticated, generate and return the session token
14 if (isAuthenticated) {
15 return authentication.generateSessionToken(email)
16 .then((sessionToken) => {
17 return {
18 sessionToken: sessionToken,
19 approved: true
20 };
21 });
22 }
23
24 // If not authenticated, return non-approval
25 return { approved: false };
26 });
27 }
28
29 /*************
30 * Page code *
31 ************/
32import { getLoginToken } from 'backend/login';
33import { authentication } from 'wix-members-frontend';
34
35// ...
36
37const email = $w('email').value;
38const password = $w('password').value;
39
40// Call the backend function to get the session token
41getLoginToken(email, password)
42 .then((loginResult) => {
43 if (loginResult.approved) {
44 // If approved, log the member in using the returned session token
45 authentication.applySessionToken(loginResult.sessionToken);
46 } else {
47 // If not approved, log a message
48 console.log("Member not approved.");
49 }
50 });
51