diff --git a/lib/main.dart b/lib/main.dart index 0eee927..c4eeba3 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:trashsure/pages/SplashPage.dart'; import 'pages/LoginPage.dart'; import 'pages/RegisterPage.dart'; import 'package:trashsure/pages/user/DepositAddPage.dart'; @@ -31,8 +32,9 @@ class MyApp extends StatelessWidget { theme: ThemeData( primarySwatch: Colors.green, ), - home: const LandingPage(), + initialRoute: "/splash", routes: { + "/splash": (BuildContext context) => const SplashPage(), "/landing": (BuildContext context) => const LandingPage(), "/admin": (BuildContext context) => const AdminPage(idx: 0), "/admin/deposit/add": (BuildContext context) => diff --git a/lib/pages/SplashPage.dart b/lib/pages/SplashPage.dart new file mode 100644 index 0000000..064eb72 --- /dev/null +++ b/lib/pages/SplashPage.dart @@ -0,0 +1,48 @@ +// ignore_for_file: file_names, use_build_context_synchronously + +import 'dart:developer'; + +import 'package:flutter/material.dart'; +import 'package:trashsure/pages/LandingPage.dart'; +import 'package:trashsure/pages/user/UserPage.dart'; +import 'package:trashsure/utils/auth.dart'; +import 'package:provider/provider.dart'; + +class SplashPage extends StatefulWidget { + const SplashPage({super.key}); + + @override + State<SplashPage> createState() => _SplashPageState(); +} + +class _SplashPageState extends State<SplashPage> { + @override + void initState() { + super.initState(); + _checkLoggedInAndNavigate(); + } + + void _checkLoggedInAndNavigate() async { + final request = context.read<CookieRequest>(); + await request.init(); + log("init ${request.cookies}"); + if (request.loggedIn) { + Navigator.pushReplacement( + context, + MaterialPageRoute(builder: (context) => const UserPage(idx: 0)), + ); + } else { + Navigator.pushReplacement( + context, + MaterialPageRoute(builder: (context) => const LandingPage()), + ); + } + } + + @override + Widget build(BuildContext context) { + return const Center( + child: CircularProgressIndicator(), // Display a loading indicator + ); + } +} diff --git a/lib/utils/auth.dart b/lib/utils/auth.dart index 1c1f95d..983ab63 100644 --- a/lib/utils/auth.dart +++ b/lib/utils/auth.dart @@ -1,5 +1,7 @@ // ignore_for_file: depend_on_referenced_packages +import 'dart:developer'; + import 'package:http/http.dart' as http; import 'dart:convert'; import 'package:flutter/foundation.dart' show kIsWeb; @@ -80,7 +82,6 @@ class CookieRequest { } http.Response response = await _client.post(Uri.parse(url), body: data, headers: headers); - _updateCookie(response); // Expects and returns JSON request body return jsonDecode(utf8.decode(response.bodyBytes)); } @@ -154,6 +155,10 @@ class CookieRequest { http.Response response = await _client.post(Uri.parse(url)); if (response.statusCode == 200) { + await _updateCookie(response); + local = await SharedPreferences.getInstance(); + local.remove("cookies"); + loggedIn = false; jsonData = {}; } else { diff --git a/lib/utils/useTestimoni.dart b/lib/utils/useTestimoni.dart index ad40879..435b5a6 100644 --- a/lib/utils/useTestimoni.dart +++ b/lib/utils/useTestimoni.dart @@ -6,7 +6,7 @@ import 'dart:convert'; class UseTestimoni { Future<List<Testimoni>> fetchTestimoni() async { - var url = Uri.parse('https://trashsure.iyoubee.xyz/testimoni/get/'); + var url = Uri.parse('https://trashsure.iyoubee.xyz/testimoni/get'); var response = await http.get(url); // melakukan decode response menjadi bentuk json var data = jsonDecode(utf8.decode(response.bodyBytes));