signUp method
Implementation
@override
Future<User?> signUp(String email, String name, String password) async {
try {
final response = await http.post(
Uri.parse('${ApiConfig.baseUrl}/register'),
headers: ApiConfig.headers,
body: jsonEncode({
'email': email,
'name': name,
'password': password,
'confirm_password': password,
}),
);
switch (response.statusCode) {
case 201:
final responseData = jsonDecode(response.body)['data'];
final user = User(
id: responseData['id'],
name: responseData['name'],
email: responseData['email'],
credits: responseData['credits'],
);
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.');
}
} on SocketException {
throw Failure('Please check your internet connection 😑');
} on FormatException {
throw Failure('Bad response format 👎');
} on HttpException {
throw Failure('Please check your internet connection');
} catch (e) {
throw ApiException('Error signing up: ${e.toString()}');
}
}