diff --git a/README-localStorage-token.md b/README-localStorage-token.md new file mode 100644 index 0000000..6b7b21a --- /dev/null +++ b/README-localStorage-token.md @@ -0,0 +1,34 @@ +# lib/provider/local_auth_provider.dart +provider for accessing current token +```dart +void redirectToHome() { + html.window.location.href = "/one-ui/"; +} + +final localAuthProvider = Provider((ref) { + try { + String? user = getUser(); + String? token = getToken(); + if (user != null) { + final localUser = OneLocalUserModel.fromJson(jsonDecode(user)); + localUser.token = token; + return localUser; + } + } catch (_) {} + return null; +}); +``` + +```dart +import 'package:js/js.dart'; +// ignore: avoid_web_libraries_in_flutter +import 'dart:html' as html; + +String? getToken() { + return html.window.localStorage['token']; +} +String? getUser() { + return html.window.localStorage['user']; +} +``` + diff --git a/lib/js/initial_route.dart b/lib/js/initial_route.dart index c200a4b..df01629 100644 --- a/lib/js/initial_route.dart +++ b/lib/js/initial_route.dart @@ -1,4 +1,14 @@ import 'package:js/js.dart'; +// ignore: avoid_web_libraries_in_flutter +import 'dart:html' as html; @JS('fx_initial_route') external String fxInitialRoute(); + +String? getToken() { + return html.window.localStorage['token']; +} + +String? getUser() { + return html.window.localStorage['user']; +} diff --git a/lib/main.dart b/lib/main.dart index 9f8dc0b..b8af549 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,12 @@ +import 'dart:convert'; + import 'package:flutter/material.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'app/constants.dart'; import 'app/route.dart'; import 'js/initial_route.dart'; +import 'model/one_user_model.dart'; +import 'provider/local_auth_provider.dart'; import 'provider/title_provider.dart'; void main() { @@ -15,13 +19,13 @@ void main() { class MyApp extends HookConsumerWidget { const MyApp({Key? key}) : super(key: key); - @override Widget build(BuildContext context, WidgetRef ref) { String initialRoute = homeRoute; try { initialRoute = fxInitialRoute(); } catch (_) {} + return MaterialApp( title: ref.watch(webTitleProvider), debugShowCheckedModeBanner: false, diff --git a/lib/model/one_user_model.dart b/lib/model/one_user_model.dart new file mode 100644 index 0000000..b21e8ea --- /dev/null +++ b/lib/model/one_user_model.dart @@ -0,0 +1,58 @@ +class OneLocalUserModel { + late String mUserID; + late String mUserUsername; + late String mUserGroupDashboard; + late String mUserDefaultTSampleStationID; + late String mStaffName; + late String isCourier; + late String ip; + late String agent; + late String version; + late String lastLogin; + late int? mSatelliteID; + String? token; + + OneLocalUserModel({ + required this.mUserID, + required this.mUserUsername, + required this.mUserGroupDashboard, + required this.mUserDefaultTSampleStationID, + required this.mStaffName, + required this.isCourier, + required this.ip, + required this.agent, + required this.version, + required this.lastLogin, + this.mSatelliteID, + }); + + OneLocalUserModel.fromJson(Map json) { + mUserID = json['M_UserID']; + mUserUsername = json['M_UserUsername']; + mUserGroupDashboard = json['M_UserGroupDashboard']; + mUserDefaultTSampleStationID = json['M_UserDefaultT_SampleStationID']; + mStaffName = json['M_StaffName']; + isCourier = json['is_courier']; + ip = json['ip']; + agent = json['agent']; + version = json['version']; + lastLogin = json['last-login']; + mSatelliteID = json['M_SatelliteID']; + } + + Map toJson() { + final Map data = {}; + data['M_UserID'] = mUserID; + data['M_UserUsername'] = mUserUsername; + data['M_UserGroupDashboard'] = mUserGroupDashboard; + data['M_UserDefaultT_SampleStationID'] = mUserDefaultTSampleStationID; + data['M_StaffName'] = mStaffName; + data['is_courier'] = isCourier; + data['ip'] = ip; + data['agent'] = agent; + data['version'] = version; + data['last-login'] = lastLogin; + data['M_SatelliteID'] = mSatelliteID; + return data; + } +} diff --git a/lib/provider/local_auth_provider.dart b/lib/provider/local_auth_provider.dart new file mode 100644 index 0000000..e821155 --- /dev/null +++ b/lib/provider/local_auth_provider.dart @@ -0,0 +1,25 @@ +import 'dart:convert'; + +import 'package:flutter_riverpod/flutter_riverpod.dart'; + +import '../js/initial_route.dart'; +import '../model/one_user_model.dart'; +// ignore: avoid_web_libraries_in_flutter +import 'dart:html' as html; + +void redirectToHome() { + html.window.location.href = "/one-ui/"; +} + +final localAuthProvider = Provider((ref) { + try { + String? user = getUser(); + String? token = getToken(); + if (user != null) { + final localUser = OneLocalUserModel.fromJson(jsonDecode(user)); + localUser.token = token; + return localUser; + } + } catch (_) {} + return null; +}); diff --git a/lib/screen/md_lab_mitra/md_lab_mitra_screen.dart b/lib/screen/md_lab_mitra/md_lab_mitra_screen.dart index 2ca5b4a..f907b92 100644 --- a/lib/screen/md_lab_mitra/md_lab_mitra_screen.dart +++ b/lib/screen/md_lab_mitra/md_lab_mitra_screen.dart @@ -1,11 +1,21 @@ import 'package:flutter/material.dart'; +import 'package:flutter_hooks/flutter_hooks.dart'; +import 'package:hooks_riverpod/hooks_riverpod.dart'; -class MdLabMitraScreen extends StatelessWidget { +import '../../provider/local_auth_provider.dart'; + +class MdLabMitraScreen extends HookConsumerWidget { const MdLabMitraScreen({Key? key}) : super(key: key); @override - Widget build(BuildContext context) { + Widget build(BuildContext context, WidgetRef ref) { final size = MediaQuery.of(context).size; + final errorMessage = useState(""); + final oneUser = ref.read(localAuthProvider); + + if (oneUser == null) { + redirectToHome(); + } return Material( child: Container( height: size.height, @@ -33,3 +43,46 @@ class MdLabMitraScreen extends StatelessWidget { ); } } + +class FxLoadingWidget extends StatelessWidget { + final String title; + final Color color; + final double size; + final double fontSize; + const FxLoadingWidget({ + Key? key, + required this.title, + this.color = Colors.blue, + this.fontSize = 16.0, + this.size = 48.0, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + return Material( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + mainAxisSize: MainAxisSize.max, + children: [ + SizedBox( + width: size, + height: size, + child: CircularProgressIndicator( + color: color, + ), + ), + const SizedBox( + height: 10, + ), + Text( + title, + style: TextStyle( + fontSize: fontSize, + color: color, + ), + ), + ], + ), + ); + } +} diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 287b6a9..cccf817 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,8 +5,6 @@ import FlutterMacOS import Foundation -import shared_preferences_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { - SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index e6659f7..185e57d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -71,20 +71,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" - ffi: - dependency: transitive - description: - name: ffi - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0" - file: - dependency: transitive - description: - name: file - url: "https://pub.dartlang.org" - source: hosted - version: "6.1.2" flutter: dependency: "direct main" description: flutter @@ -116,11 +102,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_web_plugins: - dependency: transitive - description: flutter - source: sdk - version: "0.0.0" hooks_riverpod: dependency: "direct main" description: @@ -184,48 +165,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.1" - path_provider_linux: - dependency: transitive - description: - name: path_provider_linux - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.7" - path_provider_platform_interface: - dependency: transitive - description: - name: path_provider_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.4" - path_provider_windows: - dependency: transitive - description: - name: path_provider_windows - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.0" - platform: - dependency: transitive - description: - name: platform - url: "https://pub.dartlang.org" - source: hosted - version: "3.1.0" - plugin_platform_interface: - dependency: transitive - description: - name: plugin_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.2" - process: - dependency: transitive - description: - name: process - url: "https://pub.dartlang.org" - source: hosted - version: "4.2.4" responsive_builder: dependency: "direct main" description: @@ -240,62 +179,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.3" - shared_preferences: - dependency: "direct main" - description: - name: shared_preferences - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.15" - shared_preferences_android: - dependency: transitive - description: - name: shared_preferences_android - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.12" - shared_preferences_ios: - dependency: transitive - description: - name: shared_preferences_ios - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" - shared_preferences_linux: - dependency: transitive - description: - name: shared_preferences_linux - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" - shared_preferences_macos: - dependency: transitive - description: - name: shared_preferences_macos - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.4" - shared_preferences_platform_interface: - dependency: transitive - description: - name: shared_preferences_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.0" - shared_preferences_web: - dependency: transitive - description: - name: shared_preferences_web - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.4" - shared_preferences_windows: - dependency: transitive - description: - name: shared_preferences_windows - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" sky_engine: dependency: transitive description: flutter @@ -364,20 +247,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" - win32: - dependency: transitive - description: - name: win32 - url: "https://pub.dartlang.org" - source: hosted - version: "2.7.0" - xdg_directories: - dependency: transitive + window_location_href: + dependency: "direct main" description: - name: xdg_directories + name: window_location_href url: "https://pub.dartlang.org" source: hosted - version: "0.2.0+1" + version: "1.0.0" sdks: dart: ">=2.17.1 <3.0.0" flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 849a407..6f9b6ee 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,10 +35,9 @@ dependencies: flutter_riverpod: ^1.0.4 hooks_riverpod: ^1.0.4 responsive_builder: ^0.4.1 - shared_preferences: ^2.0.7 intl: ^0.17.0 js: ^0.6.4 - + window_location_href: ^1.0.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 diff --git a/web/index.html b/web/index.html index d3045d7..5594ba1 100644 --- a/web/index.html +++ b/web/index.html @@ -38,6 +38,14 @@ function fx_initial_route() { return "/mdLabMitra"; } + localStorage.setItem( + "token", + `eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJNX1VzZXJJRCI6IjM0MCIsIk1fVXNlclVzZXJuYW1lIjoiYWRtaW5zYXMgIiwiTV9Vc2VyR3JvdXBEYXNoYm9hcmQiOiJvbmUtdWlcL3Rlc3RcL3Z1ZXhcL29uZS1mby1yZWdpc3RyYXRpb24tdjEyIiwiTV9Vc2VyRGVmYXVsdFRfU2FtcGxlU3RhdGlvbklEIjoiMCIsIk1fU3RhZmZOYW1lIjoiQURNSU4iLCJpc19jb3VyaWVyIjoiTiIsImlwIjoiMTgyLjI1My4xOTQuNCIsImFnZW50IjoiTW96aWxsYVwvNS4wIChYMTE7IExpbnV4IHg4Nl82NCkgQXBwbGVXZWJLaXRcLzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZVwvMTAxLjAuNDk1MS42NCBTYWZhcmlcLzUzNy4zNiIsInZlcnNpb24iOiJ2MiIsImxhc3QtbG9naW4iOiIyMDIyLTA1LTIzIDE0OjA2OjQ5IiwiTV9TYXRlbGxpdGVJRCI6MH0.amPh-paITGRM2Kb1c0LciDsWd_OdC0jN2WN5ugN-gvI` + ); + localStorage.setItem( + "user", + `{"M_UserID":"340","M_UserUsername":"adminsas ","M_UserGroupDashboard":"one-ui/test/vuex/one-fo-registration-v12","M_UserDefaultT_SampleStationID":"0","M_StaffName":"ADMIN","is_courier":"N","ip":"182.253.194.4","agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36","version":"v2","last-login":"2022-05-23 14:06:49","M_SatelliteID":0}` + );