Registration: Save user name

master
Rafael 2024-05-19 17:01:06 +02:00
parent 323b2c7c4c
commit b22b47581c
3 changed files with 104 additions and 23 deletions

View File

@ -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,
),

View File

@ -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<void> signOut() async {
return await _auth.signOut();
await _auth.signOut();
}
}

View File

@ -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<void> 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<List<SkillOption>> 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<String, dynamic> userData =
userDoc.data()! as Map<String, dynamic>; // Explicit cast
List<dynamic>? skills;
if (skillsSought) {
skills = userData[Constants.dbFieldUsersSkillsSought];
} else {
skills = userData[
Constants.dbFieldUsersSkills]; //as List<dynamic>?; // Explicit cast
}
if (skills != null && skills.isNotEmpty) {
// Convert skills from strings to enum values
List<SkillOption> userSkills = skills
.map((skill) => SkillOption.values
.firstWhere((x) => x.toString() == 'SkillOption.$skill'))
.toList();
return userSkills;
}
}
return [];
}
Future<bool> saveSkillsToFirebase(List<SkillOption> selectedOptions,
bool skillsSought, String userId) async {
try {
// Convert enum values to strings, removing leading EnumType with split
List<String> 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;
}
}
}