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 'auth_page.dart';
export 'basic_auth_fields.dart';
export 'bearer_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}';
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.";

View File

@@ -1,97 +1,44 @@
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(
ref.watch(
selectedRequestModelProvider.select((request) =>
request?.httpRequestModel?.authModel?.type ?? APIAuthType.none),
);
currentAuthData = selectedRequest.httpRequestModel?.authModel;
}
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<APIAuthType>(
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);
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
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) {
updateAuthData: (model) {
if (model == null) {
ref.read(collectionStateNotifierProvider.notifier).update(
authModel: AuthModel(type: APIAuthType.none),
@@ -100,47 +47,7 @@ class EditAuthType extends ConsumerWidget {
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.");
}
}
}