Add Api changes and Dart code changes from last week

Co-authored-by: Reimar <mail@reim.ar>
This commit is contained in:
Sandertp 2024-09-02 11:39:03 +02:00
parent a471a11015
commit aaf3598650
4 changed files with 23 additions and 9 deletions

View File

@ -8,8 +8,8 @@ enum ApiService {
app,
}
Future<String?> request(BuildContext context, ApiService service, String method, String path, Object? body) async {
final messenger = ScaffoldMessenger.of(context);
Future<String?> request(BuildContext? context, ApiService service, String method, String path, Object? body) async {
final messenger = context != null ? ScaffoldMessenger.of(context) : null;
final prefs = await SharedPreferences.getInstance();
final host = switch (service) {
@ -42,17 +42,20 @@ Future<String?> request(BuildContext context, ApiService service, String method,
body: body != null ? jsonEncode(body) : null,
);
}
} catch (_) {
messenger.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
} catch (e) {
debugPrint(e.toString());
messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
return null;
}
if (response.statusCode < 200 || response.statusCode >= 300) {
try {
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(json['message'])));
debugPrint('API error: ' + json['message']);
} catch (e) {
debugPrint(e.toString());
messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})')));
}
return null;
}

View File

@ -33,6 +33,7 @@ class _LoginPageState extends State<LoginPage> {
final prefs = await SharedPreferences.getInstance();
prefs.setString('token', jsonUser.token);
prefs.setString('id', jsonUser.id);
prefs.setString('refresh-token', jsonUser.refreshToken);
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in')));

View File

@ -5,6 +5,7 @@ import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:mobile/favorites.dart';
import 'package:mobile/register.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'login.dart';
import 'base/sidemenu.dart';
import 'profile.dart';
@ -12,7 +13,14 @@ import 'api.dart' as api;
import 'models.dart';
import 'package:http/http.dart' as http;
void main() {
void main() async {
final prefs = await SharedPreferences.getInstance();
debugPrint('token: ' + (prefs.getString('token') ?? '') + ' reffresshh: ' + (prefs.getString("refresh-token") ?? ''));
if (prefs.getString("token") != null && prefs.getString("refresh-token") != null) {
final token = await api.request(null, api.ApiService.auth, "POST", "/RefreshToken", {'refresh-token': prefs.getString("refresh-token")});
debugPrint(token);
}
runApp(const MyApp());
}

View File

@ -23,13 +23,15 @@ class Favorite {
class Login {
String token;
String id;
String refreshToken;
Login(this.token, this.id);
Login(this.token, this.id, this.refreshToken);
factory Login.fromJson(Map<String, dynamic> json) {
return Login(
json['token'],
json['id'],
json['refreshToken'],
);
}
}