diff --git a/lib/core/services/auth_service.dart b/lib/core/services/auth_service.dart index 70c5e96..d144187 100644 --- a/lib/core/services/auth_service.dart +++ b/lib/core/services/auth_service.dart @@ -19,6 +19,8 @@ class AuthService { static Future signUpWithEmailAndPassword({ required String email, required String password, + String? displayName, + String? role, }) async { try { print('DEBUG: Tentando criar conta para email: $email'); @@ -33,6 +35,18 @@ class AuthService { print('DEBUG: User ID: ${result.user?.uid}'); print('DEBUG: Email verified: ${result.user?.emailVerified}'); + // Update user profile with display name + if (displayName != null && displayName.isNotEmpty) { + await result.user?.updateDisplayName(displayName); + print('DEBUG: Display name atualizado para: $displayName'); + } + + // Store user role in custom claims or Firestore (simplified for now) + if (role != null) { + print('DEBUG: Papel do usuário: $role'); + // TODO: Store role in Firestore for future use + } + // Verificar se o email foi verificado if (result.user != null && !result.user!.emailVerified) { print('DEBUG: Email não verificado, tentando enviar verificação...'); @@ -73,6 +87,12 @@ class AuthService { print('DEBUG: Login realizado com sucesso para: ${result.user?.email}'); print('DEBUG: User ID: ${result.user?.uid}'); print('DEBUG: Email verified: ${result.user?.emailVerified}'); + print('DEBUG: Display name: ${result.user?.displayName}'); + + // Reload user data to ensure we have the latest information + await result.user?.reload(); + print('DEBUG: User data reloaded'); + print('DEBUG: Display name after reload: ${result.user?.displayName}'); return result; } on FirebaseAuthException catch (e) { diff --git a/lib/features/auth/presentation/pages/signup_page.dart b/lib/features/auth/presentation/pages/signup_page.dart index 844537e..86b5a7c 100644 --- a/lib/features/auth/presentation/pages/signup_page.dart +++ b/lib/features/auth/presentation/pages/signup_page.dart @@ -14,13 +14,16 @@ class SignupPage extends StatefulWidget { class _SignupPageState extends State { final _formKey = GlobalKey(); + final _nameController = TextEditingController(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); bool _isLoading = false; bool _obscurePassword = true; + String _selectedRole = 'student'; // 'student' or 'teacher' @override void dispose() { + _nameController.dispose(); _emailController.dispose(); _passwordController.dispose(); super.dispose(); @@ -34,16 +37,20 @@ class _SignupPageState extends State { }); try { - // Get email and password from controllers + // Get name, email and password from controllers + final name = _nameController.text.trim(); final email = _emailController.text.trim(); final password = _passwordController.text.trim(); print('DEBUG: Iniciando processo de signup para: $email'); + print('DEBUG: Nome: $name, Papel: $_selectedRole'); // Attempt signup with Firebase await AuthService.signUpWithEmailAndPassword( email: email, password: password, + displayName: name, + role: _selectedRole, ); print('DEBUG: Signup Firebase bem-sucedido, navegando para dashboard'); @@ -59,8 +66,12 @@ class _SignupPageState extends State { message: 'Conta criada com sucesso!', ); - // Navigate to student dashboard after successful signup - context.go('/student-dashboard'); + // Navigate to appropriate dashboard based on role + if (_selectedRole == 'teacher') { + context.go('/teacher-dashboard'); + } else { + context.go('/student-dashboard'); + } } } catch (e) { print('DEBUG: Erro no signup: $e'); @@ -181,6 +192,51 @@ class _SignupPageState extends State { ), const SizedBox(height: 24), + // Name field + TextFormField( + controller: _nameController, + keyboardType: TextInputType.name, + style: const TextStyle(color: Color(0xFF2D3748)), + decoration: InputDecoration( + labelText: 'Nome Completo', + labelStyle: const TextStyle( + color: Color(0xFF2D3748), + ), + hintStyle: const TextStyle( + color: Color(0xFF718096), + ), + prefixIcon: const Icon( + Icons.person, + color: Color(0xFF82C9BD), + ), + border: InputBorder.none, + enabledBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(8.0), + borderSide: const BorderSide( + color: Color(0xFFE2E8F0), + ), + ), + focusedBorder: OutlineInputBorder( + borderRadius: BorderRadius.circular(8.0), + borderSide: const BorderSide( + color: Color(0xFF82C9BD), + ), + ), + filled: true, + fillColor: const Color(0xFFF8F9FA), + ), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Nome é obrigatório'; + } + if (value.length < 2) { + return 'Nome muito curto'; + } + return null; + }, + ), + const SizedBox(height: 16), + // Email field TextFormField( controller: _emailController, @@ -282,6 +338,61 @@ class _SignupPageState extends State { return null; }, ), + const SizedBox(height: 16), + + // Role selection + Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const Text( + 'Eu sou:', + style: TextStyle( + color: Color(0xFF2D3748), + fontSize: 16, + fontWeight: FontWeight.w500, + ), + ), + const SizedBox(height: 8), + Row( + children: [ + Expanded( + child: RadioListTile( + title: const Text( + 'Aluno', + style: TextStyle(fontSize: 14), + ), + value: 'student', + groupValue: _selectedRole, + onChanged: (String? value) { + setState(() { + _selectedRole = value!; + }); + }, + activeColor: const Color(0xFF82C9BD), + contentPadding: EdgeInsets.zero, + ), + ), + Expanded( + child: RadioListTile( + title: const Text( + 'Professor', + style: TextStyle(fontSize: 14), + ), + value: 'teacher', + groupValue: _selectedRole, + onChanged: (String? value) { + setState(() { + _selectedRole = value!; + }); + }, + activeColor: const Color(0xFF82C9BD), + contentPadding: EdgeInsets.zero, + ), + ), + ], + ), + ], + ), const SizedBox(height: 24), // Signup button diff --git a/lib/features/dashboard/presentation/pages/student_dashboard_page.dart b/lib/features/dashboard/presentation/pages/student_dashboard_page.dart index c7309f5..cce06ce 100644 --- a/lib/features/dashboard/presentation/pages/student_dashboard_page.dart +++ b/lib/features/dashboard/presentation/pages/student_dashboard_page.dart @@ -21,12 +21,53 @@ class _StudentDashboardPageState extends State { _loadUserData(); } - void _loadUserData() { - final user = AuthService.currentUser; - if (user != null) { - setState(() { - _userName = user.displayName ?? 'Estudante'; - }); + @override + void didChangeDependencies() { + super.didChangeDependencies(); + // Reload user data when dependencies change (e.g., after navigation) + _loadUserData(); + } + + Future _loadUserData() async { + try { + final user = AuthService.currentUser; + if (user != null) { + print('DEBUG: Loading user data for dashboard'); + print('DEBUG: User email: ${user.email}'); + print('DEBUG: User displayName before reload: ${user.displayName}'); + + // Reload user data to get latest information from Firebase + await user.reload(); + + // Get the updated user + final updatedUser = AuthService.currentUser; + if (updatedUser != null) { + print( + 'DEBUG: User displayName after reload: ${updatedUser.displayName}', + ); + + // Use displayName if available, otherwise use email part before @ + String displayName = updatedUser.displayName ?? ''; + print('DEBUG: Initial displayName value: "$displayName"'); + + if (displayName.isEmpty && updatedUser.email != null) { + // Extract name from email (e.g., "fabioceia0223@gmail.com" -> "fabioceia0223") + displayName = updatedUser.email!.split('@')[0]; + print('DEBUG: Extracted name from email: "$displayName"'); + } + if (displayName.isEmpty) { + displayName = 'Estudante'; + print('DEBUG: Using fallback "Estudante"'); + } + + print('DEBUG: Final displayName to use: "$displayName"'); + setState(() { + _userName = displayName; + }); + } + } + } catch (e) { + print('DEBUG: Error loading user data: $e'); } } diff --git a/lib/features/splash/presentation/pages/splash_page.dart b/lib/features/splash/presentation/pages/splash_page.dart index 6c7767a..3683894 100644 --- a/lib/features/splash/presentation/pages/splash_page.dart +++ b/lib/features/splash/presentation/pages/splash_page.dart @@ -32,6 +32,18 @@ class _SplashPageState extends State { if (currentUser != null) { print('DEBUG: User already authenticated: ${currentUser.email}'); + print( + 'DEBUG: User displayName before reload: ${currentUser.displayName}', + ); + + // Reload user data to get latest information from Firebase + await currentUser.reload(); + + // Get the updated user + final updatedUser = AuthService.currentUser; + print( + 'DEBUG: User displayName after reload: ${updatedUser?.displayName}', + ); // Update session with current user if needed await SessionService.updateSessionWithCurrentUser();