Add apikey ref + needed to test if ok get apikey after pin code
This commit is contained in:
parent
30d88df7b6
commit
da6ff5177e
@ -17,6 +17,7 @@ class DatabaseHelper {
|
||||
static final columnConfiguration = 'configuration';
|
||||
static final columnLanguage = 'language';
|
||||
static final columnInstanceId = 'instanceId';
|
||||
static final columnApiKey = 'apiKey';
|
||||
|
||||
DatabaseHelper._privateConstructor();
|
||||
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
|
||||
@ -85,6 +86,13 @@ class DatabaseHelper {
|
||||
|
||||
Future<TabletAppContext?> getData() async {
|
||||
TabletAppContext? tabletAppContext;
|
||||
final db = await instance.database;
|
||||
|
||||
// Migrate: add apiKey column if missing
|
||||
final columns = await db.rawQuery("PRAGMA table_info($table)");
|
||||
if (columns.where((c) => c['name'] == columnApiKey).isEmpty) {
|
||||
await db.rawQuery("ALTER TABLE $table ADD $columnApiKey TEXT");
|
||||
}
|
||||
|
||||
await DatabaseHelper.instance.queryAllRows().then((value) {
|
||||
value.forEach((element) {
|
||||
@ -99,7 +107,8 @@ class DatabaseHelper {
|
||||
deviceId: element["deviceId"],
|
||||
host: element["host"],
|
||||
configuration: configuration,
|
||||
language: element["language"]
|
||||
language: element["language"],
|
||||
apiKey: element["apiKey"] as String?,
|
||||
);
|
||||
});
|
||||
}).catchError((error) {
|
||||
|
||||
@ -17,12 +17,13 @@ class TabletAppContext with ChangeNotifier{
|
||||
String? language;
|
||||
String? deviceId;
|
||||
String? instanceId;
|
||||
String? apiKey;
|
||||
Size? puzzleSize;
|
||||
String? localPath;
|
||||
ApplicationInstanceDTO? applicationInstanceDTO;
|
||||
StatisticsService? statisticsService;
|
||||
|
||||
TabletAppContext({this.id, this.deviceId, this.host, this.configuration, this.language, this.instanceId, this.clientAPI});
|
||||
TabletAppContext({this.id, this.deviceId, this.host, this.configuration, this.language, this.instanceId, this.apiKey, this.clientAPI});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
@ -31,7 +32,8 @@ class TabletAppContext with ChangeNotifier{
|
||||
'host': host,
|
||||
'configuration': configuration == null ? null : jsonEncode(configuration!.toJson()),
|
||||
'language': language,
|
||||
'instanceId': instanceId
|
||||
'instanceId': instanceId,
|
||||
'apiKey': apiKey,
|
||||
};
|
||||
}
|
||||
|
||||
@ -42,7 +44,8 @@ class TabletAppContext with ChangeNotifier{
|
||||
host: json['host'] as String,
|
||||
configuration: json['configuration'] == null ? null : ConfigurationDTO.fromJson(json['configuration']),
|
||||
language: json['language'] as String,
|
||||
instanceId: json['instanceId'] as String
|
||||
instanceId: json['instanceId'] as String,
|
||||
apiKey: json['apiKey'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -23,7 +23,9 @@ import 'package:tablet_app/Screens/MainView/main_view.dart';
|
||||
import 'package:tablet_app/app_context.dart';
|
||||
import 'package:tablet_app/client.dart';
|
||||
import 'package:tablet_app/constants.dart';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
|
||||
class ConfigViewWidget extends StatefulWidget {
|
||||
@ -219,12 +221,16 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
fontSize: 16.0
|
||||
);
|
||||
|
||||
final apiKey = await _fetchAppKey(url, pinCode!);
|
||||
final authenticatedClient = Client(url, apiKey: apiKey);
|
||||
|
||||
TabletAppContext? tabletAppContext = appContext.getContext();
|
||||
if(tabletAppContext == null) {
|
||||
tabletAppContext = new TabletAppContext(host: url, instanceId: instance.id);
|
||||
}
|
||||
tabletAppContext.instanceId = instance.id;
|
||||
tabletAppContext.clientAPI = client;
|
||||
tabletAppContext.apiKey = apiKey;
|
||||
tabletAppContext.clientAPI = authenticatedClient;
|
||||
var identifier = kIsWeb ? "WEB TEST" : await DeviceInfoHelper.getDeviceDetails();
|
||||
|
||||
if(kIsWeb) {
|
||||
@ -329,6 +335,17 @@ class _ConfigViewWidget extends State<ConfigViewWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String?> _fetchAppKey(String baseUrl, String pinCode) async {
|
||||
try {
|
||||
final resp = await http.get(Uri.parse(
|
||||
'$baseUrl/api/instance/app-key?pinCode=$pinCode&appType=TabletApp'));
|
||||
if (resp.statusCode == 200) return jsonDecode(resp.body)['key'] as String?;
|
||||
} catch (ex) {
|
||||
print(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<InstanceDTO?> getInstanceIdByPinCode(Client client, String pinCode) async {
|
||||
try {
|
||||
var instance = await client.instanceApi!.instanceGetInstanceByPinCode(pinCode: pinCode);
|
||||
|
||||
@ -32,10 +32,9 @@ class Client {
|
||||
StatsApi? _statsApi;
|
||||
StatsApi? get statsApi => _statsApi;
|
||||
|
||||
Client(String path) {
|
||||
_apiClient = ApiClient(
|
||||
basePath: path); // "http://192.168.31.96"
|
||||
//basePath: "https://localhost:44339");
|
||||
Client(String path, {String? apiKey}) {
|
||||
_apiClient = ApiClient(basePath: path);
|
||||
if (apiKey != null) _apiClient!.addDefaultHeader('X-Api-Key', apiKey);
|
||||
_authenticationApi = AuthenticationApi(_apiClient);
|
||||
_instanceApi = InstanceApi(_apiClient);
|
||||
_userApi = UserApi(_apiClient);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user