fixed api requests from sidemenu

This commit is contained in:
LilleBRG 2024-08-22 08:51:13 +02:00
parent ec498eb0f7
commit 5b38559e1c
3 changed files with 20 additions and 18 deletions

View File

@ -2,13 +2,16 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'dart:developer';
enum ApiService { enum ApiService {
auth, auth,
app, app,
} }
Future<String?> request(BuildContext context, ApiService service, String method, String path, Object? body) async { Future<String?> request(BuildContext context, ApiService service, String method,
String path, Object? body) async {
log('hello');
final messenger = ScaffoldMessenger.of(context); final messenger = ScaffoldMessenger.of(context);
final host = switch (service) { final host = switch (service) {
@ -16,6 +19,9 @@ Future<String?> request(BuildContext context, ApiService service, String method,
ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'), ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'),
}; };
log('hello');
log(const String.fromEnvironment('AUTH_SERVICE_HOST'));
final http.Response response; final http.Response response;
try { try {
@ -36,7 +42,8 @@ Future<String?> request(BuildContext context, ApiService service, String method,
); );
} }
} catch (_) { } catch (_) {
messenger.showSnackBar(const SnackBar(content: Text('Unable to connect to server'))); messenger.showSnackBar(
const SnackBar(content: Text('Unable to connect to server')));
return null; return null;
} }
@ -45,7 +52,8 @@ Future<String?> request(BuildContext context, ApiService service, String method,
final json = jsonDecode(response.body); final json = jsonDecode(response.body);
messenger.showSnackBar(SnackBar(content: Text(json['message']))); messenger.showSnackBar(SnackBar(content: Text(json['message'])));
} catch (_) { } catch (_) {
messenger.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})'))); messenger.showSnackBar(SnackBar(
content: Text('Something went wrong (HTTP ${response.statusCode})')));
} }
return null; return null;
} }
@ -67,12 +75,14 @@ Future<bool> isLoggedIn(BuildContext context) async {
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64))); final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) { if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) {
messenger.showSnackBar(const SnackBar(content: Text('Token expired, please sign in again'))); messenger.showSnackBar(
const SnackBar(content: Text('Token expired, please sign in again')));
prefs.remove('token'); prefs.remove('token');
return false; return false;
} }
} catch (e) { } catch (e) {
messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again'))); messenger.showSnackBar(
const SnackBar(content: Text('Invalid token, please sign in again')));
prefs.remove('token'); prefs.remove('token');
return false; return false;
} }

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart'; import 'package:mobile/base/sidemenu.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'register.dart';
import 'api.dart' as api; import 'api.dart' as api;
class LoginPage extends StatefulWidget { class LoginPage extends StatefulWidget {
@ -30,7 +29,7 @@ class _LoginPageState extends State<LoginPage> {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Successfully logged in'))); const SnackBar(content: Text('Successfully logged in')));
Navigator.pop(context); Navigator.pushReplacementNamed(context, '/home');
} }
} }
@ -57,10 +56,8 @@ class _LoginPageState extends State<LoginPage> {
const SizedBox(height: 10), const SizedBox(height: 10),
TextButton( TextButton(
child: const Text('Register account'), child: const Text('Register account'),
onPressed: () => Navigator.pushReplacement( onPressed: () =>
context, Navigator.pushReplacementNamed(context, '/register')),
MaterialPageRoute(
builder: (context) => const RegisterPage()))),
]), ]),
), ),
), ),

View File

@ -1,6 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart'; import 'package:mobile/base/sidemenu.dart';
import 'login.dart';
import 'api.dart' as api; import 'api.dart' as api;
class RegisterPage extends StatefulWidget { class RegisterPage extends StatefulWidget {
@ -28,8 +27,7 @@ class _RegisterPageState extends State<RegisterPage> {
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar( ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Successfully registered, please login'))); content: Text('Successfully registered, please login')));
Navigator.pushReplacement( Navigator.pushReplacementNamed(context, '/login');
context, MaterialPageRoute(builder: (context) => const LoginPage()));
} }
} }
@ -60,10 +58,7 @@ class _RegisterPageState extends State<RegisterPage> {
const SizedBox(height: 10), const SizedBox(height: 10),
TextButton( TextButton(
child: const Text('Login'), child: const Text('Login'),
onPressed: () => Navigator.pushReplacement( onPressed: () => Navigator.pushReplacementNamed(context, '/login')),
context,
MaterialPageRoute(
builder: (context) => const LoginPage()))),
]), ]),
), ),
), ),