add redirect router

This commit is contained in:
2025-08-20 11:52:07 +02:00
parent 2520afeec1
commit 68073ed062

View File

@@ -2,13 +2,24 @@ import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_objects/app_user.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_objects/arguments.dart';
import 'package:mzansi_innovation_hub/mih_packages/authentication/auth_check.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_profile/business_profile/mzansi_business_profile.dart';
import 'package:mzansi_innovation_hub/mih_components/mih_package_components/Example/package_test.dart';
import 'package:mzansi_innovation_hub/mih_packages/about_mih/about_mih.dart';
import 'package:mzansi_innovation_hub/mih_packages/access_review/mih_access.dart';
import 'package:mzansi_innovation_hub/mih_packages/authentication/forgot_password.dart';
import 'package:mzansi_innovation_hub/mih_packages/calculator/mih_calculator.dart';
import 'package:mzansi_innovation_hub/mih_packages/calendar/mzansi_calendar.dart';
import 'package:mzansi_innovation_hub/mih_packages/mih_authentication/mih_authentication.dart';
import 'package:mzansi_innovation_hub/mih_packages/mih_home/mih_home.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_ai/mzansi_ai.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_directory/mzansi_directory.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_profile/business_profile/mzansi_business_profile_view.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_profile/business_profile/profile_business_add.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_profile/personal_profile/mzansi_profile.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_profile/personal_profile/mzansi_profile_view.dart';
import 'package:ken_logger/ken_logger.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/components/mih_barcode_scanner.dart';
import 'package:mzansi_innovation_hub/mih_packages/mzansi_wallet/mih_wallet.dart';
import 'package:mzansi_innovation_hub/mih_packages/patient_profile/pat_profile/add_or_view_patient.dart';
import 'package:supertokens_flutter/supertokens.dart';
class MihGoRouterPaths {
// External
@@ -17,8 +28,9 @@ class MihGoRouterPaths {
static const String termsOfServiceExternal = '/terms-of-service';
// Internal
static const String authCheck = '/';
// static const String authCheck = '/';
static const String mihAuthentication = '/mih-authentication';
static const String mihHome = '/';
static const String notifications = '/notifications';
static const String forgotPassword = '/forgot-password';
static const String aboutMih = '/about';
@@ -47,7 +59,23 @@ class MihGoRouterPaths {
class MihGoRouter {
final GoRouter mihRouter = GoRouter(
initialLocation: MihGoRouterPaths.authCheck,
initialLocation: MihGoRouterPaths.mihHome,
redirect: (BuildContext context, GoRouterState state) async {
final bool isUserSignedIn = await SuperTokens.doesSessionExist();
// Only redirect if absolutely necessary
if (!isUserSignedIn &&
state.fullPath != MihGoRouterPaths.mihAuthentication) {
return MihGoRouterPaths.mihAuthentication;
}
if (isUserSignedIn &&
state.fullPath == MihGoRouterPaths.mihAuthentication) {
return MihGoRouterPaths.mihHome;
}
return null; // Stay on current route
},
routes: [
// External Routes - use `GoRoute` with `path` and `builder`
// GoRoute(
@@ -72,65 +100,271 @@ class MihGoRouter {
// ),
// Internal Routes - handle arguments via `extra` or path parameters
// ========================== Mih Auth ==================================
// GoRoute(
// name: "mihHome",
// path: MihGoRouterPaths.authCheck,
// builder: (BuildContext context, GoRouterState state) {
// final AuthArguments? args = state.extra as AuthArguments?;
// KenLogger.success("MihGoRouter: home");
// return AuthCheck(
// personalSelected: args?.personalSelected ?? true,
// firstBoot: args?.firstBoot ?? true,
// );
// },
// ),
// GoRoute(
// name: "mihAuthCheck",
// path: MihGoRouterPaths.authCheck,
// builder: (BuildContext context, GoRouterState state) {
// KenLogger.success("MihGoRouter: mihAuthCheck");
// return MihAuthCheck();
// },
// ),
GoRoute(
name: "home",
path: MihGoRouterPaths.authCheck,
name: "mihAuthentication",
path: MihGoRouterPaths.mihAuthentication,
builder: (BuildContext context, GoRouterState state) {
final AuthArguments? args = state.extra as AuthArguments?;
KenLogger.success("Inside MihGoRouter: home");
return AuthCheck(
personalSelected: args?.personalSelected ?? true,
firstBoot: args?.firstBoot ?? true,
KenLogger.success("MihGoRouter: mihAuthentication");
return MihAuthentication();
},
),
GoRoute(
name: "forgotPassword",
path: MihGoRouterPaths.forgotPassword,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: forgotPassword");
return const ForgotPassword();
},
),
// ========================== MIH Home ==================================
GoRoute(
name: "mihHome",
path: MihGoRouterPaths.mihHome,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: mihHome");
if (state.extra != null) {
final bool personalSelected = state.extra as bool;
return MihHome(
personalSelected: personalSelected,
);
}
return MihHome(
personalSelected: true,
);
},
),
// ========================== About MIH ==================================
GoRoute(
name: "aboutMih",
path: MihGoRouterPaths.aboutMih,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: aboutMih");
final int? packageIndex = state.extra as int?;
int index = 0;
if (packageIndex != null) {
index = packageIndex;
}
return AboutMih(packageIndex: index);
},
),
// ========================== Mzansi Profile Personal ==================================
GoRoute(
name: "mzansiProfileManage",
path: MihGoRouterPaths.mzansiProfileManage,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("Inside MihGoRouter: mzansiProfileManage");
KenLogger.success("MihGoRouter: mzansiProfileManage");
final AppProfileUpdateArguments? args =
state.extra as AppProfileUpdateArguments?;
return MzansiProfile(arguments: args!);
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MzansiProfile(arguments: args);
},
),
GoRoute(
name: "mzansiProfileView",
path: MihGoRouterPaths.mzansiProfileView,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("Inside MihGoRouter: mzansiProfileView");
KenLogger.success("MihGoRouter: mzansiProfileView");
final AppUser? user = state.extra as AppUser?;
return MzansiProfileView(user: user!);
},
),
GoRoute(
name: "businessProfileSetup",
path: MihGoRouterPaths.businessProfileSetup,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("Inside MihGoRouter: businessProfileSetup");
final AppUser? signedInUser = state.extra as AppUser?;
return ProfileBusinessAdd(signedInUser: signedInUser!);
},
),
GoRoute(
name: "businessProfileManage",
path: MihGoRouterPaths.businessProfileManage,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("Inside MihGoRouter: businessProfileManage");
final BusinessArguments? args = state.extra as BusinessArguments?;
return MzansiBusinessProfile(arguments: args!);
if (user == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MzansiProfileView(user: user);
},
),
// ========================== Mzansi Profile Business ==================================
GoRoute(
name: "businessProfileView",
path: MihGoRouterPaths.businessProfileView,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("Inside MihGoRouter: businessProfileView");
KenLogger.success("MihGoRouter: businessProfileView");
final BusinessViewArguments? args =
state.extra as BusinessViewArguments?;
return MzansiBusinessProfileView(arguments: args!);
},
),
// ========================== MIH Calculator ==================================
GoRoute(
name: "mihCalculator",
path: MihGoRouterPaths.calculator,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: mihCalculator");
final bool? personalSelected = state.extra as bool?;
bool personal = true;
if (personalSelected != null) {
personal = personalSelected;
}
return MIHCalculator(personalSelected: personal);
},
),
// ========================== MIH Calculator ==================================
GoRoute(
name: "mihCalendar",
path: MihGoRouterPaths.calendar,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: mihCalendar");
final CalendarArguments? args = state.extra as CalendarArguments?;
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MzansiCalendar(
key: UniqueKey(),
arguments: args,
);
},
),
// ========================== Mzansi AI ==================================
GoRoute(
name: "mzansiAi",
path: MihGoRouterPaths.mzansiAi,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: mzansiAi");
final MzansiAiArguments? args = state.extra as MzansiAiArguments?;
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MzansiAi(arguments: args);
},
),
// ========================== Mzansi Wallet ==================================
GoRoute(
name: "mzansiWallet",
path: MihGoRouterPaths.mzansiWallet,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: mzansiWallet");
final WalletArguments? args = state.extra as WalletArguments?;
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MihWallet(
key: UniqueKey(),
arguments: args,
);
},
),
GoRoute(
name: "barcodeScanner",
path: MihGoRouterPaths.barcodeScanner,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: barcodeScanner");
final TextEditingController? args =
state.extra as TextEditingController?;
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return MihBarcodeScanner(cardNumberController: args);
},
),
// ========================== Test Package ==================================
GoRoute(
name: "testPackage",
path: MihGoRouterPaths.packageDevTest,
builder: (BuildContext context, GoRouterState state) {
KenLogger.success("MihGoRouter: testPackage");
final TestArguments? args = state.extra as TestArguments?;
if (args == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
context.go(MihGoRouterPaths.mihHome);
});
return const SizedBox.shrink();
}
return PackageTest(arguments: args);
},
),
// ========================== MIH Access Controls ==================================
GoRoute(
name: "mihAccess",
path: MihGoRouterPaths.mihAccess,
builder: (BuildContext context, GoRouterState state) {
final AppUser? signedInUser = state.extra as AppUser?;
return MihAccess(
key: UniqueKey(),
signedInUser: signedInUser!,
);
},
),
// ========================== Patient Profile ==================================
GoRoute(
name: "patientProfile",
path: MihGoRouterPaths.patientProfile,
builder: (BuildContext context, GoRouterState state) {
final PatientViewArguments args = state.extra as PatientViewArguments;
return AddOrViewPatient(
key: UniqueKey(),
arguments: args,
);
},
),
// ========================== Mzansi Directory ==================================
GoRoute(
name: "mzansiDirectory",
path: MihGoRouterPaths.mzansiDirectory,
builder: (BuildContext context, GoRouterState state) {
final MzansiDirectoryArguments? args =
state.extra as MzansiDirectoryArguments?;
return MzansiDirectory(arguments: args!);
},
),
// ========================== End ==================================
// GoRoute(
// name: "businessProfileSetup",
// path: MihGoRouterPaths.businessProfileSetup,
// builder: (BuildContext context, GoRouterState state) {
// KenLogger.success("MihGoRouter: businessProfileSetup");
// final AppUser? signedInUser = state.extra as AppUser?;
// return ProfileBusinessAdd(signedInUser: signedInUser!);
// },
// ),
// GoRoute(
// name: "businessProfileManage",
// path: MihGoRouterPaths.businessProfileManage,
// builder: (BuildContext context, GoRouterState state) {
// KenLogger.success("MihGoRouter: businessProfileManage");
// final BusinessArguments? args = state.extra as BusinessArguments?;
// return MzansiBusinessProfile(arguments: args!);
// },
// ),
// GoRoute(
// name: "mihAuthentication",
// path: MihGoRouterPaths.mihAuthentication,
@@ -138,14 +372,6 @@ class MihGoRouter {
// MihAuthentication(),
// ),
// GoRoute(
// name: "mzansiDirectory",
// path: MihGoRouterPaths.mzansiDirectory,
// builder: (BuildContext context, GoRouterState state) {
// final MzansiDirectoryArguments? args = state.extra as MzansiDirectoryArguments?;
// return MzansiDirectory(arguments: args!);
// },
// ),
// GoRoute(
// name: "notifications",
// path: MihGoRouterPaths.notifications,
// builder: (BuildContext context, GoRouterState state) {
@@ -154,28 +380,6 @@ class MihGoRouter {
// },
// ),
// GoRoute(
// name: "forgotPassword",
// path: MihGoRouterPaths.forgotPassword,
// builder: (BuildContext context, GoRouterState state) =>
// const ForgotPassword(),
// ),
// GoRoute(
// name: "aboutMih",
// path: MihGoRouterPaths.aboutMih,
// builder: (BuildContext context, GoRouterState state) {
// final int? packageIndex = state.extra as int?;
// return AboutMih(packageIndex: packageIndex);
// },
// ),
// GoRoute(
// name: "patientProfile",
// path: MihGoRouterPaths.patientProfile,
// builder: (BuildContext context, GoRouterState state) {
// final PatientViewArguments? args = state.extra as PatientViewArguments?;
// return AddOrViewPatient(arguments: args!);
// },
// ),
// GoRoute(
// name: "patientProfileSetup",
// path: MihGoRouterPaths.patientProfileSetup,
// builder: (BuildContext context, GoRouterState state) {
@@ -194,30 +398,6 @@ class MihGoRouter {
// );
// },
// ),
// GoRoute(
// name: "mzansiWallet",
// path: MihGoRouterPaths.mzansiWallet,
// builder: (BuildContext context, GoRouterState state) {
// final WalletArguments? args = state.extra as WalletArguments?;
// return MihWallet(arguments: args!);
// },
// ),
// GoRoute(
// name: "mihAccess",
// path: MihGoRouterPaths.mihAccess,
// builder: (BuildContext context, GoRouterState state) {
// final AppUser? signedInUser = state.extra as AppUser?;
// return MihAccess(signedInUser: signedInUser!);
// },
// ),
// GoRoute(
// name: "mihCalendar",
// path: MihGoRouterPaths.calendar,
// builder: (BuildContext context, GoRouterState state) {
// final CalendarArguments? args = state.extra as CalendarArguments?;
// return MzansiCalendar(arguments: args!);
// },
// ),
// // Note: You can't have two separate GoRoutes with the same path.
// // 'appointments' and 'calendar' now need a distinct path, or be sub-routes.
// // Here, we'll assume they should be separate.
@@ -260,44 +440,12 @@ class MihGoRouter {
// final PrintPreviewArguments? args = state.extra as PrintPreviewArguments?;
// return MIHPrintPreview(arguments: args!);
// },
// ),
// GoRoute(
// name: "barcodeScanner",
// path: MihGoRouterPaths.barcodeScanner,
// builder: (BuildContext context, GoRouterState state) {
// final TextEditingController? args = state.extra as TextEditingController?;
// return MihBarcodeScanner(cardNumberController: args!);
// },
// ),
// GoRoute(
// name: "mihCalculator",
// path: MihGoRouterPaths.calculator,
// builder: (BuildContext context, GoRouterState state) {
// final bool? personalSelected = state.extra as bool?;
// return MIHCalculator(personalSelected: personalSelected!);
// },
// ),
// GoRoute(
// name: "mzansiAi",
// path: MihGoRouterPaths.mzansiAi,
// builder: (BuildContext context, GoRouterState state) {
// final MzansiAiArguments? args = state.extra as MzansiAiArguments?;
// return MzansiAi(arguments: args!);
// },
// ),
// GoRoute(
// name: "testPackage",
// path: MihGoRouterPaths.packageDevTest,
// builder: (BuildContext context, GoRouterState state) {
// final TestArguments? args = state.extra as TestArguments?;
// return PackageTest(arguments: args!);
// },
// ),
],
// 3. Error handling with `errorBuilder` and `redirect`
errorBuilder: (BuildContext context, GoRouterState state) {
KenLogger.error('Invalid Route');
return const AuthCheck(personalSelected: true, firstBoot: true);
return const Placeholder();
},
);
}