Search.../

login( )

Logs a registered member in with an email and password.

Description

The login() function returns a Promise that resolves to a session token used to log a member in to your site.

The login() function only works with existing members. To register a new member use the register() function.

To complete the login, the returned session token must be applied using the applySessionToken() function (from the wix-members-frontend API) in page code.

Note: This function replaces the deprecated wix-users-backend.login(). 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.

Syntax

function login(email: string, password: string): Promise<string>

login Parameters

NAME
TYPE
DESCRIPTION
email
string

Login email address.

password
string

Member password.

Returns

Return Type:

Promise<string>

Was this helpful?

Generate a session token if a valid email and password are provided

Copy Code
1import { authentication } from 'wix-members-backend';
2
3export function myLoginFunction(email, password) {
4
5 return authentication.login(email, password)
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.eyJkYXRhIjoie1wiaWRcIjpcIjg4MzFlZWQ2LTkyOGUtNGY4NS1iODBhLWUxZTQ4ZmI3YzRmZFwiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2MjI0MTUxMTMyNjYsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjIyNDE1MjMzMjY2LFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyMjQxNTExM30.CFJTkyDaF6LypH8UuNm74qgZMxTKFgB1ZnzsemhY_KY"
16 */
Log a member in after they provide an email and password

This example contains a backend function that authenticates a member based on the provided email and password. If the authentication is successful, a session token is returned to the page and used to log the authenticated member in.

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