Skip to content

Commit

Permalink
done cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
iyoubee committed Sep 15, 2023
1 parent 00ee064 commit 6637c7a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) =>
Expand Down
48 changes: 48 additions & 0 deletions lib/pages/SplashPage.dart
Original file line number Diff line number Diff line change
@@ -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
);
}
}
7 changes: 6 additions & 1 deletion lib/utils/auth.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/useTestimoni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit 6637c7a

Please sign in to comment.