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 { Permissions, webMethod } from 'wix-web-module';2import { authentication } from 'wix-members-backend';34export 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});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.web.js *3 ***************************/45import { Permissions, webMethod } from 'wix-web-module';6import { authentication } from 'wix-members-backend';78export const getLoginToken = webMethod(Permissions.Anyone, async (email, password) => {910 let sessionToken;1112 try {13 sessionToken = await authentication.login(email, password);1415 // If the promise resolves, the member is authenticated and can be logged in16 return {17 sessionToken: sessionToken,18 approved: true19 };20 } catch (error) {21 // If the promise is rejected, the member is not authenticated22 // and cannot be logged in23 console.error(error);24 return {25 approved: false,26 error: error27 };28 }29});3031/*************32* Page code *33************/3435import { getLoginToken } from 'backend/login.web';36import { authentication } from 'wix-members-frontend';3738$w('#login').onClick(async () => {39 const email = $w('#email').value;40 const password = $w('#password').value4142 // Call the backend function to get the session token43 const loginResult = await getLoginToken(email, password);4445 if (loginResult.approved) {46 // If approved, log the member in using the returned session token47 authentication.applySessionToken(loginResult.sessionToken);48 } else {49 // If not approved, log a message50 console.error('Login not approved.');51 }52});