mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
refactor API Key auth
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:apidash/widgets/auth_textfield.dart';
|
|||||||
import 'package:apidash_core/apidash_core.dart';
|
import 'package:apidash_core/apidash_core.dart';
|
||||||
import 'package:apidash_design_system/apidash_design_system.dart';
|
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'consts.dart';
|
||||||
|
|
||||||
class ApiKeyAuthFields extends StatefulWidget {
|
class ApiKeyAuthFields extends StatefulWidget {
|
||||||
final AuthModel? authData;
|
final AuthModel? authData;
|
||||||
@@ -28,8 +29,9 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
|
|||||||
super.initState();
|
super.initState();
|
||||||
final apiAuth = widget.authData?.apikey;
|
final apiAuth = widget.authData?.apikey;
|
||||||
_keyController = TextEditingController(text: apiAuth?.key ?? '');
|
_keyController = TextEditingController(text: apiAuth?.key ?? '');
|
||||||
_nameController = TextEditingController(text: apiAuth?.name ?? 'x-api-key');
|
_nameController =
|
||||||
_addKeyTo = apiAuth?.location ?? 'header';
|
TextEditingController(text: apiAuth?.name ?? kApiKeyHeaderName);
|
||||||
|
_addKeyTo = apiAuth?.location ?? kAddToDefaultLocation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -38,7 +40,7 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
"Add to",
|
kLabelAddTo,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.normal,
|
fontWeight: FontWeight.normal,
|
||||||
fontSize: 14,
|
fontSize: 14,
|
||||||
@@ -48,12 +50,9 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
|
|||||||
height: 4,
|
height: 4,
|
||||||
),
|
),
|
||||||
ADPopupMenu<String>(
|
ADPopupMenu<String>(
|
||||||
value: _addKeyTo == 'header' ? 'Header' : 'Query Params',
|
value: kAddToLocationsMap[_addKeyTo],
|
||||||
values: const [
|
values: kAddToLocations,
|
||||||
('header', 'Header'),
|
tooltip: kTooltipApiKeyAuth,
|
||||||
('query', 'Query Params'),
|
|
||||||
],
|
|
||||||
tooltip: "Select where to add API key",
|
|
||||||
isOutlined: true,
|
isOutlined: true,
|
||||||
onChanged: widget.readOnly
|
onChanged: widget.readOnly
|
||||||
? null
|
? null
|
||||||
@@ -70,15 +69,15 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
|
|||||||
AuthTextField(
|
AuthTextField(
|
||||||
readOnly: widget.readOnly,
|
readOnly: widget.readOnly,
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
hintText: "Header/Query Param Name",
|
hintText: kHintTextFieldName,
|
||||||
onChanged: (value) => _updateApiKeyAuth(),
|
onChanged: (value) => _updateApiKeyAuth(),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
AuthTextField(
|
AuthTextField(
|
||||||
readOnly: widget.readOnly,
|
readOnly: widget.readOnly,
|
||||||
controller: _keyController,
|
controller: _keyController,
|
||||||
title: "API Key",
|
title: kLabelApiKey,
|
||||||
hintText: "Key",
|
hintText: kHintTextKey,
|
||||||
isObscureText: true,
|
isObscureText: true,
|
||||||
onChanged: (value) => _updateApiKeyAuth(),
|
onChanged: (value) => _updateApiKeyAuth(),
|
||||||
),
|
),
|
||||||
@@ -87,21 +86,18 @@ class _ApiKeyAuthFieldsState extends State<ApiKeyAuthFields> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _updateApiKeyAuth() {
|
void _updateApiKeyAuth() {
|
||||||
|
final apiKey = AuthApiKeyModel(
|
||||||
|
key: _keyController.text.trim(),
|
||||||
|
name: _nameController.text.trim(),
|
||||||
|
location: _addKeyTo,
|
||||||
|
);
|
||||||
widget.updateAuth(widget.authData?.copyWith(
|
widget.updateAuth(widget.authData?.copyWith(
|
||||||
type: APIAuthType.apiKey,
|
type: APIAuthType.apiKey,
|
||||||
apikey: AuthApiKeyModel(
|
apikey: apiKey,
|
||||||
key: _keyController.text.trim(),
|
|
||||||
name: _nameController.text.trim(),
|
|
||||||
location: _addKeyTo,
|
|
||||||
),
|
|
||||||
) ??
|
) ??
|
||||||
AuthModel(
|
AuthModel(
|
||||||
type: APIAuthType.apiKey,
|
type: APIAuthType.apiKey,
|
||||||
apikey: AuthApiKeyModel(
|
apikey: apiKey,
|
||||||
key: _keyController.text.trim(),
|
|
||||||
name: _nameController.text.trim(),
|
|
||||||
location: _addKeyTo,
|
|
||||||
),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
9
lib/screens/common_widgets/auth/consts.dart
Normal file
9
lib/screens/common_widgets/auth/consts.dart
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
const kApiKeyHeaderName = 'x-api-key';
|
||||||
|
const kAddToLocations = [
|
||||||
|
('header', 'Header'),
|
||||||
|
('query', 'Query Params'),
|
||||||
|
];
|
||||||
|
final kAddToDefaultLocation = kAddToLocations[0].$1;
|
||||||
|
final kAddToLocationsMap = {for (var v in kAddToLocations) v.$1: v.$2};
|
||||||
|
const kLabelAddTo = "Add to";
|
||||||
|
const kTooltipApiKeyAuth = "Select where to add API key";
|
||||||
Reference in New Issue
Block a user