registerUser function
Implementation
Future<void> registerUser(String username, String email, String password,
BuildContext context) async {
Dio dio = Dio();
final String authUrl = Configuration.AuthUrl;
final String url = '$authUrl/register';
try {
final response = await dio.post(
url,
data: {
'username': username,
'email': email,
'password': password,
},
);
if (response.statusCode == 200) {
final String qrCodeDataURL = response.data['qrCodeDataURL'];
final String accessToken = response.data['accessToken'];
if (qrCodeDataURL.isNotEmpty && accessToken.isNotEmpty) {
_showQrCodePopup(context, qrCodeDataURL, accessToken);
} else {
throw 'Invalid QR code or access token';
}
} else {
throw response.data['message'] ?? 'Registration failed';
}
} on DioException catch (e) {
String errorMessage = 'Error during registration';
if (e.response != null && e.response?.data != null) {
errorMessage = e.response?.data['message'] ?? errorMessage;
}
throw errorMessage; // Throw only the error message
} catch (e) {
throw 'Error during registration'; // Catch any other unknown errors
}
}