add token session login
This commit is contained in:
parent
cb75b69a3c
commit
eb3fd7aca3
|
|
@ -1,19 +1,34 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'screens/login_screen.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
runApp(MyApp());
|
||||
import 'screens/login_screen.dart';
|
||||
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 {
|
||||
const MyApp({super.key});
|
||||
final bool isLoggedIn;
|
||||
|
||||
const MyApp({
|
||||
super.key,
|
||||
required this.isLoggedIn,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
title: 'Object Detector Assist',
|
||||
home: LoginScreen(),
|
||||
home: isLoggedIn
|
||||
? const CameraScreen()
|
||||
: const LoginScreen(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ class ApiConfig {
|
|||
static const bool useAndroidEmulator = false;
|
||||
|
||||
// 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 {
|
||||
if (Platform.isAndroid && useAndroidEmulator) {
|
||||
|
|
@ -14,3 +14,4 @@ class ApiConfig {
|
|||
return "http://$deviceHost";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -54,4 +54,19 @@ class AuthService {
|
|||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
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() {
|
||||
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
|
||||
// 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.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
|
|
|
|||
Loading…
Reference in New Issue