Added jwt authenticaation

This commit is contained in:
2026-03-10 21:00:43 +01:00
parent 9b1b540c43
commit 0b8d31e727
9 changed files with 148 additions and 64 deletions

View File

@@ -0,0 +1,45 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:frontend_splatournament_manager/main.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
class ApiClient {
static const String baseUrl = SplatournamentApp.baseUrl;
static const _storage = FlutterSecureStorage();
static Future<Map<String, String>> _getHeaders() async {
final token = await _storage.read(key: 'jwt_token');
return {
'Content-Type': 'application/json',
if (token != null) 'Authorization': 'Bearer $token',
};
}
static Future<http.Response> get(String endpoint) async {
final headers = await _getHeaders();
return await http.get(Uri.parse('$baseUrl$endpoint'), headers: headers);
}
static Future<http.Response> post(String endpoint, Map<String, dynamic> body) async {
final headers = await _getHeaders();
return await http.post(
Uri.parse('$baseUrl$endpoint'),
headers: headers,
body: jsonEncode(body),
);
}
static Future<http.Response> put(String endpoint, Map<String, dynamic> body) async {
final headers = await _getHeaders();
return await http.put(
Uri.parse('$baseUrl$endpoint'),
headers: headers,
body: jsonEncode(body),
);
}
static Future<http.Response> delete(String endpoint) async {
final headers = await _getHeaders();
return await http.delete(Uri.parse('$baseUrl$endpoint'), headers: headers);
}
}