fix(mobile): resolve client crash and shift status inconsistency
This commit is contained in:
@@ -257,4 +257,77 @@ class AuthRepositoryImpl implements AuthRepositoryInterface {
|
||||
);
|
||||
return domainUser;
|
||||
}
|
||||
@override
|
||||
Future<domain.User?> restoreSession() async {
|
||||
final User? firebaseUser = _service.auth.currentUser;
|
||||
if (firebaseUser == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. Fetch User
|
||||
final QueryResult<GetUserByIdData, GetUserByIdVariables> response =
|
||||
await _service.run(() => _service.connector
|
||||
.getUserById(
|
||||
id: firebaseUser.uid,
|
||||
)
|
||||
.execute());
|
||||
final GetUserByIdUser? user = response.data.user;
|
||||
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. Check Role
|
||||
if (user.userRole != 'STAFF' && user.userRole != 'BOTH') {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. Fetch Staff Profile
|
||||
final QueryResult<GetStaffByUserIdData, GetStaffByUserIdVariables>
|
||||
staffResponse = await _service.run(() => _service.connector
|
||||
.getStaffByUserId(
|
||||
userId: firebaseUser.uid,
|
||||
)
|
||||
.execute());
|
||||
|
||||
if (staffResponse.data.staffs.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final GetStaffByUserIdStaffs staffRecord = staffResponse.data.staffs.first;
|
||||
|
||||
// 4. Populate Session
|
||||
final domain.User domainUser = domain.User(
|
||||
id: firebaseUser.uid,
|
||||
email: user.email ?? '',
|
||||
phone: firebaseUser.phoneNumber,
|
||||
role: user.role.stringValue,
|
||||
);
|
||||
|
||||
final domain.Staff domainStaff = domain.Staff(
|
||||
id: staffRecord.id,
|
||||
authProviderId: staffRecord.userId,
|
||||
name: staffRecord.fullName,
|
||||
email: staffRecord.email ?? '',
|
||||
phone: staffRecord.phone,
|
||||
status: domain.StaffStatus.completedProfile,
|
||||
address: staffRecord.addres,
|
||||
avatar: staffRecord.photoUrl,
|
||||
);
|
||||
|
||||
StaffSessionStore.instance.setSession(
|
||||
StaffSession(
|
||||
user: domainUser,
|
||||
staff: domainStaff,
|
||||
ownerId: staffRecord.ownerId,
|
||||
),
|
||||
);
|
||||
|
||||
return domainUser;
|
||||
} catch (e) {
|
||||
// If restoration fails (network, etc), we rethrow to let UI handle it.
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,7 @@ abstract interface class AuthRepositoryInterface {
|
||||
|
||||
/// Signs out the current user.
|
||||
Future<void> signOut();
|
||||
// Future<Staff?> getStaffProfile(String userId); // Could be moved to a separate repository if needed, but useful here for routing logic.
|
||||
|
||||
/// Restores the session if a user is already logged in.
|
||||
Future<User?> restoreSession();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_modular/flutter_modular.dart';
|
||||
import 'package:krow_core/core.dart';
|
||||
import 'package:staff_authentication/src/domain/repositories/auth_repository_interface.dart';
|
||||
|
||||
class IntroPage extends StatefulWidget {
|
||||
const IntroPage({super.key});
|
||||
|
||||
@override
|
||||
State<IntroPage> createState() => _IntroPageState();
|
||||
}
|
||||
|
||||
class _IntroPageState extends State<IntroPage> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_checkSession();
|
||||
}
|
||||
|
||||
Future<void> _checkSession() async {
|
||||
// Check session immediately without artificial delay
|
||||
if (!mounted) return;
|
||||
|
||||
try {
|
||||
final AuthRepositoryInterface authRepo = Modular.get<AuthRepositoryInterface>();
|
||||
// Add a timeout to prevent infinite loading
|
||||
final user = await authRepo.restoreSession().timeout(
|
||||
const Duration(seconds: 5),
|
||||
onTimeout: () {
|
||||
// If it takes too long, navigate to Get Started.
|
||||
// This handles poor network conditions gracefully.
|
||||
throw TimeoutException('Session restore timed out');
|
||||
},
|
||||
);
|
||||
|
||||
if (mounted) {
|
||||
if (user != null) {
|
||||
Modular.to.navigate(StaffPaths.home);
|
||||
} else {
|
||||
Modular.to.navigate(StaffPaths.getStarted);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('IntroPage: Session check error: $e');
|
||||
if (mounted) {
|
||||
Modular.to.navigate(StaffPaths.getStarted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
body: Center(
|
||||
child: Image.asset(
|
||||
'assets/logo-yellow.png',
|
||||
package: 'design_system',
|
||||
width: 120,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ import 'package:staff_authentication/src/data/repositories_impl/place_repository
|
||||
import 'package:staff_authentication/src/domain/usecases/search_cities_usecase.dart';
|
||||
import 'package:staff_authentication/src/presentation/blocs/auth_bloc.dart';
|
||||
import 'package:staff_authentication/src/presentation/blocs/profile_setup/profile_setup_bloc.dart';
|
||||
import 'package:staff_authentication/src/presentation/pages/intro_page.dart';
|
||||
import 'package:staff_authentication/src/presentation/pages/get_started_page.dart';
|
||||
import 'package:staff_authentication/src/presentation/pages/phone_verification_page.dart';
|
||||
import 'package:staff_authentication/src/presentation/pages/profile_setup_page.dart';
|
||||
@@ -54,7 +55,8 @@ class StaffAuthenticationModule extends Module {
|
||||
|
||||
@override
|
||||
void routes(RouteManager r) {
|
||||
r.child(StaffPaths.root, child: (_) => const GetStartedPage());
|
||||
r.child(StaffPaths.root, child: (_) => const IntroPage());
|
||||
r.child(StaffPaths.getStarted, child: (_) => const GetStartedPage());
|
||||
r.child(
|
||||
StaffPaths.phoneVerification,
|
||||
child: (BuildContext context) {
|
||||
|
||||
Reference in New Issue
Block a user