import 'dart:async'; import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:fluffychat/widgets/matrix.dart'; import '../controllers/pangea_controller.dart'; class PAuthGaurd { static bool isPublicLeaving = false; static PangeaController? pController; /// Redirect for /home routes static FutureOr homeRedirect( BuildContext context, GoRouterState state, ) async { if (pController == null) { return Matrix.of(context).client.isLogged() ? '/rooms' : null; } final isLogged = Matrix.of(context).widget.clients.any((client) => client.isLogged()); if (!isLogged) return null; // If user hasn't set their L2, // and their URL doesn’t include ‘course,’ redirect final bool hasSetL2 = await pController!.userController.isUserL2Set; return !hasSetL2 ? '/registration/create' : '/rooms'; } /// Redirect for /rooms routes static FutureOr roomsRedirect( BuildContext context, GoRouterState state, ) async { if (pController == null) { return Matrix.of(context).client.isLogged() ? null : '/home'; } final isLogged = Matrix.of(context).widget.clients.any((client) => client.isLogged()); if (!isLogged) { return '/home'; } // If user hasn't set their L2, // and their URL doesn’t include ‘course,’ redirect final bool hasSetL2 = await pController!.userController.isUserL2Set; return !hasSetL2 ? '/registration/create' : null; } /// Redirect for onboarding routes static FutureOr onboardingRedirect( BuildContext context, GoRouterState state, ) async { if (pController == null) { return Matrix.of(context).client.isLogged() ? null : '/home'; } final isLogged = Matrix.of(context).widget.clients.any( (client) => client.isLogged(), ); if (!isLogged) { return '/home'; } return null; } }