Skip to content

Commit ee5361e

Browse files
committed
Add optional API key field for ollama.com cloud authentication
Fixes #18
1 parent c4d0d1f commit ee5361e

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

lib/Pages/settings_page/subwidgets/server_settings.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class _ServerSettingsState extends State<ServerSettings> {
2424
final _settingsBox = Hive.box('settings');
2525

2626
final _serverAddressController = TextEditingController();
27+
final _apiKeyController = TextEditingController();
28+
final _apiKeyFocusNode = FocusNode();
2729

30+
bool _apiKeyObscured = true;
2831
OllamaRequestState _requestState = OllamaRequestState.uninitialized;
2932
get _isLoading => _requestState == OllamaRequestState.loading;
3033

@@ -34,21 +37,36 @@ class _ServerSettingsState extends State<ServerSettings> {
3437
void initState() {
3538
super.initState();
3639

40+
_apiKeyFocusNode.addListener(() {
41+
if (_apiKeyFocusNode.hasFocus) {
42+
setState(() => _apiKeyObscured = false);
43+
} else {
44+
setState(() => _apiKeyObscured = true);
45+
}
46+
});
47+
3748
_initialize();
3849
}
3950

4051
_initialize() {
4152
final serverAddress = _settingsBox.get('serverAddress');
53+
final apiKey = _settingsBox.get('apiKey');
4254

4355
if (serverAddress != null) {
4456
_serverAddressController.text = serverAddress;
4557
_handleConnectButton();
4658
}
59+
60+
if (apiKey != null) {
61+
_apiKeyController.text = apiKey;
62+
}
4763
}
4864

4965
@override
5066
void dispose() {
5167
_serverAddressController.dispose();
68+
_apiKeyController.dispose();
69+
_apiKeyFocusNode.dispose();
5270

5371
super.dispose();
5472
}
@@ -89,6 +107,23 @@ class _ServerSettingsState extends State<ServerSettings> {
89107
},
90108
),
91109
const SizedBox(height: 16),
110+
TextField(
111+
controller: _apiKeyController,
112+
focusNode: _apiKeyFocusNode,
113+
keyboardType: TextInputType.text,
114+
obscureText: _apiKeyObscured,
115+
onChanged: (value) {
116+
_settingsBox.put('apiKey', value);
117+
},
118+
decoration: InputDecoration(
119+
labelText: 'API key',
120+
border: OutlineInputBorder(),
121+
),
122+
onTapOutside: (PointerDownEvent event) {
123+
FocusManager.instance.primaryFocus?.unfocus();
124+
},
125+
),
126+
const SizedBox(height: 16),
92127
SizedBox(
93128
width: double.infinity,
94129
child: Wrap(

lib/Providers/chat_provider.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,11 @@ class ChatProvider extends ChangeNotifier {
390390
void _updateOllamaServiceAddress() {
391391
final settingsBox = Hive.box('settings');
392392
_ollamaService.baseUrl = settingsBox.get('serverAddress');
393+
_ollamaService.apiKey = settingsBox.get('apiKey');
393394

394-
settingsBox.listenable(keys: ["serverAddress"]).addListener(() {
395+
settingsBox.listenable(keys: ["serverAddress", "apiKey"]).addListener(() {
395396
_ollamaService.baseUrl = settingsBox.get('serverAddress');
397+
_ollamaService.apiKey = settingsBox.get('apiKey');
396398

397399
// This will update empty chat state to dismiss "Tap to configure server address" message
398400
notifyListeners();

lib/Services/ollama_service.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,20 @@ class OllamaService {
2222
String get baseUrl => _baseUrl;
2323
set baseUrl(String? value) => _baseUrl = value ?? "http://localhost:11434";
2424

25+
/// Optional API key for authentication with ollama.com cloud API.
26+
/// When set, requests will include an Authorization: Bearer header.
27+
String? _apiKey;
28+
String? get apiKey => _apiKey;
29+
set apiKey(String? value) => _apiKey = value?.isNotEmpty == true ? value : null;
30+
2531
/// The headers to include in all network requests.
26-
final headers = {'Content-Type': 'application/json'};
32+
Map<String, String> get headers {
33+
final h = {'Content-Type': 'application/json'};
34+
if (_apiKey != null) {
35+
h['Authorization'] = 'Bearer $_apiKey';
36+
}
37+
return h;
38+
}
2739

2840
/// Creates a new instance of the Ollama service.
2941
OllamaService({String? baseUrl}) : _baseUrl = baseUrl ?? "http://localhost:11434";

0 commit comments

Comments
 (0)