feat: add authentication support with APIAuthType and APIAuthModel

- Introduced APIAuthType enum for various authentication methods.
- Created APIAuthModel with basic authentication fields.
- Updated RequestModel to include authType and authData.
- Implemented EditAuthType widget for selecting and managing authentication.
- Enhanced CollectionStateNotifier to handle authentication updates.

:wq
This commit is contained in:
Udhay-Adithya
2025-06-07 15:30:20 +05:30
parent e5b22a24fc
commit 171a118d38
15 changed files with 700 additions and 48 deletions

View File

@@ -0,0 +1,106 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash_core/apidash_core.dart';
import 'package:apidash/providers/providers.dart';
class EditAuthType extends ConsumerWidget {
const EditAuthType({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final selectedRequest = ref.watch(selectedRequestModelProvider);
if (selectedRequest == null) {
return const SizedBox.shrink();
}
final currentAuthType = selectedRequest.authType;
final currentAuthData = selectedRequest.authData;
return Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Auth Type Dropdown
DropdownButtonFormField<APIAuthType>(
value: currentAuthType,
decoration: const InputDecoration(
labelText: 'Authentication Type',
border: OutlineInputBorder(),
),
items: APIAuthType.values.map((type) {
return DropdownMenuItem(
value: type,
child: Text(type.name),
);
}).toList(),
onChanged: (APIAuthType? newType) {
if (newType != null) {
ref.read(collectionStateNotifierProvider.notifier).update(
authType: newType,
authData: null, // reset when auth type changes
);
}
},
),
const SizedBox(height: 16),
// Dynamic Auth Input Fields
_buildAuthFields(ref, currentAuthType, currentAuthData),
],
),
);
}
Widget _buildAuthFields(
WidgetRef ref,
APIAuthType authType,
APIAuthModel? authData,
) {
final controllerMap = <String, TextEditingController>{};
void updateAuth(APIAuthModel model) {
ref.read(collectionStateNotifierProvider.notifier).update(
authData: model,
);
}
switch (authType) {
case APIAuthType.basic:
final usernameController = TextEditingController(
text: (authData is BasicAuth) ? authData.username : '',
);
final passwordController = TextEditingController(
text: (authData is BasicAuth) ? authData.password : '',
);
return Column(
children: [
TextField(
controller: usernameController,
decoration: const InputDecoration(labelText: 'Username'),
onChanged: (value) => updateAuth(BasicAuth(
username: value, password: passwordController.text)),
),
const SizedBox(height: 8),
TextField(
controller: passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
onChanged: (value) => updateAuth(BasicAuth(
username: usernameController.text, password: value)),
),
],
);
//
case APIAuthType.none:
return const Text("No authentication selected.");
// TODO: Implement digest, oauth1, oauth2 later
default:
return const Text("This auth type is not implemented yet.");
}
}
}

View File

@@ -1,4 +1,6 @@
import 'package:apidash/consts.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart';
import 'package:apidash_core/apidash_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
@@ -28,11 +30,9 @@ class EditRestRequestPane extends ConsumerWidget {
.select((value) => value?.httpRequestModel?.hasBody)) ??
false;
final scriptsLength = ref.watch(selectedRequestModelProvider
.select((value) => value?.preRequestScript?.length)) ??
ref.watch(selectedRequestModelProvider
.select((value) => value?.postRequestScript?.length)) ??
0;
final hasAuth = ref.watch(
selectedRequestModelProvider.select((value) => value?.authType != APIAuthType.none));
false;
return RequestPane(
selectedId: selectedId,
@@ -51,19 +51,19 @@ class EditRestRequestPane extends ConsumerWidget {
paramLength > 0,
headerLength > 0,
hasBody,
scriptsLength > 0,
hasAuth
],
tabLabels: const [
kLabelURLParams,
kLabelHeaders,
kLabelBody,
kLabelScripts,
kLabelAuth,
],
children: const [
EditRequestURLParams(),
EditRequestHeaders(),
EditRequestBody(),
EditRequestScripts(),
EditAuthType(),
],
);
}