01a0f1029f
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
193 lines
6.8 KiB
Dart
193 lines
6.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
|
|
|
import '../services/session_client.dart';
|
|
import '../supabase_config.dart';
|
|
import '../theme.dart';
|
|
|
|
/// Création d'un compte client : email + mot de passe + profil (nom, téléphone).
|
|
/// Le code de fidélité et le QR sont générés automatiquement côté serveur.
|
|
class SignupScreen extends StatefulWidget {
|
|
const SignupScreen({super.key});
|
|
|
|
@override
|
|
State<SignupScreen> createState() => _SignupScreenState();
|
|
}
|
|
|
|
class _SignupScreenState extends State<SignupScreen> {
|
|
final _formKey = GlobalKey<FormState>();
|
|
final _nomCtrl = TextEditingController();
|
|
final _prenomCtrl = TextEditingController();
|
|
final _telCtrl = TextEditingController();
|
|
final _emailCtrl = TextEditingController();
|
|
final _mdpCtrl = TextEditingController();
|
|
bool _enCours = false;
|
|
bool _voirMdp = false;
|
|
String? _erreur;
|
|
|
|
@override
|
|
void dispose() {
|
|
_nomCtrl.dispose();
|
|
_prenomCtrl.dispose();
|
|
_telCtrl.dispose();
|
|
_emailCtrl.dispose();
|
|
_mdpCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _inscription() async {
|
|
if (!_formKey.currentState!.validate()) return;
|
|
setState(() {
|
|
_enCours = true;
|
|
_erreur = null;
|
|
});
|
|
try {
|
|
final res = await supabase.auth.signUp(
|
|
email: _emailCtrl.text.trim(),
|
|
password: _mdpCtrl.text,
|
|
);
|
|
|
|
if (res.session != null) {
|
|
// Connecté directement (confirmation d'email désactivée) → on crée le
|
|
// profil fidélité. L'AuthGate basculera ensuite vers l'accueil.
|
|
await SessionClient.instance.creerProfil(
|
|
nom: _nomCtrl.text,
|
|
prenom: _prenomCtrl.text,
|
|
telephone: _telCtrl.text,
|
|
);
|
|
if (mounted) Navigator.of(context).pop();
|
|
} else {
|
|
// Confirmation d'email requise : le profil sera finalisé à la 1re
|
|
// connexion (après validation du mail).
|
|
if (mounted) await _popupVerifierEmail();
|
|
}
|
|
} on AuthException catch (e) {
|
|
if (mounted) setState(() => _erreur = e.message);
|
|
} catch (e) {
|
|
if (mounted) setState(() => _erreur = 'Erreur : $e');
|
|
} finally {
|
|
if (mounted) setState(() => _enCours = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _popupVerifierEmail() async {
|
|
await showDialog<void>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('Vérifiez votre email'),
|
|
content: const Text(
|
|
'Un email de confirmation vous a été envoyé. Validez-le puis '
|
|
'connectez-vous pour finaliser votre carte de fidélité.'),
|
|
actions: [
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(ctx).pop(),
|
|
child: const Text('OK'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (mounted) Navigator.of(context).pop(); // retour à l'accueil
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Créer un compte')),
|
|
body: SafeArea(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
TextFormField(
|
|
controller: _nomCtrl,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Nom',
|
|
prefixIcon: Icon(Icons.person_outline),
|
|
),
|
|
validator: (v) =>
|
|
(v == null || v.trim().isEmpty) ? 'Nom obligatoire' : null,
|
|
),
|
|
const SizedBox(height: 14),
|
|
TextFormField(
|
|
controller: _prenomCtrl,
|
|
textCapitalization: TextCapitalization.words,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Prénom',
|
|
prefixIcon: Icon(Icons.badge_outlined),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
TextFormField(
|
|
controller: _telCtrl,
|
|
keyboardType: TextInputType.phone,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Téléphone (facultatif)',
|
|
prefixIcon: Icon(Icons.phone_outlined),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
TextFormField(
|
|
controller: _emailCtrl,
|
|
keyboardType: TextInputType.emailAddress,
|
|
autofillHints: const [AutofillHints.email],
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
prefixIcon: Icon(Icons.mail_outline),
|
|
),
|
|
validator: (v) =>
|
|
(v == null || !v.contains('@')) ? 'Email invalide' : null,
|
|
),
|
|
const SizedBox(height: 14),
|
|
TextFormField(
|
|
controller: _mdpCtrl,
|
|
obscureText: !_voirMdp,
|
|
decoration: InputDecoration(
|
|
labelText: 'Mot de passe',
|
|
prefixIcon: const Icon(Icons.lock_outline),
|
|
suffixIcon: IconButton(
|
|
icon: Icon(
|
|
_voirMdp ? Icons.visibility_off : Icons.visibility),
|
|
onPressed: () => setState(() => _voirMdp = !_voirMdp),
|
|
),
|
|
),
|
|
validator: (v) => (v == null || v.length < 6)
|
|
? '6 caractères minimum'
|
|
: null,
|
|
onFieldSubmitted: (_) => _inscription(),
|
|
),
|
|
if (_erreur != null) ...[
|
|
const SizedBox(height: 16),
|
|
Text(_erreur!,
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(color: Colors.red.shade600)),
|
|
],
|
|
const SizedBox(height: 24),
|
|
FilledButton(
|
|
onPressed: _enCours ? null : _inscription,
|
|
child: _enCours
|
|
? const SizedBox(
|
|
height: 22,
|
|
width: 22,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2, color: Colors.white))
|
|
: const Text('Créer mon compte'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
const Text(
|
|
'Votre QR code de fidélité sera généré automatiquement.',
|
|
textAlign: TextAlign.center,
|
|
style: TextStyle(fontSize: 13, color: AppTheme.grisTexte),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|