From d32fadfdc5d24b8bac9be056ab4124718b5050bd Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 18 Dec 2024 23:40:49 -0500 Subject: [PATCH] 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; } }