Registration: Save user name
parent
323b2c7c4c
commit
b22b47581c
|
@ -1,9 +1,10 @@
|
||||||
import 'package:cofounderella/helper.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:firebase_auth/firebase_auth.dart';
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
import '../components/my_button.dart';
|
import '../components/my_button.dart';
|
||||||
import '../components/my_textfield.dart';
|
import '../components/my_textfield.dart';
|
||||||
|
import '../helper.dart';
|
||||||
import '../services/auth/auth_service.dart';
|
import '../services/auth/auth_service.dart';
|
||||||
|
import '../services/user_service.dart';
|
||||||
|
|
||||||
class RegisterPage extends StatelessWidget {
|
class RegisterPage extends StatelessWidget {
|
||||||
//text controllers
|
//text controllers
|
||||||
|
@ -26,10 +27,13 @@ class RegisterPage extends StatelessWidget {
|
||||||
// check if passwords match
|
// check if passwords match
|
||||||
if (_passwordController.text == _confirmPassController.text) {
|
if (_passwordController.text == _confirmPassController.text) {
|
||||||
try {
|
try {
|
||||||
await auth.signUpWithEmailPassword(
|
await auth
|
||||||
_emailController.text,
|
.signUpWithEmailPassword(
|
||||||
_passwordController.text,
|
_emailController.text, _passwordController.text)
|
||||||
);
|
.then((userCredential) {
|
||||||
|
UserService().saveUserData(userCredential, _emailController.text,
|
||||||
|
_nameController.text, _lastnameController.text);
|
||||||
|
});
|
||||||
} on FirebaseAuthException catch (e) {
|
} on FirebaseAuthException catch (e) {
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
showMsg(
|
showMsg(
|
||||||
|
@ -111,7 +115,9 @@ class RegisterPage extends StatelessWidget {
|
||||||
MyButton(
|
MyButton(
|
||||||
text: "Register",
|
text: "Register",
|
||||||
onTap: () => {
|
onTap: () => {
|
||||||
if (_emailController.text.trim().isEmpty ||
|
if ((_nameController.text.trim().isEmpty &&
|
||||||
|
_lastnameController.text.trim().isEmpty) ||
|
||||||
|
_emailController.text.trim().isEmpty ||
|
||||||
_passwordController.text.trim().isEmpty)
|
_passwordController.text.trim().isEmpty)
|
||||||
{
|
{
|
||||||
showMsg(
|
showMsg(
|
||||||
|
@ -132,14 +138,14 @@ class RegisterPage extends StatelessWidget {
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
"Already have an account? ",
|
'Already have an account? ',
|
||||||
style:
|
style:
|
||||||
TextStyle(color: Theme.of(context).colorScheme.primary),
|
TextStyle(color: Theme.of(context).colorScheme.primary),
|
||||||
),
|
),
|
||||||
GestureDetector(
|
GestureDetector(
|
||||||
onTap: onTap,
|
onTap: onTap,
|
||||||
child: const Text(
|
child: const Text(
|
||||||
"Login now",
|
'Login now',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
||||||
import 'package:firebase_auth/firebase_auth.dart';
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
import '../../constants.dart';
|
|
||||||
|
|
||||||
class AuthService {
|
class AuthService {
|
||||||
// instance of auth and firestore
|
// instance of auth and firestore
|
||||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||||
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
||||||
|
|
||||||
// get current user
|
// get current user
|
||||||
User? getCurrentUser() {
|
User? getCurrentUser() {
|
||||||
|
@ -40,17 +37,6 @@ class AuthService {
|
||||||
password: password,
|
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;
|
return userCredential;
|
||||||
} on FirebaseAuthException {
|
} on FirebaseAuthException {
|
||||||
rethrow;
|
rethrow;
|
||||||
|
@ -59,6 +45,6 @@ class AuthService {
|
||||||
|
|
||||||
// sign out
|
// sign out
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
return await _auth.signOut();
|
await _auth.signOut();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue