diff --git a/lib/pages/register_page.dart b/lib/pages/register_page.dart index 56edad2..73a1a32 100644 --- a/lib/pages/register_page.dart +++ b/lib/pages/register_page.dart @@ -1,9 +1,10 @@ -import 'package:cofounderella/helper.dart'; import 'package:flutter/material.dart'; import 'package:firebase_auth/firebase_auth.dart'; import '../components/my_button.dart'; import '../components/my_textfield.dart'; +import '../helper.dart'; import '../services/auth/auth_service.dart'; +import '../services/user_service.dart'; class RegisterPage extends StatelessWidget { //text controllers @@ -26,10 +27,13 @@ class RegisterPage extends StatelessWidget { // check if passwords match if (_passwordController.text == _confirmPassController.text) { try { - await auth.signUpWithEmailPassword( - _emailController.text, - _passwordController.text, - ); + await auth + .signUpWithEmailPassword( + _emailController.text, _passwordController.text) + .then((userCredential) { + UserService().saveUserData(userCredential, _emailController.text, + _nameController.text, _lastnameController.text); + }); } on FirebaseAuthException catch (e) { if (context.mounted) { showMsg( @@ -111,7 +115,9 @@ class RegisterPage extends StatelessWidget { MyButton( text: "Register", onTap: () => { - if (_emailController.text.trim().isEmpty || + if ((_nameController.text.trim().isEmpty && + _lastnameController.text.trim().isEmpty) || + _emailController.text.trim().isEmpty || _passwordController.text.trim().isEmpty) { showMsg( @@ -132,14 +138,14 @@ class RegisterPage extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.center, children: [ Text( - "Already have an account? ", + 'Already have an account? ', style: TextStyle(color: Theme.of(context).colorScheme.primary), ), GestureDetector( onTap: onTap, child: const Text( - "Login now", + 'Login now', style: TextStyle( fontWeight: FontWeight.bold, ), diff --git a/lib/services/auth/auth_service.dart b/lib/services/auth/auth_service.dart index f701c1a..eb107c7 100644 --- a/lib/services/auth/auth_service.dart +++ b/lib/services/auth/auth_service.dart @@ -1,11 +1,8 @@ -import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; -import '../../constants.dart'; class AuthService { // instance of auth and firestore final FirebaseAuth _auth = FirebaseAuth.instance; - final FirebaseFirestore _firestore = FirebaseFirestore.instance; // get current user User? getCurrentUser() { @@ -40,17 +37,6 @@ class AuthService { password: password, ); - // save user info in a document - _firestore - .collection(Constants.dbCollectionUsers) - .doc(userCredential.user!.uid) - .set( - { - 'uid': userCredential.user!.uid, - 'email': email, - }, - ); - return userCredential; } on FirebaseAuthException { rethrow; @@ -59,6 +45,6 @@ class AuthService { // sign out Future signOut() async { - return await _auth.signOut(); + await _auth.signOut(); } } diff --git a/lib/services/user_service.dart b/lib/services/user_service.dart new file mode 100644 index 0000000..fe393e1 --- /dev/null +++ b/lib/services/user_service.dart @@ -0,0 +1,89 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; +import '../constants.dart'; +import '../enumerations.dart'; + +class UserService { + final FirebaseFirestore _firestore = FirebaseFirestore.instance; + + Future saveUserData(UserCredential userCredential, String email, + String firstname, String lastname) async { + // create full name + String fullName = (firstname.isNotEmpty && lastname.isNotEmpty) + ? '$firstname $lastname' + : (firstname.isNotEmpty + ? firstname + : (lastname.isNotEmpty ? lastname : '')); + + // save user info to users document + await _firestore + .collection(Constants.dbCollectionUsers) + .doc(userCredential.user!.uid) + .set( + { + 'uid': userCredential.user!.uid, + 'email': email, + 'firstname': firstname, + 'lastname': lastname, + 'name': fullName, + }, + ); + } + + Future> getSkillsFromFirebase( + bool skillsSought, String userId) async { + // Fetch skills from Firestore + DocumentSnapshot userDoc = await _firestore + .collection(Constants.dbCollectionUsers) + .doc(userId) + .get(); + + if (userDoc.exists && userDoc.data() != null) { + Map userData = + userDoc.data()! as Map; // Explicit cast + + List? skills; + if (skillsSought) { + skills = userData[Constants.dbFieldUsersSkillsSought]; + } else { + skills = userData[ + Constants.dbFieldUsersSkills]; //as List?; // Explicit cast + } + + if (skills != null && skills.isNotEmpty) { + // Convert skills from strings to enum values + List userSkills = skills + .map((skill) => SkillOption.values + .firstWhere((x) => x.toString() == 'SkillOption.$skill')) + .toList(); + return userSkills; + } + } + + return []; + } + + Future saveSkillsToFirebase(List selectedOptions, + bool skillsSought, String userId) async { + try { + // Convert enum values to strings, removing leading EnumType with split + List skills = selectedOptions + .map((option) => option.toString().split('.').last) + .toList(); + + // Update the corresponding 'skills' field in the user's document + String keyToUpdate = skillsSought + ? Constants.dbFieldUsersSkillsSought + : Constants.dbFieldUsersSkills; + + _firestore + .collection(Constants.dbCollectionUsers) + .doc(userId) + .update({keyToUpdate: skills}); + + return true; + } catch (e) { + return false; + } + } +}