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
Login email address.
Member password.
Returns
Return Type:
Was this helpful?
1import { authentication } from 'wix-members-backend';23export function myLoginFunction(email, password) {45 return authentication.login(email, password)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.eyJkYXRhIjoie1wiaWRcIjpcIjg4MzFlZWQ2LTkyOGUtNGY4NS1iODBhLWUxZTQ4ZmI3YzRmZFwiLFwiY29sbGVjdGlvbklkXCI6XCI5YmVjNThlNi02NDExLTQ5OTEtOGU1ZC0wYWRhOTE4MmI5NWVcIixcIm1ldGFTaXRlSWRcIjpcIjFmZjQ2YTk2LWRlYTYtNDlkYS04M2JhLTUxNjRmYjYyZDgzOVwiLFwib3duZXJcIjpmYWxzZSxcImNyZWF0aW9uVGltZVwiOjE2MjI0MTUxMTMyNjYsXCJleHBpcmVzSW5cIjoxMjA5NjAwMDAwLFwiZXhwaXJhdGlvblRpbWVcIjoxNjIyNDE1MjMzMjY2LFwibGFzdFJlZnJlc2hlZFwiOjAsXCJhZG1pblwiOmZhbHNlfSIsImlhdCI6MTYyMjQxNTExM30.CFJTkyDaF6LypH8UuNm74qgZMxTKFgB1ZnzsemhY_KY"16 */
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.
1/****************************2 * Backend code - login.jsw *3 ***************************/45import { authentication } from 'wix-members-backend';67export async function getLoginToken(email, password) {89 let sessionToken;1011 try {12 sessionToken = await authentication.login(email, password);1314 // If the promise resolves, the member is authenticated and can be logged in15 return {16 sessionToken: sessionToken,17 approved: true18 };19 } catch (error) {20 // If the promise is rejected, the member is not authenticated21 // and cannot be logged in22 console.error(error);23 return {24 approved: false,25 error: error26 };27 }28}2930/*************31* Page code *32************/3334import { getLoginToken } from 'backend/login';35import { authentication } from 'wix-members-frontend';3637$w('#login').onClick(async () => {38 const email = $w('#email').value;39 const password = $w('#password').value4041 // Call the backend function to get the session token42 const loginResult = await getLoginToken(email, password);4344 if (loginResult.approved) {45 // If approved, log the member in using the returned session token46 authentication.applySessionToken(loginResult.sessionToken);47 } else {48 // If not approved, log a message49 console.error('Login not approved.');50 }51});52