fix: Implement better error messaging for providers token check

- Resolves #508
This commit is contained in:
NaiJi 2024-08-07 22:08:04 +04:00
parent d01df51296
commit b69e4ad7ff
10 changed files with 60 additions and 26 deletions

View file

@ -49,7 +49,7 @@ class CloudflareApi extends RestApiMap {
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
String? message;
final Dio client = await getClient();
try {
response = await client.get(
@ -77,9 +77,12 @@ class CloudflareApi extends RestApiMap {
);
}
message = response.statusMessage;
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
message = 'initializing.provider_bad_key_error';
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
@ -88,7 +91,7 @@ class CloudflareApi extends RestApiMap {
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
message: message,
);
}

View file

@ -49,7 +49,7 @@ class DesecApi extends RestApiMap {
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
String? message;
final Dio client = await getClient();
try {
response = await client.get(
@ -78,9 +78,12 @@ class DesecApi extends RestApiMap {
);
}
message = response.statusMessage;
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
message = 'initializing.provider_bad_key_error';
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
@ -89,7 +92,7 @@ class DesecApi extends RestApiMap {
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
message: message,
);
}

View file

@ -49,7 +49,7 @@ class DigitalOceanDnsApi extends RestApiMap {
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
String? message;
final Dio client = await getClient();
try {
response = await client.get(
@ -77,9 +77,12 @@ class DigitalOceanDnsApi extends RestApiMap {
);
}
message = response.statusMessage;
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
message = 'initializing.provider_bad_key_error';
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
@ -88,7 +91,7 @@ class DigitalOceanDnsApi extends RestApiMap {
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
message: message,
);
}

View file

@ -147,7 +147,7 @@ class DigitalOceanApi extends RestApiMap {
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
String? message;
final Dio client = await getClient();
try {
response = await client.get(
@ -175,9 +175,12 @@ class DigitalOceanApi extends RestApiMap {
);
}
message = response.statusMessage;
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
message = 'initializing.provider_bad_key_error';
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
@ -186,7 +189,7 @@ class DigitalOceanApi extends RestApiMap {
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
message: message,
);
}

View file

@ -195,7 +195,7 @@ class HetznerApi extends RestApiMap {
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
String? message;
final Dio client = await getClient();
try {
response = await client.get(
@ -223,9 +223,12 @@ class HetznerApi extends RestApiMap {
);
}
message = response.statusMessage;
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
message = 'initializing.provider_bad_key_error';
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
@ -234,7 +237,7 @@ class HetznerApi extends RestApiMap {
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
message: message,
);
}

View file

@ -41,7 +41,7 @@ class DnsProviderFormCubit extends FormCubit {
}
if (!isKeyValid) {
apiKey.setError('initializing.dns_provider_bad_key_error'.tr());
apiKey.setError('initializing.provider_bad_key_error'.tr());
}
return isKeyValid;

View file

@ -92,11 +92,17 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
if (!apiResponse.success) {
getIt<NavigationService>().showSnackBar(
'initializing.could_not_connect'.tr(),
apiResponse.message ?? 'initializing.could_not_connect'.tr(),
);
return null;
}
if (!apiResponse.data) {
getIt<NavigationService>().showSnackBar(
(apiResponse.message ?? 'initializing.provider_bad_key_error').tr(),
);
}
return apiResponse.data;
}
@ -110,11 +116,17 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
if (!apiResponse.success) {
getIt<NavigationService>().showSnackBar(
'initializing.could_not_connect'.tr(),
apiResponse.message ?? 'initializing.could_not_connect'.tr(),
);
return null;
}
if (!apiResponse.data) {
getIt<NavigationService>().showSnackBar(
(apiResponse.message ?? 'initializing.provider_bad_key_error').tr(),
);
}
return apiResponse.data;
}
@ -800,11 +812,13 @@ class ServerInstallationCubit extends Cubit<ServerInstallationState> {
if (serverDomain == null) {
return;
}
final isTokenValid =
final isTokenValidResult =
await repository.validateDnsToken(token, serverDomain.domainName);
if (!isTokenValid) {
getIt<NavigationService>()
.showSnackBar('recovering.domain_not_available_on_token'.tr());
if (!isTokenValidResult.success) {
getIt<NavigationService>().showSnackBar(
isTokenValidResult.message ??
'recovering.domain_not_available_on_token'.tr(),
);
return;
}
final dnsProviderType = await ServerApi(

View file

@ -192,27 +192,34 @@ class ServerInstallationRepository {
return server;
}
Future<bool> validateDnsToken(
Future<GenericResult<bool>> validateDnsToken(
final String token,
final String domain,
) async {
final result =
await ProvidersController.currentDnsProvider!.tryInitApiByToken(token);
if (!result.success) {
return false;
return result;
}
await setDnsApiToken(token);
final domainResult =
await ProvidersController.currentDnsProvider!.domainList();
if (!domainResult.success || domainResult.data.isEmpty) {
return false;
return GenericResult(
success: false,
data: false,
message: domainResult.message,
);
}
await getIt<ResourcesModel>().removeDnsProviderToken(
getIt<ResourcesModel>().dnsProviderCredentials.first,
);
return domainResult.data.any(
(final serverDomain) => serverDomain.domainName == domain,
return GenericResult(
success: true,
data: domainResult.data.any(
(final serverDomain) => serverDomain.domainName == domain,
),
);
}

View file

@ -110,9 +110,8 @@ class ProviderInputDataPage extends StatelessWidget {
CubitFormTextField(
autofocus: true,
formFieldCubit: providerCubit.apiKey,
textAlign: TextAlign.center,
scrollPadding: const EdgeInsets.only(bottom: 70),
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Provider API Token',
),
),

View file

@ -118,9 +118,8 @@ class ProviderInputDataPage extends StatelessWidget {
CubitFormTextField(
autofocus: true,
formFieldCubit: providerCubit.apiKey,
textAlign: TextAlign.center,
scrollPadding: const EdgeInsets.only(bottom: 70),
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: 'Provider API Token',
),
),