Add Api changes and Dart code changes from last week
Co-authored-by: Reimar <mail@reim.ar>
This commit is contained in:
parent
0792862f25
commit
4b5c74d824
@ -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;
|
||||
}
|
||||
|
@ -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')));
|
||||
|
@ -6,6 +6,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';
|
||||
@ -15,7 +16,14 @@ import 'api.dart' as api;
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'dart:developer';
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
|
@ -25,13 +25,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'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user