login(username: string, password: string): Promise<IRequestResponse<TLoginResponse>['data'] | null>¶
The login
is an asynchronous method that allows to log the users in giving them access to some parts of the application. This method is also responsible for handling validation messages after the process occurs.
Input parameters¶
username¶
username
is a mandatory parameter of the string
type which represents an email address of a user.
password¶
password
is a mandatory parameter of the string
type which represents a password of the user.
Returned value¶
A returned value has a type of Promise<IRequestResponse<TLoginResponse>['data'] | null>
. You can read more about IRequestResponse object here and about TLoginResponse here.
Event Bus events¶
This API method dispatches the following events with the Event Bus:
- FlashMessengerApi.addFlashMessage - when the user is already logged in while logging in
- authentication.authorized - after the user has been authorized
- authentication.loggedIn - when the user logs in or has already been logged in while firing this method
- authentication.failedLogin - when the user has been unable to log in
This API method also listens to the following events:
- PageManager.loaded - to show flash messages after a page visit
Example¶
In this example we make a AuthenticationApi
call to log the user in.
useStorefront(async (storefront) => {
const authenticationApi = await storefront.getApi('AuthenticationApi');
await authenticationApi.login('email@example.com', 'test123');
});
Authentication API methods reference¶
- selectAuthStatus$
- getAuthStatus
- selectIsAuthorized$
- isAuthorized
- logout
- resetPassword
- setLoginEmail
- getLoginEmail
- selectLoginEmail$
- register
- getAdditionalFieldsForRegistration
- openLoginModal
- closeLoginModal
- openRegisterModal
- closeRegisterModal
- openRemindPasswordModal
- closeRemindPasswordModal