init
This commit is contained in:
commit
e833bcaec4
224 files changed
+11812
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'auth.service.dart';
|
||||
|
||||
class ApiService {
|
||||
static const String _baseUrl = 'http://192.168.0.9:5000'; // cambia qui
|
||||
|
||||
// Token memorizzato in memoria (per sessione)
|
||||
static String? _token;
|
||||
|
||||
static String? get token => _token;
|
||||
|
||||
static String get baseUrl => _baseUrl;
|
||||
|
||||
|
||||
|
||||
// Login con Basic Auth e salvataggio token
|
||||
static Future<String> login(String username, String password) async {
|
||||
final url = Uri.parse('$_baseUrl/User/login');
|
||||
|
||||
// Header con Basic Auth
|
||||
String basicAuth =
|
||||
'Basic ${base64Encode(utf8.encode('$username:$password'))}';
|
||||
|
||||
final response = await http.get(
|
||||
url,
|
||||
headers: {
|
||||
'Authorization': basicAuth,
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
_token = data['access_token']; // salva il token in memoria
|
||||
print('Token ricevuto: $_token'); // stampa in console
|
||||
AuthService.instance.saveToken(_token!);
|
||||
return '${response.statusCode}';
|
||||
} else {
|
||||
throw Exception(
|
||||
'Login fallito: ${response.statusCode} - ${response.body}');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> checkLogin() async {
|
||||
final token = AuthService.instance.token;
|
||||
print('Token corrente: $token'); // stampa in console
|
||||
if (token == null) {
|
||||
print('Token non disponibile. Effettua il login prima.'); // stampa in console
|
||||
throw Exception('Token non disponibile. Effettua il login prima.');
|
||||
}
|
||||
|
||||
final url = Uri.parse('$_baseUrl/Test/cklogin');
|
||||
final response = await http.get(
|
||||
url,
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final object = jsonDecode(response.body);
|
||||
print('Risposta cklogin: $object'); // stampa in console
|
||||
return object;
|
||||
} else {
|
||||
throw Exception(
|
||||
'Errore checkLogin: ${response.statusCode} - ${response.body}');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user