login method
Authenticate customer with email and password.
Attempts to log in a customer using their email and password credentials. Upon successful authentication, the customer token is automatically stored for subsequent authenticated requests.
email the customer's email address
password the customer's password
Returns an AuthResponse containing the authentication token and customer data. Throws an exception if authentication fails.
Implementation
Future<AuthResponse> login({
required String email,
required String password,
}) async {
try {
final response = await _client.guestRequest<Map<String, dynamic>>(
'/rest/V1/integration/customer/token',
data: {'username': email, 'password': password},
);
if (response.statusCode == 200) {
final authResponse = AuthResponse.fromJson(response.data!);
// Store tokens
await _client.storeTokens(
accessToken: authResponse.accessToken,
refreshToken: authResponse.refreshToken,
customerId: authResponse.customer.id,
);
return authResponse;
} else {
throw Exception('Login failed: ${response.statusMessage}');
}
} on DioException catch (e) {
if (e.response?.statusCode == 401) {
throw Exception('Invalid email or password');
}
throw Exception('Login failed: ${e.message}');
} catch (e) {
throw Exception('Login failed: $e');
}
}