Add Auth service and Login Page

This commit is contained in:
2026-03-06 08:53:41 +01:00
parent 46467c457a
commit 6a25030d90
4 changed files with 280 additions and 4 deletions

View File

@@ -0,0 +1,37 @@
import 'dart:convert';
import 'package:http/http.dart' as http;
class AuthService {
static const String baseUrl = "http://10.0.2.2:3000";
Future<Map<String, dynamic>> register(String username, String password) async {
final response = await http.post(
Uri.parse('$baseUrl/register'),
headers: {'Content-Type': 'application/json'},
body: json.encode({'username': username, 'password': password}),
);
if (response.statusCode == 201) {
return json.decode(response.body);
} else {
final body = json.decode(response.body);
throw Exception(body['error'] ?? 'Registration failed');
}
}
Future<Map<String, dynamic>> login(String username, String password) async {
final response = await http.post(
Uri.parse('$baseUrl/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode({'username': username, 'password': password}),
);
if (response.statusCode == 200) {
return json.decode(response.body);
} else {
final body = json.decode(response.body);
throw Exception(body['error'] ?? 'Login failed');
}
}
}