From 0b382d72f47a7f6de81b5b3291d16ce21b639785 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 31 Aug 2025 23:00:53 -0400 Subject: [PATCH] nearly done login, only missing the other fields which are on the conf, but those should be made into a more user friendly way, where it should parse the email username, and from it fill out the imap server and what not --- android/app/build.gradle.kts | 8 ++ lib/login.dart | 211 ++++++++++++----------------------- pubspec.yaml | 1 + 3 files changed, 83 insertions(+), 137 deletions(-) diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 86eb85d..fe27d63 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -28,6 +28,14 @@ android { targetSdk = flutter.targetSdkVersion versionCode = flutter.versionCode versionName = flutter.versionName + ndk { + abiFilters.addAll(listOf("arm64-v8a", "x86_64")) + } + sourceSets { + getByName("main") { + jniLibs.srcDirs("src/main/jniLibs") + } + } } buildTypes { diff --git a/lib/login.dart b/lib/login.dart index a03f4d1..1745355 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -12,21 +12,21 @@ import 'package:flutter/services.dart' show rootBundle; class AuthService extends ChangeNotifier { Future isUserLoggedIn() async { - ApiService.ip = '192.168.2.38'; - ApiService.port = '3001'; - print("setted up"); + // ApiService.ip = '127.0.0.1'; + // ApiService.port = '3001'; + // print("setted up"); - return true; + // return true; try { final response = - await http.get(Uri.http('localhost:6823', 'read-config')); - // await http.get(Uri.parse('http://localhost:6823/read-config')); + await http.get(Uri.http('127.0.0.1:6767', 'login_check')); print(response.statusCode); print(response.body); if (response.statusCode == 200) { final data = jsonDecode(response.body); - // return data['config']; + // print(data['state']); + // return true; try { var url = Uri.http('${data['ip']}:${data['port']}', 'is_logged_in'); var response = await http.get(url); @@ -106,18 +106,25 @@ class _SplashScreenState extends State { } Future _checkLoginStatus() async { - // SharedPreferences prefs = await SharedPreferences.getInstance(); - // print(prefs); - // bool isLoggedIn = prefs.getBool('isLoggedIn') ?? false; - await Future.delayed(const Duration(seconds: 1)); - bool isLoggedIn = await _authService.isUserLoggedIn(); - print("is logged in $isLoggedIn"); - if (isLoggedIn) { - context.go("/home"); - // Navigator.pushReplacementNamed(context, '/home'); - } else { + try { + final response = + await http.get(Uri.parse('http://127.0.0.1:6767/login_check')); + print(response.statusCode); + print(response.body); + if (response.statusCode == 200) { + final data = jsonDecode(response.body); + print(data['state']); + if (data['state']) { + context.go("/home"); + } else { + context.go("/login"); + } + } else { + context.go("/login"); + } + } catch (e) { + print("caught in checkloginstatus in login $e"); context.go("/login"); - // Navigator.pushReplacementNamed(context, '/login'); } } @@ -139,14 +146,10 @@ class _LoginPageState extends State { 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 { //this is not done :sob: :skull: - // _configManager.setField("api_addr", ip); return false; } @@ -155,31 +158,16 @@ class _LoginPageState extends State { } Future login() async { - bool result = await _handleLogin(); - if (result) { - Navigator.pushReplacementNamed(context, '/home'); + var result = await _handleLogin(); + if (result[0]) { + ApiService.ip = result[1]; + ApiService.port = result[2]; + context.go('/home'); } } - // Future _checkConfiguration() async { - // return false; - // } - - // 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 - Future _handleLogin() async { + Future _handleLogin() async { if (_formKey.currentState!.validate()) { // Perform login action (e.g., authenticate with backend) String ip = _ipController.text; @@ -193,96 +181,64 @@ class _LoginPageState extends State { print(ip); print(port); - String baseUrl = "http://$ip:$port"; - print("baseurl " + baseUrl); - print(baseUrl); try { - final response = - await http.get(Uri.parse('http://localhost:6823/read-config')); + Map requestBody = { + "user": email, + "password": password, + "ip": ip, + "port": port, + }; + var url = Uri.http("$ip:6767", "login"); + + final response = await http.post(url, + headers: { + 'Content-Type': 'application/json', + }, + body: jsonEncode( + requestBody)); //keep the port but change the ip to the server that will process it? print(response.statusCode); print(response.body); if (response.statusCode == 200) { final data = jsonDecode(response.body); - // return data['config']; + if (data["state"] == true) { + try { + final response = await http + .get(Uri.parse('http://127.0.0.1:6767/login_check')); + print(response.statusCode); + print(response.body); + if (response.statusCode == 200) { + final data = jsonDecode(response.body); + print(data['state']); + if (data['state']) { + context.go("/home"); + } else { + context.go("/login"); + } + } else { + context.go("/login"); + } + } catch (e) { + print("caught in checkloginstatus in login $e"); + context.go("/login"); + } + } + return [true, ip, port]; } + return [false]; } catch (e) { print("caught in catch"); print(e); - return false; + return [false]; } - Map updates = { - // "username": email, - // "password": password, - "ip": ip, - "port": port, - }; - print("past"); - - try { - final sending = await http.post( - Uri.parse('http://localhost:6823/update-config'), - headers: {'Content-Type': "application/json"}, - body: jsonEncode(updates)); - print("sending"); - } catch (e) { - print(e); - return false; - } - try { - // String status = await http.post(Uri.parse('')) - var url_log = Uri.http('$ip:$port', 'log_in'); - Map filteredData = { - "email": email, - "password": password, - // '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") { - ApiService.ip = ip; - ApiService.port = port; - 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"]}"); - // print("api address ${config["api_addr"]}"); - // print(config); - // const url = '' - // Clear the input fields - // _ipController.clear(); - // _portController.clear(); - // _emailController.clear(); - // _passwordController.clear(); } - return false; + return [false]; } @override Widget build(BuildContext context) { - // try { - // _configManager.loadConfig(); - // print(_configManager.getField('base_url')); - // } catch (e) { - // print("broke at build $e"); - // } - // _configManager. + _ipController.text = "127.0.0.1"; + _portController.text = "3001"; + return Center( child: Scaffold( appBar: AppBar( @@ -317,14 +273,6 @@ class _LoginPageState extends State { 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( @@ -403,17 +351,6 @@ 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'), - // ), - // ) ], ), ), diff --git a/pubspec.yaml b/pubspec.yaml index e1ef19b..284635b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,6 +33,7 @@ dependencies: go_router: ^16.0.0 super_editor: ^0.3.0-dev.27 super_editor_markdown: 0.1.8 + dev_dependencies: flutter_test: