signIn method

  1. @override
Future signIn(
  1. String email,
  2. String password
)

Implementation

@override
Future signIn(String email, String password) async {
  try {
    final response = await http.post(
      Uri.parse('${ApiConfig.baseUrl}/login'),
      headers: ApiConfig.headers,
      body: jsonEncode({
        'email': email,
        'password': password,
      }),
    );
    switch (response.statusCode) {
      case 200:
        final responseData = jsonDecode(response.body)['data'];
        final user = User(
          id: responseData['id'],
          name: responseData['name'],
          email: responseData['email'],
        );
        return user;

      case 400:
        throw Failure('Invalid input data.');

      case 405:
        throw Failure(
            'The HTTP method used is not allowed for this endpoint.');

      case 413:
        throw Failure('The request body is too long');

      case 422:
        throw Failure(
            'The server cannot process the request due to invalid data.');

      case 429:
        throw Failure('Rate limit exceeded. Please try again later.');

      default:
        throw Failure('Unknown error occurred.');
    }
  } catch (e) {
    throw Failure(e.toString());
  }
}