Manter sessão e loading
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../../l10n/app_localizations.dart';
|
||||
import '../../../../core/services/auth_service.dart';
|
||||
import '../../../../core/services/session_service.dart';
|
||||
import '../../../../shared/presentation/widgets/custom_notification.dart';
|
||||
|
||||
class LoginPage extends StatefulWidget {
|
||||
@@ -18,6 +19,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||
final _passwordController = TextEditingController();
|
||||
bool _isLoading = false;
|
||||
bool _obscurePassword = true;
|
||||
bool _rememberMe = false;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
@@ -26,6 +28,26 @@ class _LoginPageState extends State<LoginPage> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadSavedSession();
|
||||
}
|
||||
|
||||
void _loadSavedSession() async {
|
||||
try {
|
||||
final sessionData = await SessionService.getCurrentSession();
|
||||
if (sessionData['email'] != null) {
|
||||
setState(() {
|
||||
_emailController.text = sessionData['email'];
|
||||
_rememberMe = sessionData['rememberMe'] ?? false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
print('DEBUG: Error loading saved session: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _handleLogin() async {
|
||||
if (_formKey.currentState!.validate()) {
|
||||
setState(() {
|
||||
@@ -45,7 +67,16 @@ class _LoginPageState extends State<LoginPage> {
|
||||
password: password,
|
||||
);
|
||||
|
||||
print('DEBUG: Login Firebase bem-sucedido, navegando para dashboard');
|
||||
print('DEBUG: Login Firebase bem-sucedido, salvando sessão');
|
||||
|
||||
// Save session based on remember me preference
|
||||
await SessionService.saveSession(
|
||||
rememberMe: _rememberMe,
|
||||
email: email,
|
||||
displayName: AuthService.currentUser?.displayName,
|
||||
);
|
||||
|
||||
print('DEBUG: Sessão salva, navegando para dashboard');
|
||||
|
||||
// Navigate to student dashboard after successful login
|
||||
if (mounted) {
|
||||
@@ -282,7 +313,41 @@ class _LoginPageState extends State<LoginPage> {
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Remember me checkbox
|
||||
Row(
|
||||
children: [
|
||||
Checkbox(
|
||||
value: _rememberMe,
|
||||
onChanged: (bool? value) {
|
||||
setState(() {
|
||||
_rememberMe = value ?? false;
|
||||
});
|
||||
},
|
||||
activeColor: const Color(0xFF82C9BD),
|
||||
checkColor: Colors.white,
|
||||
),
|
||||
GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_rememberMe = !_rememberMe;
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
'Manter sessão iniciada',
|
||||
style: TextStyle(
|
||||
color: const Color(0xFF2D3748),
|
||||
fontSize: 14,
|
||||
fontWeight: _rememberMe
|
||||
? FontWeight.w500
|
||||
: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Login button
|
||||
SizedBox(
|
||||
|
||||
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../../core/theme/app_colors.dart';
|
||||
import '../../../../core/services/auth_service.dart';
|
||||
import '../../../../core/services/session_service.dart';
|
||||
import '../../../../l10n/app_localizations.dart';
|
||||
|
||||
class SplashPage extends StatefulWidget {
|
||||
@@ -15,13 +17,58 @@ class _SplashPageState extends State<SplashPage> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_navigateToRoleSelection();
|
||||
_checkAuthenticationAndNavigate();
|
||||
}
|
||||
|
||||
void _navigateToRoleSelection() async {
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
if (mounted) {
|
||||
context.go('/role-selection');
|
||||
void _checkAuthenticationAndNavigate() async {
|
||||
try {
|
||||
print('DEBUG: Checking authentication state...');
|
||||
|
||||
// Always show splash for full 3 seconds for complete animation
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
|
||||
// Check if user is currently authenticated
|
||||
final currentUser = AuthService.currentUser;
|
||||
|
||||
if (currentUser != null) {
|
||||
print('DEBUG: User already authenticated: ${currentUser.email}');
|
||||
|
||||
// Update session with current user if needed
|
||||
await SessionService.updateSessionWithCurrentUser();
|
||||
|
||||
// Navigate to dashboard
|
||||
if (mounted) {
|
||||
print('DEBUG: Navigating to student dashboard');
|
||||
context.go('/student-dashboard');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if user should be auto-logged in
|
||||
final sessionData = await SessionService.shouldAutoLogin();
|
||||
|
||||
if (sessionData['shouldAutoLogin'] == true) {
|
||||
print('DEBUG: Auto-login available for: ${sessionData['email']}');
|
||||
|
||||
if (mounted) {
|
||||
// Navigate to login page with pre-filled data
|
||||
print('DEBUG: Navigating to login for auto-login');
|
||||
context.go('/login');
|
||||
}
|
||||
} else {
|
||||
print('DEBUG: No auto-login available, going to role selection');
|
||||
|
||||
if (mounted) {
|
||||
context.go('/role-selection');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('DEBUG: Error in authentication check: $e');
|
||||
|
||||
// Fallback to role selection
|
||||
if (mounted) {
|
||||
context.go('/role-selection');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user