Browse Source

added the login, it works but it needs a cleaning

juan 1 week ago
parent
commit
a0652ba5c5
2 changed files with 44 additions and 7 deletions
  1. 41 6
      lib/login.dart
  2. 3 1
      pubspec.yaml

+ 41 - 6
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<LoginPage> {
     return false;
   }
 
+  Future<void> login() async {
+    bool result = await _handleLogin();
+    if (result) {
+      Navigator.pushReplacementNamed(context, '/home');
+    }
+  }
   // Future<bool> _checkConfiguration() async {
   //   return false;
   // }
@@ -138,7 +144,7 @@ class _LoginPageState extends State<LoginPage> {
   // }
 
   // Function to handle login action
-  Future<void> _handleLogin() async {
+  Future<bool> _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<LoginPage> {
       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<LoginPage> {
       } catch (e) {
         print("caught in catch");
         print(e);
+        return false;
       }
       Map<String, dynamic> updates = {
         "username": email,
@@ -181,8 +188,35 @@ class _LoginPageState extends State<LoginPage> {
         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<String, dynamic> 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<LoginPage> {
       // _emailController.clear();
       // _passwordController.clear();
     }
+    return false;
   }
 
   @override
@@ -322,7 +357,7 @@ class _LoginPageState extends State<LoginPage> {
                     SizedBox(
                       width: 200,
                       child: ElevatedButton(
-                        onPressed: _handleLogin,
+                        onPressed: login,
                         child: const Text('Login'),
                       ),
                     ),

+ 3 - 1
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