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,
# packages, and plugins designed to encourage good coding practices.
analyzer:
errors:
use_build_context_synchronously: ignore
include: package:flutter_lints/flutter.yaml
linter:

View File

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

View File

@ -22,6 +22,7 @@ void main() {
loginRoute: (context) => const LoginView(),
registerRoute: (context) => const RegisterView(),
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/firebase_options.dart';
import 'dart:developer' as devtools show log;
import 'package:mynotes/utilities/show_error_dialog.dart';
class RegisterView extends StatefulWidget {
const RegisterView({super.key});
@ -59,19 +60,40 @@ class _RegisterViewState extends State<RegisterView> {
final email = _email.text;
final password = _password.text;
try {
final userCredential = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: email, password: password);
devtools.log('User: ${userCredential.user}');
await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
final user = FirebaseAuth.instance.currentUser;
await user?.sendEmailVerification();
Navigator.of(context).pushNamed(verifyEmailRoute);
} on FirebaseAuthException catch (e) {
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') {
devtools.log('The account already exists for that email.');
await showErrorDialog(
context,
'Email already in use',
);
} 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) {
await showErrorDialog(
context,
e.toString(),
);
devtools.log(e.toString());
}
},

View File

@ -1,5 +1,6 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:mynotes/constants/routes.dart';
class VerifyEmailView extends StatefulWidget {
const VerifyEmailView({super.key});
@ -18,7 +19,12 @@ class _VerifyEmailViewState extends State<VerifyEmailView> {
backgroundColor: Colors.blue),
body: Column(
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(
onPressed: () async {
final user = FirebaseAuth.instance.currentUser;
@ -26,7 +32,17 @@ class _VerifyEmailViewState extends State<VerifyEmailView> {
},
child: const Text('Send verification email',
style: TextStyle(color: Colors.blue)),
)
),
TextButton(
onPressed: () async {
await FirebaseAuth.instance.signOut();
Navigator.of(context).pushNamedAndRemoveUntil(
registerRoute,
(route) => false,
);
},
child: const Text('Restart'),
),
],
),
);