From 972d8be6b03ed715fd93cde47ace36f00e482cb0 Mon Sep 17 00:00:00 2001 From: Ankit Mahato Date: Mon, 14 Jul 2025 01:01:21 +0530 Subject: [PATCH] Add AuthPage --- lib/screens/common_widgets/auth/auth.dart | 1 + .../common_widgets/auth/auth_page.dart | 89 ++++++++++ lib/screens/common_widgets/auth/consts.dart | 9 + .../request_pane/request_auth.dart | 161 ++++-------------- 4 files changed, 133 insertions(+), 127 deletions(-) create mode 100644 lib/screens/common_widgets/auth/auth_page.dart diff --git a/lib/screens/common_widgets/auth/auth.dart b/lib/screens/common_widgets/auth/auth.dart index 376feb90..0ea643fd 100644 --- a/lib/screens/common_widgets/auth/auth.dart +++ b/lib/screens/common_widgets/auth/auth.dart @@ -1,4 +1,5 @@ export 'api_key_auth_fields.dart'; +export 'auth_page.dart'; export 'basic_auth_fields.dart'; export 'bearer_auth_fields.dart'; export 'digest_auth_fields.dart'; diff --git a/lib/screens/common_widgets/auth/auth_page.dart b/lib/screens/common_widgets/auth/auth_page.dart new file mode 100644 index 00000000..e34b9f06 --- /dev/null +++ b/lib/screens/common_widgets/auth/auth_page.dart @@ -0,0 +1,89 @@ +import 'package:apidash_design_system/apidash_design_system.dart'; +import 'package:flutter/material.dart'; +import 'package:apidash_core/apidash_core.dart'; +import 'api_key_auth_fields.dart'; +import 'basic_auth_fields.dart'; +import 'bearer_auth_fields.dart'; +import 'digest_auth_fields.dart'; +import 'jwt_auth_fields.dart'; +import 'consts.dart'; + +class AuthPage extends StatelessWidget { + final AuthModel? authModel; + final bool readOnly; + final Function(APIAuthType? newType)? onChangedAuthType; + final Function(AuthModel? model)? updateAuthData; + + const AuthPage({ + super.key, + this.authModel, + this.readOnly = false, + this.onChangedAuthType, + this.updateAuthData, + }); + + @override + Widget build(BuildContext context) { + return SingleChildScrollView( + child: Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + kLabelSelectAuthType, + style: TextStyle( + fontWeight: FontWeight.bold, + ), + ), + SizedBox( + height: 8, + ), + ADPopupMenu( + value: authModel?.type.displayType, + values: APIAuthType.values + .map((type) => (type, type.displayType)) + .toList(), + tooltip: kTooltipSelectAuth, + isOutlined: true, + onChanged: readOnly ? null : onChangedAuthType, + ), + const SizedBox(height: 48), + switch (authModel?.type) { + APIAuthType.basic => BasicAuthFields( + readOnly: readOnly, + authData: authModel, + updateAuth: updateAuthData, + ), + APIAuthType.bearer => BearerAuthFields( + readOnly: readOnly, + authData: authModel, + updateAuth: updateAuthData, + ), + APIAuthType.apiKey => ApiKeyAuthFields( + readOnly: readOnly, + authData: authModel, + updateAuth: updateAuthData, + ), + APIAuthType.jwt => JwtAuthFields( + readOnly: readOnly, + authData: authModel, + updateAuth: updateAuthData, + ), + APIAuthType.digest => DigestAuthFields( + readOnly: readOnly, + authData: authModel, + updateAuth: updateAuthData, + ), + APIAuthType.none => + Text(readOnly ? kMsgNoAuth : kMsgNoAuthSelected), + _ => Text(readOnly + ? "${authModel?.type.name} $kMsgAuthNotSupported" + : kMsgNotImplemented), + } + ], + ), + ), + ); + } +} diff --git a/lib/screens/common_widgets/auth/consts.dart b/lib/screens/common_widgets/auth/consts.dart index 0670d642..479f0980 100644 --- a/lib/screens/common_widgets/auth/consts.dart +++ b/lib/screens/common_widgets/auth/consts.dart @@ -62,3 +62,12 @@ const kHintJson = '{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}'; const kHeaderPrefix = 'Bearer'; const kQueryParamKey = 'token'; + +//AuthPAge +const kLabelSelectAuthType = "Authentication Type"; +const kTooltipSelectAuth = "Select Authentication Type"; +const kMsgNoAuth = "No authentication was used for this request."; +const kMsgNoAuthSelected = "No authentication selected."; +const kMsgAuthNotSupported = + "authentication details are not yet supported in history view."; +const kMsgNotImplemented = "This auth type is not implemented yet."; diff --git a/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart b/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart index 0d73f099..daf0e0d1 100644 --- a/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart +++ b/lib/screens/home_page/editor_pane/details_card/request_pane/request_auth.dart @@ -1,146 +1,53 @@ -import 'package:apidash/screens/common_widgets/auth/api_key_auth_fields.dart'; -import 'package:apidash/screens/common_widgets/auth/basic_auth_fields.dart'; -import 'package:apidash/screens/common_widgets/auth/bearer_auth_fields.dart'; -import 'package:apidash/screens/common_widgets/auth/digest_auth_fields.dart'; -import 'package:apidash/screens/common_widgets/auth/jwt_auth_fields.dart'; -import 'package:apidash_design_system/widgets/popup_menu.dart'; 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'; +import '../../../../common_widgets/common_widgets.dart'; class EditAuthType extends ConsumerWidget { - final AuthModel? authModel; final bool readOnly; const EditAuthType({ super.key, - this.authModel, this.readOnly = false, }); @override Widget build(BuildContext context, WidgetRef ref) { - final AuthModel? currentAuthData; - final APIAuthType currentAuthType; - - if (authModel != null) { - currentAuthData = authModel; - currentAuthType = authModel!.type; - } else { - final selectedRequest = ref.read(selectedRequestModelProvider); - if (selectedRequest == null) { - return const SizedBox.shrink(); - } - - currentAuthType = ref.watch( - selectedRequestModelProvider.select((request) => - request?.httpRequestModel?.authModel?.type ?? APIAuthType.none), - ); - currentAuthData = selectedRequest.httpRequestModel?.authModel; + final selectedRequest = ref.read(selectedRequestModelProvider); + if (selectedRequest == null) { + return const SizedBox.shrink(); } - return SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.all(16.0), - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text( - "Authentication Type", - style: TextStyle( - fontWeight: FontWeight.bold, - ), - ), - SizedBox( - height: 8, - ), - ADPopupMenu( - value: currentAuthType.displayType, - values: APIAuthType.values - .map((type) => (type, type.displayType)) - .toList(), - tooltip: "Select Authentication Type", - isOutlined: true, - onChanged: readOnly - ? null - : (APIAuthType? newType) { - final selectedRequest = - ref.read(selectedRequestModelProvider); - if (newType != null) { - ref - .read(collectionStateNotifierProvider.notifier) - .update( - authModel: selectedRequest - ?.httpRequestModel?.authModel - ?.copyWith(type: newType) ?? - AuthModel(type: newType), - ); - } - }, - ), - const SizedBox(height: 48), - _buildAuthFields(context, ref, currentAuthData), - ], - ), - ), + + ref.watch( + selectedRequestModelProvider.select((request) => + request?.httpRequestModel?.authModel?.type ?? APIAuthType.none), + ); + final currentAuthData = selectedRequest.httpRequestModel?.authModel; + + return AuthPage( + authModel: currentAuthData, + readOnly: readOnly, + onChangedAuthType: (newType) { + final selectedRequest = ref.read(selectedRequestModelProvider); + if (newType != null) { + ref.read(collectionStateNotifierProvider.notifier).update( + authModel: selectedRequest?.httpRequestModel?.authModel + ?.copyWith(type: newType) ?? + AuthModel(type: newType), + ); + } + }, + updateAuthData: (model) { + if (model == null) { + ref.read(collectionStateNotifierProvider.notifier).update( + authModel: AuthModel(type: APIAuthType.none), + ); + } + ref.read(collectionStateNotifierProvider.notifier).update( + authModel: model, + ); + }, ); } - - Widget _buildAuthFields( - BuildContext context, - WidgetRef ref, - AuthModel? authData, - ) { - void updateAuth(AuthModel? model) { - if (model == null) { - ref.read(collectionStateNotifierProvider.notifier).update( - authModel: AuthModel(type: APIAuthType.none), - ); - } - ref.read(collectionStateNotifierProvider.notifier).update( - authModel: model, - ); - } - - switch (authData?.type) { - case APIAuthType.basic: - return BasicAuthFields( - readOnly: readOnly, - authData: authData, - updateAuth: updateAuth, - ); - case APIAuthType.bearer: - return BearerAuthFields( - readOnly: readOnly, - authData: authData, - updateAuth: updateAuth, - ); - case APIAuthType.apiKey: - return ApiKeyAuthFields( - readOnly: readOnly, - authData: authData, - updateAuth: updateAuth, - ); - case APIAuthType.jwt: - return JwtAuthFields( - readOnly: readOnly, - authData: authData, - updateAuth: updateAuth, - ); - case APIAuthType.digest: - return DigestAuthFields( - readOnly: readOnly, - authData: authData, - updateAuth: updateAuth, - ); - case APIAuthType.none: - return Text(readOnly - ? "No authentication was used for this request." - : "No authentication selected."); - default: - return Text(readOnly - ? "Authentication details for ${authData?.type.name} are not yet supported in history view." - : "This auth type is not implemented yet."); - } - } }