mynotes: Improve email verification

This commit is contained in:
Evan Ferrao 2024-07-10 17:13:17 +05:30
parent a2c2b9570d
commit 4939850724
No known key found for this signature in database
GPG Key ID: F01DEB4D7CFC9B52
5 changed files with 52 additions and 9 deletions

View File

@ -7,6 +7,9 @@
# The following line activates a set of recommended lints for Flutter apps, # The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices. # packages, and plugins designed to encourage good coding practices.
analyzer:
errors:
use_build_context_synchronously: ignore
include: package:flutter_lints/flutter.yaml include: package:flutter_lints/flutter.yaml
linter: linter:

View File

@ -1,3 +1,4 @@
const loginRoute = '/login/'; const loginRoute = '/login/';
const registerRoute = '/register/'; const registerRoute = '/register/';
const notesRoute = '/notes/'; const notesRoute = '/notes/';
const verifyEmailRoute = '/verify-email/';

View File

@ -22,6 +22,7 @@ void main() {
loginRoute: (context) => const LoginView(), loginRoute: (context) => const LoginView(),
registerRoute: (context) => const RegisterView(), registerRoute: (context) => const RegisterView(),
notesRoute: (context) => const NotesView(), notesRoute: (context) => const NotesView(),
verifyEmailRoute: (context) => const VerifyEmailView(),
}, },
), ),
); );

View File

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:mynotes/constants/routes.dart'; import 'package:mynotes/constants/routes.dart';
import 'package:mynotes/firebase_options.dart'; import 'package:mynotes/firebase_options.dart';
import 'dart:developer' as devtools show log; import 'dart:developer' as devtools show log;
import 'package:mynotes/utilities/show_error_dialog.dart';
class RegisterView extends StatefulWidget { class RegisterView extends StatefulWidget {
const RegisterView({super.key}); const RegisterView({super.key});
@ -59,19 +60,40 @@ class _RegisterViewState extends State<RegisterView> {
final email = _email.text; final email = _email.text;
final password = _password.text; final password = _password.text;
try { try {
final userCredential = await FirebaseAuth.instance await FirebaseAuth.instance.createUserWithEmailAndPassword(
.createUserWithEmailAndPassword( email: email,
email: email, password: password); password: password,
devtools.log('User: ${userCredential.user}'); );
final user = FirebaseAuth.instance.currentUser;
await user?.sendEmailVerification();
Navigator.of(context).pushNamed(verifyEmailRoute);
} on FirebaseAuthException catch (e) { } on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') { if (e.code == 'weak-password') {
devtools.log('The password provided is too weak.'); await showErrorDialog(
context,
'Weak Password',
);
} else if (e.code == 'email-already-in-use') { } else if (e.code == 'email-already-in-use') {
devtools.log('The account already exists for that email.'); await showErrorDialog(
context,
'Email already in use',
);
} else if (e.code == 'invalid-email') { } else if (e.code == 'invalid-email') {
devtools.log('The email provided is invalid.'); await showErrorDialog(
context,
'Invalid Email',
);
} else {
await showErrorDialog(
context,
e.toString(),
);
} }
} catch (e) { } catch (e) {
await showErrorDialog(
context,
e.toString(),
);
devtools.log(e.toString()); devtools.log(e.toString());
} }
}, },

View File

@ -1,5 +1,6 @@
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mynotes/constants/routes.dart';
class VerifyEmailView extends StatefulWidget { class VerifyEmailView extends StatefulWidget {
const VerifyEmailView({super.key}); const VerifyEmailView({super.key});
@ -18,7 +19,12 @@ class _VerifyEmailViewState extends State<VerifyEmailView> {
backgroundColor: Colors.blue), backgroundColor: Colors.blue),
body: Column( body: Column(
children: [ children: [
const Text('Please verify your email address'), const Text(
"we've sent an email to your email address, Please open it to verify your account",
),
const Text(
'If you did not receive the email, please press the button below to resend it.',
),
TextButton( TextButton(
onPressed: () async { onPressed: () async {
final user = FirebaseAuth.instance.currentUser; final user = FirebaseAuth.instance.currentUser;
@ -26,7 +32,17 @@ class _VerifyEmailViewState extends State<VerifyEmailView> {
}, },
child: const Text('Send verification email', child: const Text('Send verification email',
style: TextStyle(color: Colors.blue)), style: TextStyle(color: Colors.blue)),
) ),
TextButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
Navigator.of(context).pushNamedAndRemoveUntil(
registerRoute,
(route) => false,
);
},
child: const Text('Restart'),
),
], ],
), ),
); );