(Feat) Migration PocketBase
This commit is contained in:
@@ -1,35 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
|
||||
import '../pocketbase_config.dart';
|
||||
import '../services/client_repository.dart';
|
||||
import '../services/recompense_repository.dart';
|
||||
import '../services/reglages.dart';
|
||||
import '../supabase_config.dart';
|
||||
import 'home_shell.dart';
|
||||
import 'staff_login_screen.dart';
|
||||
|
||||
/// Aiguille entre l'écran de connexion (staff non connecté) et l'app.
|
||||
/// Écoute l'état d'authentification Supabase en continu.
|
||||
/// Réagit aux changements de session PocketBase.
|
||||
class AuthGate extends StatelessWidget {
|
||||
const AuthGate({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!SupabaseConfig.estConfigure) return const _EcranNonConfigure();
|
||||
|
||||
return StreamBuilder<AuthState>(
|
||||
stream: supabase.auth.onAuthStateChange,
|
||||
return StreamBuilder(
|
||||
stream: pb.authStore.onChange,
|
||||
builder: (context, snapshot) {
|
||||
final session = supabase.auth.currentSession;
|
||||
if (session == null) return const StaffLoginScreen();
|
||||
if (!estStaff) return const StaffLoginScreen();
|
||||
return const _AppChargee();
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Charge les données (réglages, clients, récompenses) après connexion, puis
|
||||
/// affiche l'app. Recharge si le compte connecté change.
|
||||
/// Charge les données (réglages, clients, récompenses) après connexion.
|
||||
class _AppChargee extends StatefulWidget {
|
||||
const _AppChargee();
|
||||
|
||||
@@ -67,34 +62,3 @@ class _AppChargeeState extends State<_AppChargee> {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _EcranNonConfigure extends StatelessWidget {
|
||||
const _EcranNonConfigure();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: const [
|
||||
Icon(Icons.cloud_off, size: 56, color: Colors.grey),
|
||||
SizedBox(height: 16),
|
||||
Text('Supabase non configuré',
|
||||
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w700)),
|
||||
SizedBox(height: 8),
|
||||
Text(
|
||||
'Renseignez l\'URL et la clé anon dans lib/supabase_config.dart.\n'
|
||||
'Voir supabase/SETUP.md',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Color(0xFF6B6B72)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:flutter/services.dart';
|
||||
import '../models/client.dart';
|
||||
import '../models/mouvement.dart';
|
||||
import '../models/recompense.dart';
|
||||
import '../pocketbase_config.dart';
|
||||
import '../services/client_repository.dart';
|
||||
import '../services/recompense_repository.dart';
|
||||
import '../services/reglages.dart';
|
||||
@@ -26,11 +27,33 @@ class _ClientDetailScreenState extends State<ClientDetailScreen> {
|
||||
final _repo = ClientRepository.instance;
|
||||
List<Mouvement> _mouvements = [];
|
||||
bool _chargeMouvements = true;
|
||||
Future<void> Function()? _desabonner;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_rechargerMouvements();
|
||||
_sabonnerMouvements();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_desabonner?.call();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
/// Historique en temps réel : si un autre appareil ajoute/retire un mouvement
|
||||
/// pour ce client, la liste se met à jour toute seule.
|
||||
Future<void> _sabonnerMouvements() async {
|
||||
try {
|
||||
_desabonner = await pb.collection('mouvements').subscribe('*', (e) {
|
||||
final rec = e.record;
|
||||
if (rec != null && rec.data['client'] != widget.clientId) return;
|
||||
_rechargerMouvements();
|
||||
});
|
||||
} catch (_) {
|
||||
// Temps réel indisponible : on garde le rafraîchissement manuel.
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _rechargerMouvements() async {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../pocketbase_config.dart';
|
||||
import '../services/client_repository.dart';
|
||||
import '../services/reglages.dart';
|
||||
import '../supabase_config.dart';
|
||||
import '../theme.dart';
|
||||
import '../utils/format.dart';
|
||||
|
||||
@@ -19,6 +20,8 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
final _reglages = Reglages.instance;
|
||||
late final TextEditingController _ratioCtrl;
|
||||
late final TextEditingController _nomCtrl;
|
||||
bool? _serveurOk; // null = en cours de vérification
|
||||
bool _verifEnCours = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -27,6 +30,24 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
text: _formaterRatio(_reglages.eurosParPoint),
|
||||
);
|
||||
_nomCtrl = TextEditingController(text: _reglages.nomMagasin);
|
||||
_verifierServeur();
|
||||
}
|
||||
|
||||
Future<void> _verifierServeur() async {
|
||||
setState(() => _verifEnCours = true);
|
||||
bool ok;
|
||||
try {
|
||||
await pb.health.check();
|
||||
ok = true;
|
||||
} catch (_) {
|
||||
ok = false;
|
||||
}
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_serveurOk = ok;
|
||||
_verifEnCours = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -155,12 +176,15 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
_titre('Serveur'),
|
||||
_carteServeur(),
|
||||
const SizedBox(height: 24),
|
||||
_titre('À propos'),
|
||||
const Card(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text(
|
||||
'Données partagées via Supabase avec l\'app client. '
|
||||
'Données hébergées sur le serveur PocketBase du magasin. '
|
||||
'Le scan se fait avec la caméra ; la scanette externe sera '
|
||||
'branchée dans une prochaine version.',
|
||||
style: TextStyle(color: AppTheme.grisTexte, height: 1.4),
|
||||
@@ -197,7 +221,10 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
String? _emailConnecte() => supabase.auth.currentUser?.email;
|
||||
String? _emailConnecte() {
|
||||
final email = pb.authStore.record?.getStringValue('email');
|
||||
return (email == null || email.isEmpty) ? null : email;
|
||||
}
|
||||
|
||||
Future<void> _deconnexion() async {
|
||||
final ok = await showDialog<bool>(
|
||||
@@ -217,7 +244,7 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
],
|
||||
),
|
||||
);
|
||||
if (ok == true) await supabase.auth.signOut();
|
||||
if (ok == true) pb.authStore.clear();
|
||||
}
|
||||
|
||||
Widget _apercus() {
|
||||
@@ -255,6 +282,81 @@ class _ReglagesScreenState extends State<ReglagesScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _carteServeur() {
|
||||
final syncOk = ClientRepository.instance.tempsReelActif;
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.dns_outlined,
|
||||
size: 18, color: AppTheme.grisTexte),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
PocketBaseConfig.url,
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const Divider(height: 22),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: _verifEnCours
|
||||
? _ligneStatut(null, 'Vérification…')
|
||||
: _ligneStatut(_serveurOk, 'Connecté au serveur',
|
||||
koTexte: 'Serveur injoignable'),
|
||||
),
|
||||
if (_verifEnCours)
|
||||
const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
else
|
||||
IconButton(
|
||||
tooltip: 'Revérifier',
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: _verifierServeur,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
_ligneStatut(syncOk, 'Synchronisation temps réel active',
|
||||
koTexte: 'Temps réel inactif'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _ligneStatut(bool? ok, String texte, {String? koTexte}) {
|
||||
final couleur = ok == null
|
||||
? AppTheme.grisTexte
|
||||
: (ok ? Colors.green.shade600 : Colors.red.shade600);
|
||||
return Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 10,
|
||||
height: 10,
|
||||
decoration: BoxDecoration(color: couleur, shape: BoxShape.circle),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
(ok == false && koTexte != null) ? koTexte : texte,
|
||||
style: TextStyle(color: couleur, fontWeight: FontWeight.w600),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _titre(String texte) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(4, 0, 4, 10),
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||
import 'package:pocketbase/pocketbase.dart';
|
||||
|
||||
import '../supabase_config.dart';
|
||||
import '../pocketbase_config.dart';
|
||||
import '../theme.dart';
|
||||
|
||||
/// Connexion du personnel du magasin (staff). Les comptes staff sont créés dans
|
||||
/// Supabase (voir SETUP.md) — pas d'inscription libre ici.
|
||||
/// PocketBase (champ is_staff = true) — pas d'inscription libre ici.
|
||||
class StaffLoginScreen extends StatefulWidget {
|
||||
const StaffLoginScreen({super.key});
|
||||
|
||||
@@ -35,22 +35,24 @@ class _StaffLoginScreenState extends State<StaffLoginScreen> {
|
||||
_erreur = null;
|
||||
});
|
||||
try {
|
||||
await supabase.auth.signInWithPassword(
|
||||
email: _emailCtrl.text.trim(),
|
||||
password: _mdpCtrl.text,
|
||||
);
|
||||
await pb.collection('users').authWithPassword(
|
||||
_emailCtrl.text.trim(),
|
||||
_mdpCtrl.text,
|
||||
);
|
||||
// Vérifie que ce compte est bien autorisé (staff).
|
||||
final estStaff = await supabase.rpc('est_staff') as bool? ?? false;
|
||||
if (!estStaff) {
|
||||
await supabase.auth.signOut();
|
||||
pb.authStore.clear();
|
||||
if (mounted) {
|
||||
setState(() => _erreur =
|
||||
'Ce compte n\'est pas autorisé pour la tablette (staff).');
|
||||
}
|
||||
}
|
||||
// Si staff : l'AuthGate bascule automatiquement vers l'app.
|
||||
} on AuthException catch (e) {
|
||||
if (mounted) setState(() => _erreur = e.message);
|
||||
} on ClientException catch (e) {
|
||||
if (mounted) {
|
||||
setState(() => _erreur = 'Identifiants incorrects');
|
||||
}
|
||||
debugPrint('Login error: ${e.response}');
|
||||
} catch (e) {
|
||||
if (mounted) setState(() => _erreur = 'Erreur : $e');
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user