init
This commit is contained in:
commit
6acec998bc
132 files changed
+5128
No files matched your search
@@ -0,0 +1,26 @@
|
||||
class auth_token {
|
||||
// Istanza privata statica
|
||||
static final auth_token _instance = auth_token._internal();
|
||||
|
||||
// Variabile per salvare il token
|
||||
String? _token;
|
||||
|
||||
// Costruttore privato
|
||||
auth_token._internal();
|
||||
|
||||
// Factory per ritornare sempre la stessa istanza
|
||||
factory auth_token() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
// Getter e setter per il token
|
||||
String? get token => _token;
|
||||
set token(String? value) {
|
||||
_token = value;
|
||||
}
|
||||
|
||||
// Metodo per cancellare il token (es. logout)
|
||||
void clear() {
|
||||
_token = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'pages/login_page.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Login App',
|
||||
theme: ThemeData(
|
||||
primarySwatch: Colors.deepPurple,
|
||||
),
|
||||
home: const LoginPage(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/api_service.dart';
|
||||
|
||||
class DashboardPage extends StatefulWidget {
|
||||
const DashboardPage({super.key});
|
||||
|
||||
@override
|
||||
State<DashboardPage> createState() => _DashboardPageState();
|
||||
}
|
||||
|
||||
class _DashboardPageState extends State<DashboardPage> {
|
||||
String? _result;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadLoginCheck();
|
||||
}
|
||||
|
||||
Future<void> _loadLoginCheck() async {
|
||||
try {
|
||||
final result = await ApiService.checkLogin();
|
||||
setState(() {
|
||||
_result = result.toString(); // oppure usa jsonEncode(result)
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Dashboard')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: _error != null
|
||||
? Text('Errore: $_error')
|
||||
: _result != null
|
||||
? Text('Oggetto ricevuto:\n$_result')
|
||||
: const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../services/api_service.dart';
|
||||
import 'dashboard_page.dart';
|
||||
import '../entities/auth_token.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
const LoginPage({super.key});
|
||||
|
||||
@override
|
||||
State<LoginPage> createState() => _LoginPageState();
|
||||
}
|
||||
|
||||
class _LoginPageState extends State<LoginPage> {
|
||||
final _emailController = TextEditingController();
|
||||
final _passwordController = TextEditingController();
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
void _login() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
final email = _emailController.text.trim();
|
||||
final password = _passwordController.text;
|
||||
|
||||
try {
|
||||
final token = await ApiService.login(email, password);
|
||||
auth_token().token = token;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Login riuscito! Token: $token')),
|
||||
);
|
||||
if (!mounted) return;
|
||||
Navigator.of(context).pushReplacement(
|
||||
MaterialPageRoute(builder: (_) => const DashboardPage()),
|
||||
);
|
||||
} catch (e) {
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
});
|
||||
} finally {
|
||||
setState(() {
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Login')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
children: [
|
||||
TextField(
|
||||
controller: _emailController,
|
||||
decoration: const InputDecoration(labelText: 'Email'),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _passwordController,
|
||||
obscureText: true,
|
||||
decoration: const InputDecoration(labelText: 'Password'),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
if (_error != null)
|
||||
Text(
|
||||
_error!,
|
||||
style: const TextStyle(color: Colors.red),
|
||||
),
|
||||
_loading
|
||||
? const CircularProgressIndicator()
|
||||
: ElevatedButton(
|
||||
onPressed: _login,
|
||||
child: const Text('Login'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:uwine_portal/entities/auth_token.dart';
|
||||
|
||||
class ApiService {
|
||||
static const String baseUrl = 'http://192.168.0.11:5000'; // cambia qui
|
||||
|
||||
// Token memorizzato in memoria (per sessione)
|
||||
static String? _token;
|
||||
|
||||
static String? get token => _token;
|
||||
|
||||
// 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
|
||||
return _token!;
|
||||
} else {
|
||||
throw Exception(
|
||||
'Login fallito: ${response.statusCode} - ${response.body}');
|
||||
}
|
||||
}
|
||||
|
||||
static Future<dynamic> checkLogin() async {
|
||||
final token = auth_token().token;
|
||||
if (token == null) {
|
||||
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