new start
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'token_storage.dart';
|
||||
|
||||
class AuthRepository {
|
||||
final String baseUrl;
|
||||
final TokenStorage tokens;
|
||||
AuthRepository(this.baseUrl, this.tokens);
|
||||
|
||||
Future<void> register(String email, String password) async {
|
||||
final res = await http.post(Uri.parse('$baseUrl/auth/register'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'email': email, 'password': password}));
|
||||
if (res.statusCode != 201) {
|
||||
throw Exception('Register failed: ${res.body}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> login(String email, String password) async {
|
||||
final res = await http.post(Uri.parse('$baseUrl/auth/login'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'email': email, 'password': password}));
|
||||
if (res.statusCode == 200) {
|
||||
final m = jsonDecode(res.body) as Map<String, dynamic>;
|
||||
await tokens.save(
|
||||
m['access_token'] as String, m['refresh_token'] as String);
|
||||
} else {
|
||||
throw Exception('Login failed: ${res.body}');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
final r = await tokens.refresh;
|
||||
if (r != null) {
|
||||
await http.post(Uri.parse('$baseUrl/auth/logout'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'refresh_token': r}));
|
||||
}
|
||||
await tokens.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
class TokenStorage {
|
||||
final _storage = const FlutterSecureStorage();
|
||||
final String baseUrl;
|
||||
TokenStorage(this.baseUrl);
|
||||
|
||||
Future<void> save(String access, String refresh) async {
|
||||
await _storage.write(key: 'access', value: access);
|
||||
await _storage.write(key: 'refresh', value: refresh);
|
||||
}
|
||||
|
||||
Future<String?> get access async => await _storage.read(key: 'access');
|
||||
Future<String?> get refresh async => await _storage.read(key: 'refresh');
|
||||
Future<void> clear() async {
|
||||
await _storage.deleteAll();
|
||||
}
|
||||
|
||||
/// Einfache Auto‑Refresh Logik
|
||||
Future<String?> getValidAccessToken() async {
|
||||
final token = await access;
|
||||
// (Optional) hier exp prüfen. Für Kürze direkt refresh call beim 401 außerhalb.
|
||||
return token;
|
||||
}
|
||||
|
||||
Future<void> refreshAccess() async {
|
||||
final r = await refresh;
|
||||
if (r == null) return;
|
||||
final res = await http.post(Uri.parse('$baseUrl/auth/refresh'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({'refresh_token': r}));
|
||||
if (res.statusCode == 200) {
|
||||
final m = jsonDecode(res.body) as Map<String, dynamic>;
|
||||
await _storage.write(key: 'access', value: m['access_token'] as String);
|
||||
} else {
|
||||
await clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user