From 54585b163d0dd026aee1863b30779a7bdc5818f3 Mon Sep 17 00:00:00 2001 From: juan Date: Fri, 22 Nov 2024 02:19:31 -0500 Subject: [PATCH 1/7] functionality to not show login screen, if alr logged in --- lib/login.dart | 242 ++++++++++++++++++++++++++++++------------------- lib/main.dart | 5 +- 2 files changed, 152 insertions(+), 95 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index 55daed0..c611a5d 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -1,6 +1,9 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; +import 'package:shared_preferences/shared_preferences.dart'; +import 'dart:io'; +import 'package:path_provider/path_provider.dart'; class AuthService { Future isUserLoggedIn() async { @@ -10,15 +13,21 @@ class AuthService { print(response.body); if (response.statusCode == 200) { print('all good in the east!'); - List json = jsonDecode(response.body); - print(json[0]); - return true; + String jsonOuter = jsonDecode(response.body); + Map json = jsonDecode(jsonOuter); + // print(json["is_logged_in"]); + return json["is_logged_in"]; } } catch (e) { print(e); } return false; } + + Future loginUser(String email, String password) async { + try {} catch (e) {} + return false; + } } class LoginPage extends StatefulWidget { @@ -28,6 +37,43 @@ class LoginPage extends StatefulWidget { _LoginPageState createState() => _LoginPageState(); } +class SplashScreen extends StatefulWidget { + @override + _SplashScreenState createState() => _SplashScreenState(); +} + +class _SplashScreenState extends State { + final AuthService _authService = AuthService(); + @override + void initState() { + super.initState(); + _checkLoginStatus(); + } + + Future _checkLoginStatus() async { + // SharedPreferences prefs = await SharedPreferences.getInstance(); + // print(prefs); + // bool isLoggedIn = prefs.getBool('isLoggedIn') ?? false; + bool isLoggedIn = await _authService.isUserLoggedIn(); + print("is loogeed in $isLoggedIn"); + if (isLoggedIn) { + print('here'); + Navigator.pushReplacementNamed(context, '/home'); + } else { + Navigator.pushReplacementNamed(context, '/login'); + } + } + + @override + Widget build(BuildContext context) { + return Center( + child: Scaffold( + body: Center(child: CircularProgressIndicator()), + ), + ); + } +} + class _LoginPageState extends State { final AuthService _authService = AuthService(); // Controllers for capturing user input @@ -37,20 +83,19 @@ class _LoginPageState extends State { // Key to identify the form final _formKey = GlobalKey(); - void checkLogin() async { - // try { - // var url = Uri.http('127.0.0.1:3001', 'is_logged_in'); - // var response = await http.get(url); - // print(response.body); - // if (response.statusCode == 200) { - // print('all good on the west'); - // } - // } catch (e) { - // print(e); - // } - bool isLoggedIn = await _authService.isUserLoggedIn(); - - } + // void checkLogin() async { + // try { + // var url = Uri.http('127.0.0.1:3001', 'is_logged_in'); + // var response = await http.get(url); + // print(response.body); + // if (response.statusCode == 200) { + // print('all good on the west'); + // } + // } catch (e) { + // print(e); + // } + // // bool isLoggedIn = await _authService.isUserLoggedIn(); + // } // Function to handle login action void _handleLogin() { @@ -71,86 +116,95 @@ class _LoginPageState extends State { @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: const Text('Login Page'), - ), - body: Padding( - padding: const EdgeInsets.all(16.0), - child: Form( - key: _formKey, - child: Align( - alignment: Alignment.centerLeft, - child: Column( - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text('Sign in to your email'), - SizedBox( - height: 5, - ), - // Email Field - Container( - width: 200, - child: TextFormField( - controller: _emailController, - decoration: const InputDecoration( - labelText: 'Email', - hintText: 'Enter your email', - helperMaxLines: 1, + return Center( + child: Scaffold( + appBar: AppBar( + title: const Text('Login Page'), + ), + body: Center( + child: SingleChildScrollView( + padding: const EdgeInsets.all(16.0), + child: Form( + key: _formKey, + child: Align( + alignment: Alignment.center, + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text('Sign in to your email'), + SizedBox( + height: 5, ), - validator: (value) { - // Simple email validation - if (value == null || value.isEmpty) { - return 'Please enter your email'; - } - if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) { - return 'Please enter a valid email address'; - } - return null; - }, - ), - ), - const SizedBox(height: 16.0), - // Password Field - Container( - width: 200, - child: TextFormField( - controller: _passwordController, - decoration: const InputDecoration( - labelText: 'Password', - hintText: 'Enter your password', + // Email Field + Container( + width: 200, + child: TextFormField( + controller: _emailController, + decoration: const InputDecoration( + labelText: 'Email', + hintText: 'Enter your email', + helperMaxLines: 1, + ), + validator: (value) { + // Simple email validation + if (value == null || value.isEmpty) { + return 'Please enter your email'; + } + if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) { + // print(value); + return 'Please enter a valid email address'; + } + return null; + }, + ), ), - obscureText: true, // Hide the password text - validator: (value) { - // Simple password validation - if (value == null || value.isEmpty) { - return 'Please enter your password'; - } - if (value.length < 6) { - return 'Password must be at least 6 characters long'; - } - return null; - }, - ), + const SizedBox(height: 16.0), + // Password Field + Container( + width: 200, + child: TextFormField( + controller: _passwordController, + decoration: const InputDecoration( + labelText: 'Password', + hintText: 'Enter your password', + ), + obscureText: true, // Hide the password text + validator: (value) { + // Simple password validation + if (value == null || value.isEmpty) { + return 'Please enter your password'; + } + if (value.length < 6) { + return 'Password must be at least 6 characters long'; + } + return null; + }, + ), + ), + const SizedBox(height: 32.0), + // Login Button + SizedBox( + width: 200, + child: ElevatedButton( + onPressed: _handleLogin, + child: const Text('Login'), + ), + ), + SizedBox( + width: 200, + child: ElevatedButton( + // onPressed: checkLogin, + onPressed: () async { + await _authService.isUserLoggedIn(); + // print(result); + }, + child: const Text('checker'), + ), + ) + ], ), - const SizedBox(height: 32.0), - // Login Button - SizedBox( - width: 200, - child: ElevatedButton( - onPressed: _handleLogin, - child: const Text('Login'), - ), - ), - SizedBox( - width: 200, - child: ElevatedButton( - onPressed: checkLogin, - child: const Text('checker'), - ), - ) - ], + ), ), ), ), diff --git a/lib/main.dart b/lib/main.dart index 84b104a..081af44 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -22,10 +22,13 @@ class HyM extends StatelessWidget { theme: ThemeData.light(), title: 'HyM', // home: HomeScreen(), - home: HomeScreen(), + // home: HomeScreen(), + initialRoute: "/", routes: { + "/": (context) => SplashScreen(), "/login": (context) => const LoginPage(), + "/home": (context) => HomeScreen(), // "/email": (context) => EmailPage(), "/contacts": (context) => ContactsPage(), }, -- 2.34.1 From 89a17e2354b6f4b5334c8fa9030982d81e86c7ac Mon Sep 17 00:00:00 2001 From: juan Date: Sat, 23 Nov 2024 01:56:35 -0500 Subject: [PATCH 2/7] got the login working to read and write data to the conf --- lib/login.dart | 171 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 153 insertions(+), 18 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index c611a5d..6f49470 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -1,9 +1,10 @@ import 'dart:convert'; +import 'package:crab_ui/configManager.dart'; import 'package:flutter/material.dart'; +// import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; import 'package:http/http.dart' as http; -import 'package:shared_preferences/shared_preferences.dart'; -import 'dart:io'; -import 'package:path_provider/path_provider.dart'; +// import 'package:shared_preferences/shared_preferences.dart'; +import 'package:flutter/services.dart' show rootBundle; class AuthService { Future isUserLoggedIn() async { @@ -24,6 +25,29 @@ class AuthService { return false; } + Future readConfFile() async { + final content = await rootBundle.loadString('/crabmail.conf'); + return content; + } + + Map parseConfFile(String content) { + final Map config = {}; + final lines = content.split('\n'); + + for (var line in lines) { + line = line.trim(); + if (line.isEmpty || line.startsWith('#')) continue; + + final parts = line.split('='); + if (parts.length == 2) { + final key = parts[0].trim(); + final value = parts[1].trim(); + config[key] = value; + } + } + return config; + } + Future loginUser(String email, String password) async { try {} catch (e) {} return false; @@ -57,7 +81,6 @@ class _SplashScreenState extends State { bool isLoggedIn = await _authService.isUserLoggedIn(); print("is loogeed in $isLoggedIn"); if (isLoggedIn) { - print('here'); Navigator.pushReplacementNamed(context, '/home'); } else { Navigator.pushReplacementNamed(context, '/login'); @@ -77,12 +100,29 @@ class _SplashScreenState extends State { class _LoginPageState extends State { final AuthService _authService = AuthService(); // Controllers for capturing user input + final TextEditingController _ipController = TextEditingController(); + final TextEditingController _portController = TextEditingController(); final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController(); + // final ConfigManager _configManager = + // ConfigManager("${Directory.current.parent}../crabmail2.conf"); // Key to identify the form final _formKey = GlobalKey(); + Future setIp(String ip) async { + // _configManager.setField("api_addr", ip); + return false; + } + + Future setPort(String port) async { + return false; + } + + // Future _checkConfiguration() async { + // return false; + // } + // void checkLogin() async { // try { // var url = Uri.http('127.0.0.1:3001', 'is_logged_in'); @@ -98,24 +138,74 @@ class _LoginPageState extends State { // } // Function to handle login action - void _handleLogin() { + Future _handleLogin() async { if (_formKey.currentState!.validate()) { // Perform login action (e.g., authenticate with backend) + String ip = _ipController.text; + String port = _portController.text; String email = _emailController.text; String password = _passwordController.text; // For demonstration, just print the values print('Email: $email'); print('Password: $password'); + print(ip); + print(port); + final String baseUrl = "http://127.0.0.1:5000"; + + try { + final response = await http.get(Uri.parse('${baseUrl}/read-config')); + print(response.statusCode); + print(response.body); + if (response.statusCode == 200) { + final data = jsonDecode(response.body); + // return data['config']; + } + } catch (e) { + print("caught in catch"); + print(e); + } + Map updates = { + "username": email, + "password": password, + "api_addr": ip, + "api_port": port, + }; + print("past"); + + try { + final sending = await http.post(Uri.parse('$baseUrl/update-config'), + headers: {'Content-Type': "application/json"}, + body: jsonEncode(updates)); + print("sending"); + } catch (e) { + print(e); + } + + // final content = await _authService.readConfFile(); + // final config = await _authService.parseConfFile(content); + // print("BASE URL ${config["base_url"]}"); + // print("api address ${config["api_addr"]}"); + // print(config); + // const url = '' // Clear the input fields - _emailController.clear(); - _passwordController.clear(); + // _ipController.clear(); + // _portController.clear(); + // _emailController.clear(); + // _passwordController.clear(); } } @override Widget build(BuildContext context) { + // try { + // _configManager.loadConfig(); + // print(_configManager.getField('base_url')); + // } catch (e) { + // print("broke at build $e"); + // } + // _configManager. return Center( child: Scaffold( appBar: AppBar( @@ -136,6 +226,50 @@ class _LoginPageState extends State { SizedBox( height: 5, ), + Container( + width: 200, + child: TextFormField( + controller: _ipController, + decoration: const InputDecoration( + labelText: "IP", + hintText: 'Enter your IP for the backend', + helperMaxLines: 1, + ), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter your ip'; + } + }, + // onSaved: (value) async { + // final content = await _authService.readConfFile(); + // final config = + // await _authService.parseConfFile(content); + // print("BASE URL ${config["base_url"]}"); + // print("api address ${config["api_addr"]}"); + // //TODO: call a function to set the field ip in conf + // }, + ), + ), + Container( + width: 200, + child: TextFormField( + controller: _portController, + decoration: const InputDecoration( + labelText: "Port", + hintText: 'Enter the port', + helperMaxLines: 1, + ), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter your port'; + } + return null; + }, + onSaved: (value) { + //TODO: call a function to set the field port in conf + }, + ), + ), // Email Field Container( width: 200, @@ -170,6 +304,7 @@ class _LoginPageState extends State { hintText: 'Enter your password', ), obscureText: true, // Hide the password text + autofillHints: null, validator: (value) { // Simple password validation if (value == null || value.isEmpty) { @@ -191,17 +326,17 @@ class _LoginPageState extends State { child: const Text('Login'), ), ), - SizedBox( - width: 200, - child: ElevatedButton( - // onPressed: checkLogin, - onPressed: () async { - await _authService.isUserLoggedIn(); - // print(result); - }, - child: const Text('checker'), - ), - ) + // SizedBox( + // width: 200, + // child: ElevatedButton( + // // onPressed: checkLogin, + // onPressed: () async { + // await _authService.isUserLoggedIn(); + // // print(result); + // }, + // child: const Text('checker'), + // ), + // ) ], ), ), -- 2.34.1 From a0652ba5c5053ec52738ab6b358c0824336ec6d7 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 24 Nov 2024 17:01:07 -0500 Subject: [PATCH 3/7] added the login, it works but it needs a cleaning --- lib/login.dart | 47 +++++++++++++++++++++++++++++++++++++++++------ pubspec.yaml | 4 +++- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index 6f49470..23ba997 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -1,5 +1,5 @@ import 'dart:convert'; -import 'package:crab_ui/configManager.dart'; +import 'package:crab_ui/home_page.dart'; import 'package:flutter/material.dart'; // import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; import 'package:http/http.dart' as http; @@ -119,6 +119,12 @@ class _LoginPageState extends State { return false; } + Future login() async { + bool result = await _handleLogin(); + if (result) { + Navigator.pushReplacementNamed(context, '/home'); + } + } // Future _checkConfiguration() async { // return false; // } @@ -138,7 +144,7 @@ class _LoginPageState extends State { // } // Function to handle login action - Future _handleLogin() async { + Future _handleLogin() async { if (_formKey.currentState!.validate()) { // Perform login action (e.g., authenticate with backend) String ip = _ipController.text; @@ -152,10 +158,10 @@ class _LoginPageState extends State { print(ip); print(port); - final String baseUrl = "http://127.0.0.1:5000"; + String baseUrl = "http://127.0.0.1:5000"; try { - final response = await http.get(Uri.parse('${baseUrl}/read-config')); + final response = await http.get(Uri.parse('$baseUrl/read-config')); print(response.statusCode); print(response.body); if (response.statusCode == 200) { @@ -165,6 +171,7 @@ class _LoginPageState extends State { } catch (e) { print("caught in catch"); print(e); + return false; } Map updates = { "username": email, @@ -181,8 +188,35 @@ class _LoginPageState extends State { print("sending"); } catch (e) { print(e); + return false; } - + try { + // String status = await http.post(Uri.parse('')) + var url_log = Uri.http('127.0.0.1:3001', 'log_in'); + Map filteredData = { + 'email': updates['username'], + 'password': updates['password'] + }; + print(filteredData); + var status = await http.post( + url_log, + headers: { + 'Content-Type': 'application/json', + }, + body: jsonEncode(filteredData), + ); + if (status.statusCode == 200) { + print('response status ${status.body}'); + if (status.body == "Successful log in") { + return true; + } + } + } catch (e) { + print(e); + return false; + } + print("after"); + return false; // final content = await _authService.readConfFile(); // final config = await _authService.parseConfFile(content); // print("BASE URL ${config["base_url"]}"); @@ -195,6 +229,7 @@ class _LoginPageState extends State { // _emailController.clear(); // _passwordController.clear(); } + return false; } @override @@ -322,7 +357,7 @@ class _LoginPageState extends State { SizedBox( width: 200, child: ElevatedButton( - onPressed: _handleLogin, + onPressed: login, child: const Text('Login'), ), ), diff --git a/pubspec.yaml b/pubspec.yaml index a985a82..9bc58aa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,7 +14,9 @@ dependencies: http: 1.2.2 flutter_html_all: 3.0.0-beta.2 flutter_widget_from_html: ^0.10.0 - + shared_preferences: ^2.0.6 + encrypt: ^5.0.0 + pointycastle: ^3.4.0 english_words: ^4.0.0 provider: ^6.0.0 -- 2.34.1 From ed9d2d8a7bf075369dfb723c30018a51909ea581 Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 12 Dec 2024 12:47:39 -0500 Subject: [PATCH 4/7] latest bug with sonic --- lib/api_service.dart | 58 ++++++++++++++++++++++++++++++++++++++------ lib/main.dart | 8 ++---- lib/structs.dart | 23 +++++++++++++++--- pubspec.yaml | 1 + 4 files changed, 73 insertions(+), 17 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 8136798..7ba5512 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -13,7 +13,7 @@ class ApiService { Future> fetchEmailsFromFolder( String folder, int pagenitaion) async { try { - var url = Uri.http('127.0.0.1:3001', 'sorted_threads_by_date', { + var url = Uri.http('0.0.0.0:3001', 'sorted_threads_by_date', { 'folder': folder, 'limit': '50', 'offset': pagenitaion.toString(), @@ -49,7 +49,7 @@ class ApiService { List allEmails) async { try { var url = - Uri.http('127.0.0.1:3001', 'get_thread', {'id': threadId.toString()}); + Uri.http('0.0.0.0:3001', 'get_thread', {'id': threadId.toString()}); var response = await http.get(url); if (response.statusCode == 200) { @@ -70,15 +70,16 @@ class ApiService { Future> sonicSearch( String list, int limit, int offset, String query) async { try { - var url = Uri.http('127.0.0.1:3001', 'search', { + var url = Uri.http('0.0.0.0:3001', 'search_emails', { 'list': list, 'limit': limit.toString(), 'offset': offset.toString(), 'query': query }); + print(url); var response = await http.get(url); - + print(response); if (response.statusCode == 200) { List messagesJson = json.decode(response.body); List messages = @@ -86,6 +87,7 @@ class ApiService { return messages; } + print(response.statusCode); } catch (e) { print("caught $e"); } @@ -99,12 +101,17 @@ class ApiService { try { //attaches email after email from a thread for (var id in IDs) { - var url = Uri.http('127.0.0.1:3001', 'email', {'id': id}); + var url = Uri.http('0.0.0.0:3001', 'email', {'id': id}); var response = await http.get(url); if (response.statusCode == 200) { content += response.body; + try { + getAttachmentsInfo("INBOX", id); + } catch (innerError) { + print('_getAttachment info caught error $innerError'); + } content += "
"; } } @@ -121,7 +128,7 @@ class ApiService { Future> fetchFolders() async { try { - var url = Uri.http('127.0.0.1:3001', 'folders'); + var url = Uri.http('0.0.0.0:3001', 'folders'); var response = await http.get(url); return List.from(json.decode(response.body)); } catch (e) { @@ -131,7 +138,7 @@ class ApiService { } Future createFolder(String folderName) async { - var url = Uri.http('127.0.0.1:3001', 'create_folder'); + var url = Uri.http('0.0.0.0:3001', 'create_folder'); Map requestBody = {'name': folderName}; @@ -154,7 +161,7 @@ class ApiService { } Future deleteFolder(String folderName) async { - var url = Uri.http('127.0.0.1:3001', 'delete_folder'); + var url = Uri.http('0.0.0.0:3001', 'delete_folder'); Map requestBody = {'name': folderName}; @@ -175,6 +182,41 @@ class ApiService { print('error making post req: $e'); } } + + Future logIn(String json) async { + // var url = Uri.https('') + // try{ + // String response = await http.post( + // url + // ); + // } + + return false; + } + + Future> getAttachmentsInfo( + String folder, String email_id) async { + try { + var url = Uri.http('0.0.0.0:3001', 'get_attachments_info', + {'folder': folder, 'email_id': email_id}); + print(url); + var response = await http.get(url); + print("response $response"); + if (response.statusCode == 200) { + var result = response.body; + List attachmentList = json.decode(result); + print("attachment list $attachmentList"); + List attachments = + attachmentList.map((al) => AttachmentInfo.fromJson(al)).toList(); + print("attachments $attachments"); + + return attachments; + } + } catch (e) { + print(e); + } + return []; + } } class EmailView extends StatefulWidget { diff --git a/lib/main.dart b/lib/main.dart index 081af44..da90a50 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,14 +1,12 @@ import 'package:crab_ui/contact.dart'; import 'package:flutter/material.dart'; import 'home_page.dart'; -// import 'api_service.dart'; import 'login.dart'; -import 'email.dart'; + void main() { WidgetsFlutterBinding.ensureInitialized(); - // debugPaintSizeEnabled = true; - runApp( HyM()); + runApp(HyM()); } class HyM extends StatelessWidget { @@ -22,14 +20,12 @@ class HyM extends StatelessWidget { theme: ThemeData.light(), title: 'HyM', // home: HomeScreen(), - // home: HomeScreen(), initialRoute: "/", routes: { "/": (context) => SplashScreen(), "/login": (context) => const LoginPage(), "/home": (context) => HomeScreen(), - // "/email": (context) => EmailPage(), "/contacts": (context) => ContactsPage(), }, ); diff --git a/lib/structs.dart b/lib/structs.dart index 9bc3ecf..6eb1c17 100644 --- a/lib/structs.dart +++ b/lib/structs.dart @@ -21,14 +21,14 @@ class GetThreadResponse { factory GetThreadResponse.fromJson(Map json) { var toList = json['to'] as List; - return GetThreadResponse ( + return GetThreadResponse( id: json['id'], messages: List.from(json['messages']), subject: json['subject'], date: DateTime.parse(json['date']), from_name: json['from_name'], from_address: json['from_address'], - to: toList.map((i)=> MailAddress.fromJson(i)).toList(), + to: toList.map((i) => MailAddress.fromJson(i)).toList(), ); } } @@ -51,6 +51,7 @@ class MailAddress { return '${name} <${address}>'; } } + // //old data structure class SerializableMessage { final String name; @@ -100,4 +101,20 @@ class SerializableMessage { in_reply_to: json['in_reply_to'], ); } -} \ No newline at end of file +} + +class AttachmentInfo { + final String name; + final int size; + final String path; + + AttachmentInfo({required this.name, required this.size, required this.path}); + + factory AttachmentInfo.fromJson(Map json) { + return AttachmentInfo( + name: json['name'], + size: json['size'], + path: json['path'], + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 9bc58aa..de4abb5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: shared_preferences: ^2.0.6 encrypt: ^5.0.0 pointycastle: ^3.4.0 + mime: ^1.0.3 english_words: ^4.0.0 provider: ^6.0.0 -- 2.34.1 From a86d71340332bb3d9fcd95da3768bb7c132b7d39 Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 18 Dec 2024 16:37:12 -0500 Subject: [PATCH 5/7] got the login working on my machine p1 --- lib/login.dart | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index 23ba997..c76dc89 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -9,7 +9,7 @@ import 'package:flutter/services.dart' show rootBundle; class AuthService { Future isUserLoggedIn() async { try { - var url = Uri.http('127.0.0.1:3001', 'is_logged_in'); + var url = Uri.http('0.0.0.0:3001', 'is_logged_in'); var response = await http.get(url); print(response.body); if (response.statusCode == 200) { @@ -158,10 +158,11 @@ class _LoginPageState extends State { print(ip); print(port); - String baseUrl = "http://127.0.0.1:5000"; - + String baseUrl = "http://$ip:$port"; + print("baseurl " + baseUrl); + print(baseUrl); try { - final response = await http.get(Uri.parse('$baseUrl/read-config')); + final response = await http.get(Uri.parse('http://localhost:6823/read-config')); print(response.statusCode); print(response.body); if (response.statusCode == 200) { @@ -182,7 +183,7 @@ class _LoginPageState extends State { print("past"); try { - final sending = await http.post(Uri.parse('$baseUrl/update-config'), + final sending = await http.post(Uri.parse('http://localhost:6823/update-config'), headers: {'Content-Type': "application/json"}, body: jsonEncode(updates)); print("sending"); @@ -192,7 +193,7 @@ class _LoginPageState extends State { } try { // String status = await http.post(Uri.parse('')) - var url_log = Uri.http('127.0.0.1:3001', 'log_in'); + var url_log = Uri.http('localhost:6823', 'log_in'); Map filteredData = { 'email': updates['username'], 'password': updates['password'] -- 2.34.1 From d32fadfdc5d24b8bac9be056ab4124718b5050bd Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 18 Dec 2024 23:40:49 -0500 Subject: [PATCH 6/7] login using the new js api, and using a dynamic way of putting ips and ports --- lib/login.dart | 53 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index c76dc89..c1d8a08 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:crab_ui/api_service.dart'; import 'package:crab_ui/home_page.dart'; import 'package:flutter/material.dart'; // import 'package:flutter_widget_from_html/flutter_widget_from_html.dart'; @@ -9,15 +10,29 @@ import 'package:flutter/services.dart' show rootBundle; class AuthService { Future isUserLoggedIn() async { try { - var url = Uri.http('0.0.0.0:3001', 'is_logged_in'); - var response = await http.get(url); + final response = + await http.get(Uri.parse('http://localhost:6823/read-config')); + print(response.statusCode); print(response.body); if (response.statusCode == 200) { - print('all good in the east!'); - String jsonOuter = jsonDecode(response.body); - Map json = jsonDecode(jsonOuter); - // print(json["is_logged_in"]); - return json["is_logged_in"]; + final data = jsonDecode(response.body); + // return data['config']; + try { + var url = Uri.http('${data['ip']}:${data['port']}', 'is_logged_in'); + var response = await http.get(url); + print(response.body); + if (response.statusCode == 200) { + print('all good in the east!'); + String jsonOuter = jsonDecode(response.body); + Map json = jsonDecode(jsonOuter); + // print(json["is_logged_in"]); + ApiService.ip = data['ip']; + ApiService.port = data['port']; + return json["is_logged_in"]; + } + } catch (er) { + print(er); + } } } catch (e) { print(e); @@ -162,7 +177,8 @@ class _LoginPageState extends State { print("baseurl " + baseUrl); print(baseUrl); try { - final response = await http.get(Uri.parse('http://localhost:6823/read-config')); + final response = + await http.get(Uri.parse('http://localhost:6823/read-config')); print(response.statusCode); print(response.body); if (response.statusCode == 200) { @@ -175,15 +191,16 @@ class _LoginPageState extends State { return false; } Map updates = { - "username": email, - "password": password, - "api_addr": ip, - "api_port": port, + // "username": email, + // "password": password, + "ip": ip, + "port": port, }; print("past"); try { - final sending = await http.post(Uri.parse('http://localhost:6823/update-config'), + final sending = await http.post( + Uri.parse('http://localhost:6823/update-config'), headers: {'Content-Type': "application/json"}, body: jsonEncode(updates)); print("sending"); @@ -193,10 +210,12 @@ class _LoginPageState extends State { } try { // String status = await http.post(Uri.parse('')) - var url_log = Uri.http('localhost:6823', 'log_in'); + var url_log = Uri.http('$ip:$port', 'log_in'); Map filteredData = { - 'email': updates['username'], - 'password': updates['password'] + "email": email, + "password": password, + // 'email': updates['username'], + // 'password': updates['password'] }; print(filteredData); var status = await http.post( @@ -209,6 +228,8 @@ class _LoginPageState extends State { if (status.statusCode == 200) { print('response status ${status.body}'); if (status.body == "Successful log in") { + ApiService.ip = ip; + ApiService.port = port; return true; } } -- 2.34.1 From 7423135fead257beca4e17ed137a0684b2b344ea Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 18 Dec 2024 23:41:17 -0500 Subject: [PATCH 7/7] making ips and ports dynamic on the login --- lib/api_service.dart | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 7ba5512..6c71127 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -10,10 +10,13 @@ import 'augment.dart'; import 'dart:html' as html; class ApiService { + static String ip = ""; + static String port = ""; Future> fetchEmailsFromFolder( String folder, int pagenitaion) async { + // print(ip + " " + port); try { - var url = Uri.http('0.0.0.0:3001', 'sorted_threads_by_date', { + var url = Uri.http('$ip:$port', 'sorted_threads_by_date', { 'folder': folder, 'limit': '50', 'offset': pagenitaion.toString(), @@ -49,7 +52,7 @@ class ApiService { List allEmails) async { try { var url = - Uri.http('0.0.0.0:3001', 'get_thread', {'id': threadId.toString()}); + Uri.http('$ip:$port', 'get_thread', {'id': threadId.toString()}); var response = await http.get(url); if (response.statusCode == 200) { @@ -70,7 +73,7 @@ class ApiService { Future> sonicSearch( String list, int limit, int offset, String query) async { try { - var url = Uri.http('0.0.0.0:3001', 'search_emails', { + var url = Uri.http('$ip:$port', 'search_emails', { 'list': list, 'limit': limit.toString(), 'offset': offset.toString(), @@ -101,7 +104,7 @@ class ApiService { try { //attaches email after email from a thread for (var id in IDs) { - var url = Uri.http('0.0.0.0:3001', 'email', {'id': id}); + var url = Uri.http('$ip:$port', 'email', {'id': id}); var response = await http.get(url); @@ -128,7 +131,7 @@ class ApiService { Future> fetchFolders() async { try { - var url = Uri.http('0.0.0.0:3001', 'folders'); + var url = Uri.http('$ip:$port', 'folders'); var response = await http.get(url); return List.from(json.decode(response.body)); } catch (e) { @@ -138,7 +141,7 @@ class ApiService { } Future createFolder(String folderName) async { - var url = Uri.http('0.0.0.0:3001', 'create_folder'); + var url = Uri.http('$ip:$port', 'create_folder'); Map requestBody = {'name': folderName}; @@ -161,7 +164,7 @@ class ApiService { } Future deleteFolder(String folderName) async { - var url = Uri.http('0.0.0.0:3001', 'delete_folder'); + var url = Uri.http('$ip:$port', 'delete_folder'); Map requestBody = {'name': folderName}; @@ -197,7 +200,7 @@ class ApiService { Future> getAttachmentsInfo( String folder, String email_id) async { try { - var url = Uri.http('0.0.0.0:3001', 'get_attachments_info', + var url = Uri.http('127.0.0.1:3001', 'get_attachments_info', {'folder': folder, 'email_id': email_id}); print(url); var response = await http.get(url); -- 2.34.1