From 97d319b4a5255abeafa92117bb31c83430e1ba9c Mon Sep 17 00:00:00 2001 From: Jaimey Frank <104035227+jaimeyfrank@users.noreply.github.com> Date: Mon, 18 Nov 2024 19:15:22 -0500 Subject: [PATCH 1/3] add new user fields and remove admin flag --- login_page.dart | 182 ++++++++++++++++++++++++++++++++++++++++++++++++ user.dart | 33 +++++++++ 2 files changed, 215 insertions(+) create mode 100644 login_page.dart create mode 100644 user.dart diff --git a/login_page.dart b/login_page.dart new file mode 100644 index 0000000..a554aba --- /dev/null +++ b/login_page.dart @@ -0,0 +1,182 @@ +import 'package:bu_passport/pages/signup_page.dart'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:flutter/material.dart'; +import 'package:google_sign_in/google_sign_in.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; + +class LoginPage extends StatefulWidget { + const LoginPage({Key? key}) : super(key: key); + + @override + State createState() => _LoginPageState(); +} + +class _LoginPageState extends State { + String? _errorMessage; + static bool newUser = false; + static bool BUemail = true; + + // Navigate to the signup page + void _navigateToSignUp() { + if (mounted) { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => const SignUpPage(), + ), + ); + } + } + + // Navigate to the home page + void _navigateToHome() { + if (mounted) { + Navigator.pushNamed(context, '/home'); + } + } + + Future _signInWithGoogle() async { + try { + final GoogleSignIn googleSignIn = GoogleSignIn( + hostedDomain: 'bu.edu', + ); + final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); + if (googleUser == null) { + return null; // The user canceled the sign-in + } + + final GoogleSignInAuthentication googleAuth = await googleUser.authentication; + final AuthCredential credential = GoogleAuthProvider.credential( + accessToken: googleAuth.accessToken, + idToken: googleAuth.idToken, + ); + + final UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential); + final User? user = userCredential.user; + + if (user != null) { + if (user.email != null && !user.email!.contains("bu.edu")) { + BUemail = false; + await user.delete(); + setState(() { + _errorMessage = 'Please sign in with a BU email address.'; + }); + return null; + } else { + BUemail = true; + } + // Check if the user document exists in Firestore + final userDoc = FirebaseFirestore.instance.collection('users').doc(user.uid); + final docSnapshot = await userDoc.get(); + + if (!docSnapshot.exists) { + // Create a new user document + await userDoc.set({ + 'userUID': user.uid, + 'userEmail': user.email, + 'firstName': userCredential.additionalUserInfo?.profile?['given_name'], + 'lastName': userCredential.additionalUserInfo?.profile?['family_name'], + 'userProfileURL': user.photoURL, + 'userPoints': 0, + 'userSavedEvents': {}, + 'userBUID': '', + 'userSchool': '', + 'userYear': 0, + 'userCollectedStickers': {}, + 'userPhotos': [], + 'userCreatedAt': FieldValue.serverTimestamp(), + }); + // Set newUser flag + newUser = true; + } else { + // Check if additional fields are set + final data = docSnapshot.data(); + + if (data != null && (data['userBUID'] == '' || data['userSchool'] == '' || data['userYear'] == 0)) { + newUser = true; + } + } + } else { + setState(() { + _errorMessage = 'Sign in failed. Please sign in with a BU email address.'; + }); + return null; + } + + return user; + } catch (e) { + if (_errorMessage != null) { + setState(() { + _errorMessage = 'Please sign in with a BU email address. Error: $e'; + }); + } else if (mounted) { + setState(() { + _errorMessage = 'Google Sign-In failed. Please try again. Error: $e'; + }); + } + return null; + } + } + + @override + Widget build(BuildContext context) { + double sizedBoxHeight = (MediaQuery.of(context).size.height * 0.05); + double edgeInsets = (MediaQuery.of(context).size.width * 0.02); + + return Scaffold( + body: Center( + child: Padding( + padding: EdgeInsets.all(edgeInsets), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Image.asset( + "assets/images/onboarding/BU art logo.png", + fit: BoxFit.contain, + ), + SizedBox(height: sizedBoxHeight), + GestureDetector( + onTap: () async { + User? user = await _signInWithGoogle(); + if (user != null) { + if (newUser) { + debugPrint("navigate to signup"); + _navigateToSignUp(); + } else { + debugPrint("navigate to home"); + _navigateToHome(); + } + } + }, + child: Container( + width: 244.14, + height: 43.89, + decoration: BoxDecoration( + color: const Color(0xFFCC0000), + borderRadius: BorderRadius.circular(66.75), + ), + child: const Center( + child: Text( + 'Sign In with BU Gmail', + style: TextStyle( + color: Colors.white, + fontSize: 16, + ), + ), + ), + ), + ), + SizedBox(height: sizedBoxHeight), + if (_errorMessage != null || !BUemail) + Text( + 'Please sign in with a BU email address.', + style: const TextStyle(color: Color(0xFFCC0000)), + ), + SizedBox(height: sizedBoxHeight), + ], + ), + ), + ), + ); + } +} \ No newline at end of file diff --git a/user.dart b/user.dart new file mode 100644 index 0000000..a227de6 --- /dev/null +++ b/user.dart @@ -0,0 +1,33 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; + +class Users { + final String firstName; + final String lastName; + final String userBUID; + final String userEmail; + final String userSchool; + final String userUID; + final int userYear; + final int userPoints; + final String userProfileURL; + final Map userSavedEvents; + final Map userCollectedStickers; + final List userPhotos; + final Timestamp userCreatedAt; + + Users({ + required this.firstName, + required this.lastName, + required this.userBUID, + required this.userEmail, + required this.userSchool, + required this.userUID, + required this.userYear, + required this.userSavedEvents, + required this.userPoints, + required this.userProfileURL, + required this.userCollectedStickers, + required this.userPhotos, + required this.userCreatedAt, + }); +} From 0041694fe017b246de97c1323d3dfbeab77de2d0 Mon Sep 17 00:00:00 2001 From: jaimeyfrank Date: Mon, 18 Nov 2024 19:22:16 -0500 Subject: [PATCH 2/3] fixed db changes --- bu_passport/lib/classes/user.dart | 12 +++++++----- bu_passport/lib/pages/login_page.dart | 5 +++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/bu_passport/lib/classes/user.dart b/bu_passport/lib/classes/user.dart index bd2dee1..aa934d8 100644 --- a/bu_passport/lib/classes/user.dart +++ b/bu_passport/lib/classes/user.dart @@ -1,4 +1,4 @@ -import 'package:bu_passport/classes/sticker.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; class Users { final String firstName; @@ -11,8 +11,9 @@ class Users { final int userPoints; final String userProfileURL; final Map userSavedEvents; - final bool admin; - final Map userStickerCollection; + final Map userCollectedStickers; + final List userPhotos; + final Timestamp userCreatedAt; Users({ required this.firstName, @@ -25,7 +26,8 @@ class Users { required this.userSavedEvents, required this.userPoints, required this.userProfileURL, - required this.admin, - required this.userStickerCollection, + required this.userCollectedStickers, + required this.userPhotos, + required this.userCreatedAt, }); } diff --git a/bu_passport/lib/pages/login_page.dart b/bu_passport/lib/pages/login_page.dart index 2c8d80d..8862b51 100644 --- a/bu_passport/lib/pages/login_page.dart +++ b/bu_passport/lib/pages/login_page.dart @@ -79,11 +79,12 @@ class _LoginPageState extends State { 'userProfileURL': user.photoURL, 'userPoints': 0, 'userSavedEvents': {}, - // Additional fields to be filled by the user 'userBUID': '', 'userSchool': '', 'userYear': 0, - 'admin': false, + 'userCollectedStickers': {}, + 'userPhotos': [], + 'userCreatedAt': FieldValue.serverTimestamp(), }); // Set newUser flag newUser = true; From b31840baf0bd7ab2636bd13c510b7ccf3ba02a38 Mon Sep 17 00:00:00 2001 From: jaimeyfrank Date: Mon, 18 Nov 2024 19:25:33 -0500 Subject: [PATCH 3/3] putting files in the right place oops --- login_page.dart | 182 ------------------------------------------------ user.dart | 33 --------- 2 files changed, 215 deletions(-) delete mode 100644 login_page.dart delete mode 100644 user.dart diff --git a/login_page.dart b/login_page.dart deleted file mode 100644 index a554aba..0000000 --- a/login_page.dart +++ /dev/null @@ -1,182 +0,0 @@ -import 'package:bu_passport/pages/signup_page.dart'; -import 'package:firebase_auth/firebase_auth.dart'; -import 'package:flutter/material.dart'; -import 'package:google_sign_in/google_sign_in.dart'; -import 'package:cloud_firestore/cloud_firestore.dart'; - -class LoginPage extends StatefulWidget { - const LoginPage({Key? key}) : super(key: key); - - @override - State createState() => _LoginPageState(); -} - -class _LoginPageState extends State { - String? _errorMessage; - static bool newUser = false; - static bool BUemail = true; - - // Navigate to the signup page - void _navigateToSignUp() { - if (mounted) { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => const SignUpPage(), - ), - ); - } - } - - // Navigate to the home page - void _navigateToHome() { - if (mounted) { - Navigator.pushNamed(context, '/home'); - } - } - - Future _signInWithGoogle() async { - try { - final GoogleSignIn googleSignIn = GoogleSignIn( - hostedDomain: 'bu.edu', - ); - final GoogleSignInAccount? googleUser = await googleSignIn.signIn(); - if (googleUser == null) { - return null; // The user canceled the sign-in - } - - final GoogleSignInAuthentication googleAuth = await googleUser.authentication; - final AuthCredential credential = GoogleAuthProvider.credential( - accessToken: googleAuth.accessToken, - idToken: googleAuth.idToken, - ); - - final UserCredential userCredential = await FirebaseAuth.instance.signInWithCredential(credential); - final User? user = userCredential.user; - - if (user != null) { - if (user.email != null && !user.email!.contains("bu.edu")) { - BUemail = false; - await user.delete(); - setState(() { - _errorMessage = 'Please sign in with a BU email address.'; - }); - return null; - } else { - BUemail = true; - } - // Check if the user document exists in Firestore - final userDoc = FirebaseFirestore.instance.collection('users').doc(user.uid); - final docSnapshot = await userDoc.get(); - - if (!docSnapshot.exists) { - // Create a new user document - await userDoc.set({ - 'userUID': user.uid, - 'userEmail': user.email, - 'firstName': userCredential.additionalUserInfo?.profile?['given_name'], - 'lastName': userCredential.additionalUserInfo?.profile?['family_name'], - 'userProfileURL': user.photoURL, - 'userPoints': 0, - 'userSavedEvents': {}, - 'userBUID': '', - 'userSchool': '', - 'userYear': 0, - 'userCollectedStickers': {}, - 'userPhotos': [], - 'userCreatedAt': FieldValue.serverTimestamp(), - }); - // Set newUser flag - newUser = true; - } else { - // Check if additional fields are set - final data = docSnapshot.data(); - - if (data != null && (data['userBUID'] == '' || data['userSchool'] == '' || data['userYear'] == 0)) { - newUser = true; - } - } - } else { - setState(() { - _errorMessage = 'Sign in failed. Please sign in with a BU email address.'; - }); - return null; - } - - return user; - } catch (e) { - if (_errorMessage != null) { - setState(() { - _errorMessage = 'Please sign in with a BU email address. Error: $e'; - }); - } else if (mounted) { - setState(() { - _errorMessage = 'Google Sign-In failed. Please try again. Error: $e'; - }); - } - return null; - } - } - - @override - Widget build(BuildContext context) { - double sizedBoxHeight = (MediaQuery.of(context).size.height * 0.05); - double edgeInsets = (MediaQuery.of(context).size.width * 0.02); - - return Scaffold( - body: Center( - child: Padding( - padding: EdgeInsets.all(edgeInsets), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Image.asset( - "assets/images/onboarding/BU art logo.png", - fit: BoxFit.contain, - ), - SizedBox(height: sizedBoxHeight), - GestureDetector( - onTap: () async { - User? user = await _signInWithGoogle(); - if (user != null) { - if (newUser) { - debugPrint("navigate to signup"); - _navigateToSignUp(); - } else { - debugPrint("navigate to home"); - _navigateToHome(); - } - } - }, - child: Container( - width: 244.14, - height: 43.89, - decoration: BoxDecoration( - color: const Color(0xFFCC0000), - borderRadius: BorderRadius.circular(66.75), - ), - child: const Center( - child: Text( - 'Sign In with BU Gmail', - style: TextStyle( - color: Colors.white, - fontSize: 16, - ), - ), - ), - ), - ), - SizedBox(height: sizedBoxHeight), - if (_errorMessage != null || !BUemail) - Text( - 'Please sign in with a BU email address.', - style: const TextStyle(color: Color(0xFFCC0000)), - ), - SizedBox(height: sizedBoxHeight), - ], - ), - ), - ), - ); - } -} \ No newline at end of file diff --git a/user.dart b/user.dart deleted file mode 100644 index a227de6..0000000 --- a/user.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; - -class Users { - final String firstName; - final String lastName; - final String userBUID; - final String userEmail; - final String userSchool; - final String userUID; - final int userYear; - final int userPoints; - final String userProfileURL; - final Map userSavedEvents; - final Map userCollectedStickers; - final List userPhotos; - final Timestamp userCreatedAt; - - Users({ - required this.firstName, - required this.lastName, - required this.userBUID, - required this.userEmail, - required this.userSchool, - required this.userUID, - required this.userYear, - required this.userSavedEvents, - required this.userPoints, - required this.userProfileURL, - required this.userCollectedStickers, - required this.userPhotos, - required this.userCreatedAt, - }); -}