2024-08-21 09:03:03 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-08-21 11:49:38 +01:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2024-08-21 09:03:03 +01:00
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
enum ApiService {
|
|
|
|
auth,
|
|
|
|
app,
|
|
|
|
}
|
|
|
|
|
2024-09-02 10:39:03 +01:00
|
|
|
Future<String?> request(BuildContext? context, ApiService service, String method, String path, Object? body) async {
|
|
|
|
final messenger = context != null ? ScaffoldMessenger.of(context) : null;
|
2024-08-22 08:17:36 +01:00
|
|
|
final prefs = await SharedPreferences.getInstance();
|
2024-08-21 09:03:03 +01:00
|
|
|
|
|
|
|
final host = switch (service) {
|
|
|
|
ApiService.auth => const String.fromEnvironment('AUTH_SERVICE_HOST'),
|
|
|
|
ApiService.app => const String.fromEnvironment('APP_SERVICE_HOST'),
|
|
|
|
};
|
|
|
|
|
2024-08-22 08:17:36 +01:00
|
|
|
final token = prefs.getString('token');
|
|
|
|
final Map<String, String> headers = {};
|
|
|
|
if (token != null) headers.addAll({'Authorization': 'Bearer $token'});
|
|
|
|
|
2024-08-21 09:03:03 +01:00
|
|
|
final http.Response response;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (method == 'GET') {
|
2024-08-22 08:17:36 +01:00
|
|
|
response = await http.get(Uri.parse(host + path), headers: headers);
|
2024-08-21 09:03:03 +01:00
|
|
|
} else {
|
|
|
|
final function = switch (method) {
|
|
|
|
'POST' => http.post,
|
|
|
|
'PUT' => http.put,
|
|
|
|
'DELETE' => http.delete,
|
|
|
|
_ => throw const FormatException('Invalid method'),
|
|
|
|
};
|
|
|
|
|
2024-08-22 08:17:36 +01:00
|
|
|
headers.addAll({'Content-Type': 'application/json'});
|
|
|
|
|
2024-08-21 09:03:03 +01:00
|
|
|
response = await function(
|
|
|
|
Uri.parse(host + path),
|
2024-08-22 08:17:36 +01:00
|
|
|
headers: headers,
|
2024-08-21 09:03:03 +01:00
|
|
|
body: body != null ? jsonEncode(body) : null,
|
|
|
|
);
|
|
|
|
}
|
2024-09-02 10:39:03 +01:00
|
|
|
} catch (e) {
|
|
|
|
debugPrint(e.toString());
|
|
|
|
messenger?.showSnackBar(const SnackBar(content: Text('Unable to connect to server')));
|
2024-08-21 09:03:03 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode < 200 || response.statusCode >= 300) {
|
|
|
|
try {
|
|
|
|
final json = jsonDecode(response.body);
|
2024-09-02 12:14:13 +01:00
|
|
|
messenger?.showSnackBar(SnackBar(content: Text(json['message'] ?? json['title'])));
|
2024-09-02 10:39:03 +01:00
|
|
|
debugPrint('API error: ' + json['message']);
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint(e.toString());
|
|
|
|
messenger?.showSnackBar(SnackBar(content: Text('Something went wrong (HTTP ${response.statusCode})')));
|
2024-08-21 09:03:03 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:16:07 +01:00
|
|
|
return utf8.decode(response.bodyBytes);
|
2024-08-21 11:49:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isLoggedIn(BuildContext context) async {
|
|
|
|
final messenger = ScaffoldMessenger.of(context);
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
final token = prefs.getString('token');
|
2024-08-27 07:21:15 +01:00
|
|
|
if (token == null) {
|
2024-09-02 12:14:13 +01:00
|
|
|
logout();
|
2024-08-23 11:34:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2024-08-21 11:49:38 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
String base64 = token.split('.')[1];
|
2024-08-22 09:20:59 +01:00
|
|
|
base64 += List.filled(base64.length % 4 == 0 ? 0 : 4 - base64.length % 4, '=').join();
|
2024-08-21 11:49:38 +01:00
|
|
|
|
|
|
|
final payload = jsonDecode(String.fromCharCodes(base64Decode(base64)));
|
|
|
|
|
|
|
|
if (payload['exp'] < DateTime.now().millisecondsSinceEpoch / 1000) {
|
2024-08-22 08:12:50 +01:00
|
|
|
messenger.showSnackBar(const SnackBar(content: Text('Token expired, please sign in again')));
|
2024-09-02 12:14:13 +01:00
|
|
|
|
|
|
|
logout();
|
2024-08-21 11:49:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2024-08-22 08:12:50 +01:00
|
|
|
messenger.showSnackBar(const SnackBar(content: Text('Invalid token, please sign in again')));
|
2024-08-22 11:14:06 +01:00
|
|
|
debugPrint(e.toString());
|
2024-09-02 12:14:13 +01:00
|
|
|
|
|
|
|
logout();
|
2024-08-21 11:49:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2024-09-02 12:14:13 +01:00
|
|
|
|
|
|
|
void logout() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
prefs.remove('token');
|
|
|
|
prefs.remove('refresh-token');
|
|
|
|
prefs.remove('id');
|
|
|
|
}
|