Browse Source

localStorage

main
padmanto 2 years ago
parent
commit
06ee750b65
  1. 34
      README-localStorage-token.md
  2. 10
      lib/js/initial_route.dart
  3. 6
      lib/main.dart
  4. 58
      lib/model/one_user_model.dart
  5. 25
      lib/provider/local_auth_provider.dart
  6. 57
      lib/screen/md_lab_mitra/md_lab_mitra_screen.dart
  7. 2
      macos/Flutter/GeneratedPluginRegistrant.swift
  8. 132
      pubspec.lock
  9. 3
      pubspec.yaml
  10. 8
      web/index.html

34
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<OneLocalUserModel?>((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'];
}
```

10
lib/js/initial_route.dart

@ -1,4 +1,14 @@
import 'package:js/js.dart'; import 'package:js/js.dart';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
@JS('fx_initial_route') @JS('fx_initial_route')
external String fxInitialRoute(); external String fxInitialRoute();
String? getToken() {
return html.window.localStorage['token'];
}
String? getUser() {
return html.window.localStorage['user'];
}

6
lib/main.dart

@ -1,8 +1,12 @@
import 'dart:convert';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'app/constants.dart'; import 'app/constants.dart';
import 'app/route.dart'; import 'app/route.dart';
import 'js/initial_route.dart'; import 'js/initial_route.dart';
import 'model/one_user_model.dart';
import 'provider/local_auth_provider.dart';
import 'provider/title_provider.dart'; import 'provider/title_provider.dart';
void main() { void main() {
@ -15,13 +19,13 @@ void main() {
class MyApp extends HookConsumerWidget { class MyApp extends HookConsumerWidget {
const MyApp({Key? key}) : super(key: key); const MyApp({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context, WidgetRef ref) { Widget build(BuildContext context, WidgetRef ref) {
String initialRoute = homeRoute; String initialRoute = homeRoute;
try { try {
initialRoute = fxInitialRoute(); initialRoute = fxInitialRoute();
} catch (_) {} } catch (_) {}
return MaterialApp( return MaterialApp(
title: ref.watch(webTitleProvider), title: ref.watch(webTitleProvider),
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,

58
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<String, dynamic> 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<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
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;
}
}

25
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<OneLocalUserModel?>((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;
});

57
lib/screen/md_lab_mitra/md_lab_mitra_screen.dart

@ -1,11 +1,21 @@
import 'package:flutter/material.dart'; 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); const MdLabMitraScreen({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context, WidgetRef ref) {
final size = MediaQuery.of(context).size; final size = MediaQuery.of(context).size;
final errorMessage = useState("");
final oneUser = ref.read(localAuthProvider);
if (oneUser == null) {
redirectToHome();
}
return Material( return Material(
child: Container( child: Container(
height: size.height, 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,
),
),
],
),
);
}
}

2
macos/Flutter/GeneratedPluginRegistrant.swift

@ -5,8 +5,6 @@
import FlutterMacOS import FlutterMacOS
import Foundation import Foundation
import shared_preferences_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
} }

132
pubspec.lock

@ -71,20 +71,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.0" 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: flutter:
dependency: "direct main" dependency: "direct main"
description: flutter description: flutter
@ -116,11 +102,6 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
hooks_riverpod: hooks_riverpod:
dependency: "direct main" dependency: "direct main"
description: description:
@ -184,48 +165,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.1" 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: responsive_builder:
dependency: "direct main" dependency: "direct main"
description: description:
@ -240,62 +179,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.3" 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: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
@ -364,20 +247,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.2" version: "2.1.2"
win32: window_location_href:
dependency: transitive dependency: "direct main"
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "2.7.0"
xdg_directories:
dependency: transitive
description: description:
name: xdg_directories name: window_location_href
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.0+1" version: "1.0.0"
sdks: sdks:
dart: ">=2.17.1 <3.0.0" dart: ">=2.17.1 <3.0.0"
flutter: ">=3.0.0" flutter: ">=3.0.0"

3
pubspec.yaml

@ -35,10 +35,9 @@ dependencies:
flutter_riverpod: ^1.0.4 flutter_riverpod: ^1.0.4
hooks_riverpod: ^1.0.4 hooks_riverpod: ^1.0.4
responsive_builder: ^0.4.1 responsive_builder: ^0.4.1
shared_preferences: ^2.0.7
intl: ^0.17.0 intl: ^0.17.0
js: ^0.6.4 js: ^0.6.4
window_location_href: ^1.0.0
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2

8
web/index.html

@ -38,6 +38,14 @@
function fx_initial_route() { function fx_initial_route() {
return "/mdLabMitra"; 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}`
);
</script> </script>
<!-- This script adds the flutter initialization JS code --> <!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script> <script src="flutter.js" defer></script>

Loading…
Cancel
Save