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