Add AuthPage

This commit is contained in:
Ankit Mahato
2025-07-14 01:01:21 +05:30
parent 394835187a
commit 972d8be6b0
4 changed files with 133 additions and 127 deletions

View File

@@ -1,4 +1,5 @@
export 'api_key_auth_fields.dart'; export 'api_key_auth_fields.dart';
export 'auth_page.dart';
export 'basic_auth_fields.dart'; export 'basic_auth_fields.dart';
export 'bearer_auth_fields.dart'; export 'bearer_auth_fields.dart';
export 'digest_auth_fields.dart'; export 'digest_auth_fields.dart';

View File

@@ -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<APIAuthType>(
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),
}
],
),
),
);
}
}

View File

@@ -62,3 +62,12 @@ const kHintJson =
'{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}'; '{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}';
const kHeaderPrefix = 'Bearer'; const kHeaderPrefix = 'Bearer';
const kQueryParamKey = 'token'; 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.";

View File

@@ -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/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash_core/apidash_core.dart'; import 'package:apidash_core/apidash_core.dart';
import 'package:apidash/providers/providers.dart'; import 'package:apidash/providers/providers.dart';
import '../../../../common_widgets/common_widgets.dart';
class EditAuthType extends ConsumerWidget { class EditAuthType extends ConsumerWidget {
final AuthModel? authModel;
final bool readOnly; final bool readOnly;
const EditAuthType({ const EditAuthType({
super.key, super.key,
this.authModel,
this.readOnly = false, this.readOnly = false,
}); });
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
final AuthModel? currentAuthData; final selectedRequest = ref.read(selectedRequestModelProvider);
final APIAuthType currentAuthType; if (selectedRequest == null) {
return const SizedBox.shrink();
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;
} }
return SingleChildScrollView(
child: Padding( ref.watch(
padding: const EdgeInsets.all(16.0), selectedRequestModelProvider.select((request) =>
child: Column( request?.httpRequestModel?.authModel?.type ?? APIAuthType.none),
crossAxisAlignment: CrossAxisAlignment.start, );
children: [ final currentAuthData = selectedRequest.httpRequestModel?.authModel;
Text(
"Authentication Type", return AuthPage(
style: TextStyle( authModel: currentAuthData,
fontWeight: FontWeight.bold, readOnly: readOnly,
), onChangedAuthType: (newType) {
), final selectedRequest = ref.read(selectedRequestModelProvider);
SizedBox( if (newType != null) {
height: 8, ref.read(collectionStateNotifierProvider.notifier).update(
), authModel: selectedRequest?.httpRequestModel?.authModel
ADPopupMenu<APIAuthType>( ?.copyWith(type: newType) ??
value: currentAuthType.displayType, AuthModel(type: newType),
values: APIAuthType.values );
.map((type) => (type, type.displayType)) }
.toList(), },
tooltip: "Select Authentication Type", updateAuthData: (model) {
isOutlined: true, if (model == null) {
onChanged: readOnly ref.read(collectionStateNotifierProvider.notifier).update(
? null authModel: AuthModel(type: APIAuthType.none),
: (APIAuthType? newType) { );
final selectedRequest = }
ref.read(selectedRequestModelProvider); ref.read(collectionStateNotifierProvider.notifier).update(
if (newType != null) { authModel: model,
ref );
.read(collectionStateNotifierProvider.notifier) },
.update(
authModel: selectedRequest
?.httpRequestModel?.authModel
?.copyWith(type: newType) ??
AuthModel(type: newType),
);
}
},
),
const SizedBox(height: 48),
_buildAuthFields(context, ref, currentAuthData),
],
),
),
); );
} }
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.");
}
}
} }