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:http/http.dart' as http;
import 'dart:convert';
import 'dart:developer';
enum ApiService {
auth,
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 host = switch (service) {
@ -16,6 +19,9 @@ Future<String?> request(BuildContext context, ApiService service, String method,
ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'),
};
log('hello');
log(const String.fromEnvironment('AUTH_SERVICE_HOST'));
final http.Response response;
try {
@ -36,7 +42,8 @@ Future<String?> request(BuildContext context, ApiService service, String method,
);
}
} 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;
}
@ -45,7 +52,8 @@ Future<String?> request(BuildContext context, ApiService service, String method,
final json = jsonDecode(response.body);
messenger.showSnackBar(SnackBar(content: Text(json['message'])));
} 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;
}
@ -67,12 +75,14 @@ Future<bool> isLoggedIn(BuildContext context) async {
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
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');
return false;
}
} 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');
return false;
}

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:mobile/base/sidemenu.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'register.dart';
import 'api.dart' as api;
class LoginPage extends StatefulWidget {
@ -30,7 +29,7 @@ class _LoginPageState extends State<LoginPage> {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
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),
TextButton(
child: const Text('Register account'),
onPressed: () => Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => const RegisterPage()))),
onPressed: () =>
Navigator.pushReplacementNamed(context, '/register')),
]),
),
),

View File

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