Skip to content

Commit

Permalink
API token support #10
Browse files Browse the repository at this point in the history
  • Loading branch information
enm10k committed May 19, 2024
1 parent 1a96dc9 commit dbf1f50
Show file tree
Hide file tree
Showing 8 changed files with 241 additions and 199 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
fix:
dart fix --apply lib

fmt:
dart format lib

build-runner-watch watch:
# flutter pub run build_runner build -d -v
flutter pub run build_runner watch -d -v
Expand Down Expand Up @@ -33,4 +36,4 @@ cloc:
cloc . --vcs=git --include-ext=dart,yaml lib

run-web:
CHROME_EXECUTABLE="./scripts/google-chrome-unsafe.sh" flutter run -d chrome
CHROME_EXECUTABLE="./scripts/google-chrome-unsafe.sh" flutter run -d chrome
6 changes: 3 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ linter:
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
enable-experiment:
- records
- patterns
# enable-experiment:
# - records
# - patterns
exclude:
- "**/*.freezed.dart"
- "**/*.g.dart"
Expand Down
67 changes: 53 additions & 14 deletions lib/common/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,75 @@ import 'dart:io';

import 'package:flutter/foundation.dart';

import 'logger.dart';
import 'preferences.dart';
import '../nocodb_sdk/client.dart';

final settings = Settings();

class RememberedData {
final String host;
final Token token;

RememberedData(this.host, this.token);
}

class Settings {
Preferences? prefs;
bool get initialized => prefs != null;
init(Preferences prefs) {
this.prefs = prefs;
}

Future<void> remember({
required String host,
required Token token,
String? email,
}) async {
await clear();
await prefs?.set(key: _apiBaseUrl, value: host);
await prefs?.set(
key: _rememberMe,
value: true,
);

switch (token) {
case AuthToken(authToken: final authToken):
await prefs?.set(key: _authToken, value: authToken, secure: true);
break;
case ApiToken(apiToken: final apiToken):
await prefs?.set(key: _apiToken, value: apiToken, secure: true);
break;
default:
throw Exception('unsupported token type: ${token.runtimeType}');
}
}

Future<RememberedData?> getRemembered() async {
final host = await apiBaseUrl;
if (host == null) {
return null;
}

final authToken = await prefs?.getSecure(key: _authToken);
if (authToken != null) {
return RememberedData(host, AuthToken(authToken));
}

final apiToken = await prefs?.getSecure(key: _apiToken);
if (apiToken != null) {
logger.finest('B');
return RememberedData(host, ApiToken(apiToken));
}
return null;
}

static const _authToken = 'authToken';
Future<String?> get authToken async =>
await prefs?.getSecure(key: _authToken);

Future<void> setAuthToken(String v) async =>
await prefs?.set(key: _authToken, value: v, secure: true);
static const _apiToken = 'apiToken';

static const _email = 'email';
Future<String?> get email async => await prefs?.get(key: _email);

Future<void> setEmail(String v) async => await prefs?.set(
key: _email,
value: v,
Expand All @@ -37,16 +85,7 @@ class Settings {
return v;
}

Future<void> setApiBaseUrl(String v) async =>
await prefs?.set(key: _apiBaseUrl, value: v);

static const _rememberMe = 'rememberMe';
Future<bool> get rememberMe async =>
await prefs?.get<bool>(key: _rememberMe) ?? false;
Future<void> setRememberMe(bool v) async => await prefs?.set(
key: _rememberMe,
value: v,
);

Future<void> clear() async {
await prefs?.clear();
Expand Down
4 changes: 1 addition & 3 deletions lib/features/core/components/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ class Cell {
case UITypes.linkToAnotherRecord:
if (value != null) {
assert(
column.isBelongsTo
? value is Map<String, dynamic>
: value is int,
column.isBelongsTo ? value is Map<String, dynamic> : value is int,
);
}

Expand Down
Loading

0 comments on commit dbf1f50

Please sign in to comment.