Register done

pull/28/head
root 2 years ago
parent d8cf5ed01e
commit ffc8e24ea3

@ -29,7 +29,7 @@ class _LoginScreenState extends State<LoginScreen> {
try { try {
await MyApp.userViewModel await MyApp.userViewModel
.login(_userMailTextField.text, _passwordTextField.text); .login(_userMailTextField.text, _passwordTextField.text);
Navigator.pushNamed(context, '/explanation'); Navigator.pushNamed(context, '/feed');
} catch (e) { } catch (e) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(

@ -10,6 +10,7 @@ import 'package:google_fonts/google_fonts.dart';
import 'package:justmusic/values/constants.dart'; import 'package:justmusic/values/constants.dart';
import '../components/login_button.dart'; import '../components/login_button.dart';
import '../main.dart';
class RegistrationScreen extends StatefulWidget { class RegistrationScreen extends StatefulWidget {
const RegistrationScreen({Key? key}) : super(key: key); const RegistrationScreen({Key? key}) : super(key: key);
@ -20,12 +21,42 @@ class RegistrationScreen extends StatefulWidget {
class _RegistrationScreenState extends State<RegistrationScreen> { class _RegistrationScreenState extends State<RegistrationScreen> {
bool passenable = true; bool passenable = true;
final _formKey = GlobalKey<FormState>();
final _userPseudoTextField = TextEditingController();
final _userMailTextField = TextEditingController();
final _passwordTextField = TextEditingController();
final _passwordConfirmTextField = TextEditingController();
handleRegister() async {
if (_formKey.currentState!.validate()) {
try {
await MyApp.userViewModel.register(_userPseudoTextField.text,
_passwordTextField.text, _userMailTextField.text);
Navigator.pushNamed(context, '/explanation');
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
e.toString() ?? "",
style: GoogleFonts.plusJakartaSans(
color: Colors.white,
fontWeight: FontWeight.w400,
fontSize: 20.h),
),
backgroundColor: Colors.red,
),
);
}
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
backgroundColor: bgColor, backgroundColor: bgColor,
body: Stack( body: Form(
key: _formKey,
child: Stack(
children: [ children: [
SingleChildScrollView( SingleChildScrollView(
child: SizedBox( child: SizedBox(
@ -51,8 +82,8 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
Padding( Padding(
padding: padding: EdgeInsets.symmetric(
EdgeInsets.symmetric(horizontal: defaultPadding), horizontal: defaultPadding),
child: Padding( child: Padding(
padding: EdgeInsets.only(bottom: 50.h), padding: EdgeInsets.only(bottom: 50.h),
child: Column( child: Column(
@ -83,10 +114,11 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
left: defaultPadding, left: defaultPadding,
right: defaultPadding), right: defaultPadding),
child: TextFormField( child: TextFormField(
controller: _userPseudoTextField,
keyboardAppearance: Brightness.dark, keyboardAppearance: Brightness.dark,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return 'TODO'; return 'entrez un pseudo valide';
} }
return null; return null;
}, },
@ -97,15 +129,18 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
decoration: InputDecoration( decoration: InputDecoration(
focusedBorder: OutlineInputBorder( focusedBorder: OutlineInputBorder(
borderSide: BorderSide( borderSide: BorderSide(
width: 1.sp, color: strokeTextField), width: 1.sp,
color: strokeTextField),
borderRadius: const BorderRadius.all( borderRadius: const BorderRadius.all(
Radius.circular(10))), Radius.circular(10))),
contentPadding: const EdgeInsets.only( prefix: const Padding(
top: 0, bottom: 0, left: defaultPadding), padding: EdgeInsets.only(left: 20.0)),
suffix: const Padding(
padding: EdgeInsets.only(left: 20.0)),
fillColor: bgTextField, fillColor: bgTextField,
filled: true, filled: true,
focusColor: focusColor: const Color.fromRGBO(
const Color.fromRGBO(255, 255, 255, 0.30), 255, 255, 255, 0.30),
enabledBorder: const OutlineInputBorder( enabledBorder: const OutlineInputBorder(
borderSide: BorderSide( borderSide: BorderSide(
width: 1, color: strokeTextField), width: 1, color: strokeTextField),
@ -121,10 +156,11 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
left: defaultPadding, left: defaultPadding,
right: defaultPadding), right: defaultPadding),
child: TextFormField( child: TextFormField(
controller: _userMailTextField,
keyboardAppearance: Brightness.dark, keyboardAppearance: Brightness.dark,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return 'TODO'; return 'entrez un email valide';
} }
return null; return null;
}, },
@ -138,8 +174,10 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
width: 1, color: strokeTextField), width: 1, color: strokeTextField),
borderRadius: BorderRadius.all( borderRadius: BorderRadius.all(
Radius.circular(10))), Radius.circular(10))),
contentPadding: EdgeInsets.only( prefix: const Padding(
top: 0, bottom: 0, left: defaultPadding), padding: EdgeInsets.only(left: 20.0)),
suffix: const Padding(
padding: EdgeInsets.only(left: 20.0)),
fillColor: bgTextField, fillColor: bgTextField,
filled: true, filled: true,
focusColor: focusColor:
@ -159,16 +197,16 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
left: defaultPadding, left: defaultPadding,
right: defaultPadding), right: defaultPadding),
child: TextFormField( child: TextFormField(
controller: _passwordTextField,
keyboardAppearance: Brightness.dark, keyboardAppearance: Brightness.dark,
obscureText: passenable, obscureText: passenable,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return 'TODO'; return 'entrez un mot de passe valide';
} }
return null; return null;
}, },
cursorColor: primaryColor, cursorColor: primaryColor,
keyboardType: TextInputType.emailAddress,
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: primaryColor), color: primaryColor),
decoration: InputDecoration( decoration: InputDecoration(
@ -177,8 +215,10 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
width: 1, color: strokeTextField), width: 1, color: strokeTextField),
borderRadius: borderRadius:
BorderRadius.all(Radius.circular(10))), BorderRadius.all(Radius.circular(10))),
contentPadding: EdgeInsets.only( prefix: const Padding(
top: 0, bottom: 0, left: defaultPadding), padding: EdgeInsets.only(left: 20.0)),
suffix: const Padding(
padding: EdgeInsets.only(left: 20.0)),
fillColor: bgTextField, fillColor: bgTextField,
filled: true, filled: true,
focusColor: Color.fromRGBO(255, 255, 255, 0.30), focusColor: Color.fromRGBO(255, 255, 255, 0.30),
@ -203,9 +243,10 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
passenable = true; passenable = true;
} }
}); });
}, // Image tapped },
splashColor: Colors // Image tapped
.white10, // Splash color over image splashColor: Colors.white10,
// Splash color over image
child: Image( child: Image(
image: passenable image: passenable
? AssetImage( ? AssetImage(
@ -224,11 +265,14 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
left: defaultPadding, left: defaultPadding,
right: defaultPadding), right: defaultPadding),
child: TextFormField( child: TextFormField(
controller: _passwordConfirmTextField,
keyboardAppearance: Brightness.dark, keyboardAppearance: Brightness.dark,
obscureText: passenable, obscureText: passenable,
validator: (value) { validator: (value) {
if (value == null || value.isEmpty) { if (value == null || value.isEmpty) {
return 'TODO'; return 'entrez un mot de passe valide';
} else if (_passwordTextField.text != value) {
return 'les mots de passes ne sont pas identiques';
} }
return null; return null;
}, },
@ -242,8 +286,10 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
width: 1, color: strokeTextField), width: 1, color: strokeTextField),
borderRadius: borderRadius:
BorderRadius.all(Radius.circular(10))), BorderRadius.all(Radius.circular(10))),
contentPadding: EdgeInsets.only( prefix: const Padding(
top: 0, bottom: 0, left: defaultPadding), padding: EdgeInsets.only(left: 20.0)),
suffix: const Padding(
padding: EdgeInsets.only(left: 20.0)),
fillColor: bgTextField, fillColor: bgTextField,
filled: true, filled: true,
focusColor: Color.fromRGBO(255, 255, 255, 0.30), focusColor: Color.fromRGBO(255, 255, 255, 0.30),
@ -268,9 +314,10 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
passenable = true; passenable = true;
} }
}); });
}, // Image tapped },
splashColor: Colors // Image tapped
.white10, // Splash color over image splashColor: Colors.white10,
// Splash color over image
child: Image( child: Image(
image: passenable image: passenable
? AssetImage( ? AssetImage(
@ -284,12 +331,12 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
), ),
), ),
Padding( Padding(
padding: padding: EdgeInsets.symmetric(
EdgeInsets.symmetric(horizontal: defaultPadding), horizontal: defaultPadding),
child: SizedBox( child: SizedBox(
width: 600, width: 600,
child: LoginButton( child: LoginButton(
callback: () {}, callback: handleRegister,
)), )),
), ),
Align( Align(
@ -340,7 +387,8 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
child: Text( child: Text(
'Ou', 'Ou',
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white, fontWeight: FontWeight.bold), color: Colors.white,
fontWeight: FontWeight.bold),
), ),
), ),
Expanded( Expanded(
@ -362,7 +410,8 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
text: "Login with Google", text: "Login with Google",
onPressed: () {}, onPressed: () {},
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20))), borderRadius:
BorderRadius.all(Radius.circular(20))),
), ),
), ),
), ),
@ -403,6 +452,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
), ),
], ],
), ),
),
); );
} }
} }

@ -26,12 +26,13 @@ class AuthService {
.catchError((error) => print("Failed to add user: $error")); .catchError((error) => print("Failed to add user: $error"));
} on FirebaseAuthException catch (e) { } on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') { if (e.code == 'weak-password') {
throw ('The password provided is too weak.'); throw ('Mot de passe trop court');
} else if (e.code == 'email-already-in-use') { } else if (e.code == 'email-already-in-use') {
throw ('The account already exists for that email.'); throw ('Mail déjà utilisé');
} else if (e.code == 'invalid-email') {
throw ('Mauvais format de mail');
} }
} catch (e) { rethrow;
throw (e);
} }
} }
@ -40,14 +41,14 @@ class AuthService {
await FirebaseAuth.instance await FirebaseAuth.instance
.signInWithEmailAndPassword(email: email, password: password); .signInWithEmailAndPassword(email: email, password: password);
} on FirebaseAuthException catch (e) { } on FirebaseAuthException catch (e) {
if(e.code == 'user-not-found') { if (e.code == 'user-not-found') {
throw('Mail incorrect'); throw ('Mail incorrect');
} else if(e.code == 'wrong-password') { } else if (e.code == 'wrong-password') {
throw('Mot de passe incorrect'); throw ('Mot de passe incorrect');
} else if(e.code == 'invalid-email') { } else if (e.code == 'invalid-email') {
throw('Format de mail incorrect'); throw ('Format de mail incorrect');
} else if(e.code == 'too-many-requests') { } else if (e.code == 'too-many-requests') {
throw('L\'accès à ce compte a été temporairement désactivé en raison de nombreuses tentatives de connexion infructueuses. Réessayer plus tard.'); throw ('L\'accès à ce compte a été temporairement désactivé en raison de nombreuses tentatives de connexion infructueuses. Réessayer plus tard.');
} }
rethrow; rethrow;
} }

@ -38,13 +38,17 @@ class UserViewModel {
} }
register(String pseudo, String password, String email) async { register(String pseudo, String password, String email) async {
_authService.register(pseudo, email, password); try {
await _authService.register(pseudo, email, password);
final user = await MyApp.db final user = await MyApp.db
.collection("users") .collection("users")
.doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid) .doc(firebase_auth.FirebaseAuth.instance.currentUser?.uid)
.get(); .get();
User? data = UserMapper.toModel(user, null); User data = UserMapper.toModel(user, null);
_userCurrent = data; _userCurrent = data;
} catch (e) {
rethrow;
}
} }
logout() { logout() {

Loading…
Cancel
Save