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