diff --git a/lib/app.dart b/lib/app.dart index 4decf06..1ca11b7 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -23,6 +23,7 @@ import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_quill/flutter_quill.dart' hide Text; import 'dart:html' as html; import 'package:frontend/config/apiUrl.dart'; // 1 newly added +import 'package:go_router/go_router.dart'; import 'package:http/http.dart' as http; // 2 newly added import 'dart:convert'; // 3 newly added @@ -53,51 +54,50 @@ class _MyAppState extends State { // print(">>> _isAuthRedirect: $_isAuthRedirect"); print(">>> Auth code found at startup: $_authCode"); - // You can now: - // - call an API - // - store this in a Provider - // - or just pass it through GoRouter as usual - - // if(_authCode){ + WidgetsBinding.instance.addPostFrameCallback((_) { handleTokenUsingMS(_authCode); - // }else{ - // _isAuthRedirect = false; - // } - + }); } } } - Future handleTokenUsingMS(_authCode) async { + Future handleTokenUsingMS(authCode) async { + if (authCode == null) return; - final url = '$apiUrl/auth/auth/verifyMSAuthUser?code=$_authCode'; + final url = '$apiUrl/auth/verifyMSAuthUser?code=$authCode'; try { - final response = await http.get( - Uri.parse(url), - headers: { 'Content-Type': 'application/json' } - ); + final response = await http + .get(Uri.parse(url), headers: {'Content-Type': 'application/json'}); if (response.statusCode == 200) { final MS_Token = json.decode(response.body)['token']; print("MS_Token - $MS_Token"); - if(MS_Token != ''){ + if (MS_Token != '') { print('Microsoft - Token Available'); await storeUserDetails(MS_Token); - }else{ + + print("userRole - $userRole"); + + if (userRole == "Travel Agent") { + router.go('/listTravelAgentPlan'); + } else if (userRole == "Org Admin" || userRole == "Travel Admin") { + router.go('/listAllPlan'); + } else { + router.go('/listPlan'); + } + } else { print('Microsoft - Token Not Available'); throw Exception('Token not Founded'); } - }else{ + } else { final errorMessage = json.decode(response.body)['message']; print(errorMessage); throw Exception(errorMessage); } - } - catch (e) { + } catch (e) { print("Error: $e"); } - } Future storeUserDetails(String token) async {