login #1
					 1 changed files with 37 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -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,7 +10,15 @@ import 'package:flutter/services.dart' show rootBundle;
 | 
			
		|||
class AuthService {
 | 
			
		||||
  Future<bool> isUserLoggedIn() async {
 | 
			
		||||
    try {
 | 
			
		||||
      var url = Uri.http('0.0.0.0:3001', 'is_logged_in');
 | 
			
		||||
      final response =
 | 
			
		||||
          await http.get(Uri.parse('http://localhost:6823/read-config'));
 | 
			
		||||
      print(response.statusCode);
 | 
			
		||||
      print(response.body);
 | 
			
		||||
      if (response.statusCode == 200) {
 | 
			
		||||
        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) {
 | 
			
		||||
| 
						 | 
				
			
			@ -17,8 +26,14 @@ class AuthService {
 | 
			
		|||
            String jsonOuter = jsonDecode(response.body);
 | 
			
		||||
            Map<String, dynamic> 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<LoginPage> {
 | 
			
		|||
      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<LoginPage> {
 | 
			
		|||
        return false;
 | 
			
		||||
      }
 | 
			
		||||
      Map<String, dynamic> 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<LoginPage> {
 | 
			
		|||
      }
 | 
			
		||||
      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<String, dynamic> 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<LoginPage> {
 | 
			
		|||
        if (status.statusCode == 200) {
 | 
			
		||||
          print('response status ${status.body}');
 | 
			
		||||
          if (status.body == "Successful log in") {
 | 
			
		||||
            ApiService.ip = ip;
 | 
			
		||||
            ApiService.port = port;
 | 
			
		||||
            return true;
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue