Search.../

generateSessionToken( )

Create 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 user into your site.

Use generateSessionToken() to bypass Wix member authentication when using a 3rd party service to authenticate your users. Meaning, you can provide Single Sign-On (SSO) for users where they are authenticated by a non-Wix entity to login 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

Email address of the user to approve.

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?

Log a user in after 3rd party authentication

This example contains a backend function which uses a 3rd party authentication service to authenticate a user. If the authentication is successful, a session session token is returned to the client-side and used to log in the authenticated user.

Copy Code
1/*******************************
2 * Backend code - login.jsw *
3 *******************************/
4import wixUsersBackend from 'wix-users-backend';
5import { authBy3rdParty } from 'backend/authentications';
6
7export function getLoginToken(email, password) {
8 // authenticate using 3rd party
9 return authBy3rdParty(email, password)
10 .then((isAuthenticated) => {
11 // if authenticated generate and return session token
12 if (isAuthenticated) {
13 return wixUsersBackend.generateSessionToken(email)
14 .then((sessionToken) => {
15 return { "sessionToken": sessionToken, "approved": true };
16 });
17 }
18 // if not authenticated return non-approval
19 return { "approved": false };
20 });
21}
22
23/*********************************
24 * Client-side login code *
25 *********************************/
26import { getLoginToken } from 'backend/login';
27import wixUsers from 'wix-users';
28
29export async function button_onClick(event) {
30 // call backend function
31 getLoginToken($w("#email").value, $w("#password").value)
32 .then((loginResult) => {
33 // if approved log the user in with the session token
34 if (loginResult.approved) {
35 wixUsers.applySessionToken(loginResult.sessionToken);
36 }
37 // if not approved log a message
38 else {
39 console.log("User not approved.");
40 }
41 });
42}
43