add token session login
This commit is contained in:
parent
cb75b69a3c
commit
eb3fd7aca3
|
|
@ -1,19 +1,34 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'screens/login_screen.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
void main() {
|
import 'screens/login_screen.dart';
|
||||||
runApp(MyApp());
|
import 'screens/camera_screen.dart';
|
||||||
|
|
||||||
|
Future<void> main() async {
|
||||||
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
final String? token = prefs.getString("auth_token");
|
||||||
|
|
||||||
|
runApp(MyApp(isLoggedIn: token != null && token.isNotEmpty));
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatelessWidget {
|
||||||
const MyApp({super.key});
|
final bool isLoggedIn;
|
||||||
|
|
||||||
|
const MyApp({
|
||||||
|
super.key,
|
||||||
|
required this.isLoggedIn,
|
||||||
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
debugShowCheckedModeBanner: false,
|
debugShowCheckedModeBanner: false,
|
||||||
title: 'Object Detector Assist',
|
title: 'Object Detector Assist',
|
||||||
home: LoginScreen(),
|
home: isLoggedIn
|
||||||
|
? const CameraScreen()
|
||||||
|
: const LoginScreen(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,7 +5,7 @@ class ApiConfig {
|
||||||
static const bool useAndroidEmulator = false;
|
static const bool useAndroidEmulator = false;
|
||||||
|
|
||||||
// Ganti IP ini dengan IP laptop/PC Anda saat menggunakan HP fisik (ADB).
|
// Ganti IP ini dengan IP laptop/PC Anda saat menggunakan HP fisik (ADB).
|
||||||
static const String deviceHost = "192.168.18.14:8000";
|
static const String deviceHost = "192.168.110.147:8000";
|
||||||
|
|
||||||
static String get baseUrl {
|
static String get baseUrl {
|
||||||
if (Platform.isAndroid && useAndroidEmulator) {
|
if (Platform.isAndroid && useAndroidEmulator) {
|
||||||
|
|
@ -14,3 +14,4 @@ class ApiConfig {
|
||||||
return "http://$deviceHost";
|
return "http://$deviceHost";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,4 +54,19 @@ class AuthService {
|
||||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
await prefs.setString(_tokenKey, token);
|
await prefs.setString(_tokenKey, token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<String?> getToken() async {
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
return prefs.getString(_tokenKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> isLoggedIn() async {
|
||||||
|
final String? token = await getToken();
|
||||||
|
return token != null && token.isNotEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> logout() async {
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
await prefs.remove(_tokenKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ import 'package:smart_vision_assist/main.dart';
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||||
// Build our app and trigger a frame.
|
// Build our app and trigger a frame.
|
||||||
await tester.pumpWidget(MyApp());
|
await tester.pumpWidget(const MyApp(isLoggedIn: false));
|
||||||
|
|
||||||
// Verify that our counter starts at 0.
|
// Verify that our counter starts at 0.
|
||||||
expect(find.text('0'), findsOneWidget);
|
expect(find.text('0'), findsOneWidget);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue