Skip to content

Commit

Permalink
Merge pull request #195 from SCCapstone/no-duplicate-emails-nghia
Browse files Browse the repository at this point in the history
no duplicate emails
  • Loading branch information
nghia-t-nguyen authored Apr 24, 2023
2 parents 654f37e + b32fe8a commit 45d46fb
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 43 deletions.
28 changes: 15 additions & 13 deletions app/lib/screens/ProfileScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class _ProfileScreenState extends State<ProfileScreen> {
appBar: AppBar(
automaticallyImplyLeading: false,
title: Text("Profile"),
backgroundColor: Color.fromARGB(255, 0, 161, 222),
backgroundColor: Color.fromARGB(255, 117, 178, 221),
),
body: FutureBuilder(
builder: (snap, ctx) {
// RefreshIndicator allows you to refresh the page so you can see
// your lesson progress change as you go through the lessons
return RefreshIndicator(
onRefresh: () async {
setState(() {}); // Refresh when drag down
Expand Down Expand Up @@ -88,26 +90,27 @@ class _ProfileScreenState extends State<ProfileScreen> {
Text(
usermodel
.UserPreferences.myUser.name,
style: const TextStyle(
style: TextStyle(
fontSize: 27,
fontWeight: FontWeight.w600),
),
const SizedBox(
SizedBox(
height: 10,
),
const Text(
Text(
"Lesson Progress",
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.w400,
),
),
const SizedBox(
SizedBox(
height: 5,
),
Text(
// sum up lesson progress
"${usermodel.UserPreferences.myUser.lessonProgress.reduce((a, b) => a + b)}",
style: const TextStyle(
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.w600,
color: style.success),
Expand All @@ -116,25 +119,24 @@ class _ProfileScreenState extends State<ProfileScreen> {
)
],
)),
const Divider(
Divider(
thickness: 2,
color: Colors.grey,
),
Container(
padding: EdgeInsets.fromLTRB(30, 15, 30, 15),
child: const Text(
child: Text(
"About Me",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w500),
)),
Container(
margin:
const EdgeInsets.fromLTRB(30, 0, 30, 0),
margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
// ignore: prefer_const_constructors
child: Text(
usermodel.UserPreferences.myUser.about,
style: const TextStyle(fontSize: 18),
style: TextStyle(fontSize: 18),
)),
],
)),
Expand All @@ -144,15 +146,15 @@ class _ProfileScreenState extends State<ProfileScreen> {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
builder: (BuildContext ctx) =>
const LoginPage()))
});
},
child: const Text(
"Log Out",
style: TextStyle(fontSize: 18),
)),
const SizedBox(height: 80)
SizedBox(height: 80)
]));
},
future: usermodel.UserPreferences.myUser.loadData(),
Expand Down
106 changes: 78 additions & 28 deletions app/lib/screens/SignUpScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class _SignUpPage extends State<SignUpPageState> {
static const d = Color.fromRGBO(146, 148, 156, 1.0);
static const background = Color.fromARGB(219, 39, 183, 196);

// Create a new user with Email and password using text inside the
// Email and password controllers
Future<void> createUserWithEmailAndPassword() async {
try {
await Auth().createUserWithEmailAndPassword(
Expand All @@ -101,6 +103,21 @@ class _SignUpPage extends State<SignUpPageState> {
}
}

// Returns true if email address is in use by checking in Firebase.
Future<bool> checkIfEmailInUse() async {
try {
final list = await FirebaseAuth.instance
.fetchSignInMethodsForEmail(emailController.text);
if (list.isNotEmpty) {
return true;
} else {
return false;
}
} catch (error) {
return true;
}
}

@override
Widget build(BuildContext context) {
return Padding(
Expand Down Expand Up @@ -206,34 +223,67 @@ class _SignUpPage extends State<SignUpPageState> {
builder: (context) => const LoginPage())),
child: const Text('Already Have an Account? Sign in'),
)),
ElevatedButton(
onPressed: () async {
if (passwordController.text != cpasswordController.text) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Passwords do not match.'),
backgroundColor: Colors.red,
),
);
return;
}

try {
await createUserWithEmailAndPassword();
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (context) => const Home()),
);
} on FirebaseAuthException catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(e.message!),
backgroundColor: Colors.red,
),
);
}
},
child: const Text('Sign Up'),
),
Container(
padding: const EdgeInsets.fromLTRB(0, 10, 10, 0),
alignment: Alignment.bottomCenter,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: (b3),
),
child: Text('Sign Up!'),
onPressed: () {
if (passwordController.text.length < 6) {
// Toast if password is too short
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text(
"Passwords must have at least 6 characters.")));
} else if (passwordController.text ==
cpasswordController.text) {
checkIfEmailInUse().then((emailInUse) => {
if (emailInUse)
{
// Toast if email is already in use
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content:
Text("Email is already in use.")))
}
else
{
createUserWithEmailAndPassword().then(
(value) {
UserPreferences.myUser.name =
nameController.text;
UserPreferences.myUser.saveData();
Auth()
.signInWithEmailAndPassword(
email: emailController.text.trim(),
password:
passwordController.text.trim())
.then((value) {
UserPreferences.myUser.saveData();
}).then((value) {
Auth().signOut();
});
},
).then(
(value) => {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
const LoginPage()))
},
)
}
});
} else {
// Toast if passwords do not match
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text("Passwords do not match.")));
}
},
))
],
));
}
Expand Down
10 changes: 8 additions & 2 deletions app/lib/services/auth.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:pohnpeian_language_app/models/userModel.dart' as um;
Expand Down Expand Up @@ -26,8 +27,13 @@ class Auth {

Future<void> deleteUser() async {
try {
_firebaseAuth.currentUser?.delete();
um.UserPreferences.myUser.reset();
final User? user = _firebaseAuth.currentUser;
FirebaseFirestore _firestore = FirebaseFirestore.instance;
if (user != null) {
await user.delete();
await _firestore.collection('users').doc(user.uid).delete();
um.UserPreferences.myUser.reset();
}
} catch (e) {}
}
}

0 comments on commit 45d46fb

Please sign in to comment.