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

View File

@ -33,6 +33,7 @@ class _LoginPageState extends State<LoginPage> {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
prefs.setString('token', jsonUser.token); prefs.setString('token', jsonUser.token);
prefs.setString('id', jsonUser.id); prefs.setString('id', jsonUser.id);
prefs.setString('refresh-token', jsonUser.refreshToken);
if (mounted) { if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Successfully logged in'))); 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:latlong2/latlong.dart';
import 'package:mobile/favorites.dart'; import 'package:mobile/favorites.dart';
import 'package:mobile/register.dart'; import 'package:mobile/register.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'login.dart'; import 'login.dart';
import 'base/sidemenu.dart'; import 'base/sidemenu.dart';
import 'profile.dart'; import 'profile.dart';
@ -12,7 +13,14 @@ import 'api.dart' as api;
import 'models.dart'; import 'models.dart';
import 'package:http/http.dart' as http; 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()); runApp(const MyApp());
} }

View File

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