signIn method

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

Implementation

@override
Future signIn(String email, String password) async {
  final pref = await SharedPreferences.getInstance();

  try {
    final response = await http.post(Uri.parse('${ApiConfig.baseUrl}/login'),
        headers: ApiConfig.headers,
        body: jsonEncode({
          'email': email,
          'password': password,
        }));
    var resCookie = response.headers['set-cookie'];
    pref.setString('cookie', resCookie!);

    switch (response.statusCode) {
      case 200:
        final responseData = jsonDecode(response.body)['data'];
        final user = User(
          id: responseData['id'],
          name: responseData['name'],
          email: responseData['email'],
          cookie: response.headers['set-cookie'],
          credits: responseData['credits'],
        );
        return user;

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

       case 401:
        throw Failure('Incorrect password.');

      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());
  }
}