From e387bd783c51f3f15b9833cb396316152601d531 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Thu, 22 May 2025 08:53:43 +0000 Subject: [PATCH 1/2] apicall --- .../authentication/login/login_widget.dart | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/lib/Screens/authentication/login/login_widget.dart b/lib/Screens/authentication/login/login_widget.dart index 0446170..13cf433 100644 --- a/lib/Screens/authentication/login/login_widget.dart +++ b/lib/Screens/authentication/login/login_widget.dart @@ -1,4 +1,6 @@ import 'dart:convert'; +import 'dart:html' as html; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:frontend/config/apiUrl.dart'; @@ -709,7 +711,9 @@ class _LoginWidgetState extends State { children: [ Expanded( child: ElevatedButton( - onPressed: () => (), + onPressed: () { + handleMS(); + }, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, // Button color foregroundColor: Colors.black, // Text color @@ -815,4 +819,56 @@ class _LoginWidgetState extends State { ), ); } + + + Future handleMS() async { + + final url = '$apiUrl/auth/mslogin'; + print(url); + try { + final response = await http.get( + Uri.parse(url), + headers: { 'Content-Type': 'application/json' } + ); + print("inside try method"); + if (response.statusCode == 200) { + final authUrl = json.decode(response.body)['auth_url']; + print("authurl - $authUrl"); + if(authUrl != ''){ + // final prefs = await SharedPreferences.getInstance(); + // await prefs.setString('auth_token', authUrl); + print('i have auth URL'); + // canLaunchUrl(authUrl); + + if (kIsWeb) { + print("kIsWeb"); + // Use web redirect (e.g., via JS interop or window.location.href) + // redirectTo(url); + + html.window.location.href = authUrl; + } else { + // For mobile/desktop, open in external browser + // launchUrl(Uri.parse(url), + // mode: LaunchMode.externalApplication); + } + // final result = await FlutterWebAuth.authenticate( + // url: authUrl, + // callbackUrlScheme: "myapp", // Use a custom scheme you registered + // ); + }else{ + print('auth URL not Founded'); + throw Exception('auth URL not Founded'); + } + }else{ + final errorMessage = json.decode(response.body)['message']; + print(errorMessage); + throw Exception(errorMessage); + } + } + catch (e) { + print("Error: $e"); + } + + } + } From e4acf06234f7e9bb380739e4214657527f9778a6 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Thu, 22 May 2025 09:49:17 +0000 Subject: [PATCH 2/2] token from microsoft account --- lib/app.dart | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/lib/app.dart b/lib/app.dart index cd137fe..4decf06 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -22,6 +22,11 @@ import 'package:frontend/routes/custom_router.dart'; 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:http/http.dart' as http; // 2 newly added +import 'dart:convert'; // 3 newly added + +import 'package:shared_preferences/shared_preferences.dart'; class MyApp extends StatefulWidget { const MyApp({super.key}); @@ -32,6 +37,7 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { String? _authCode; + String? userRole; bool _isAuthRedirect = false; @override void initState() { @@ -51,10 +57,80 @@ class _MyAppState extends State { // - call an API // - store this in a Provider // - or just pass it through GoRouter as usual + + // if(_authCode){ + handleTokenUsingMS(_authCode); + // }else{ + // _isAuthRedirect = false; + // } + } } } + Future handleTokenUsingMS(_authCode) async { + + final url = '$apiUrl/auth/auth/verifyMSAuthUser?code=$_authCode'; + try { + 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 != ''){ + print('Microsoft - Token Available'); + await storeUserDetails(MS_Token); + }else{ + print('Microsoft - Token Not Available'); + throw Exception('Token not Founded'); + } + }else{ + final errorMessage = json.decode(response.body)['message']; + print(errorMessage); + throw Exception(errorMessage); + } + } + catch (e) { + print("Error: $e"); + } + + } + + Future storeUserDetails(String token) async { + try { + final parts = token.split('.'); + if (parts.length != 3) throw Exception('Invalid token format'); + + final payload = json + .decode(utf8.decode(base64Url.decode(base64Url.normalize(parts[1])))); + + final userData = payload['data']; + + final prefs = await SharedPreferences.getInstance(); + await prefs.setString('auth_token', token); + await prefs.setString( + 'user_data', jsonEncode(userData)); // Store full user data + + if (userData != null) { + final pref = await SharedPreferences.getInstance(); + await pref.setString('auth_token', token); + await pref.setString('user_data', jsonEncode(userData)); + + userRole = userData['role']; + + print("userData - $userData"); + print("userData11 - ${userData['role']}"); + print("userData12 - $userRole"); + } + } catch (e) { + print('Error decoding token: $e'); + } + } + @override Widget build(BuildContext context) { // if (_isAuthRedirect) {