import 'dart:convert'; import 'package:faucethub/Backend/DebugHelper.dart'; import 'package:faucethub/Backend/brains.dart'; import 'package:flutter/cupertino.dart'; import 'package:http/http.dart' as http; import 'package:shared_preferences/shared_preferences.dart'; final GlobalKey navigatorKey = new GlobalKey(); class LoginManager { static Future AutoLogin() async{ final prefs = await SharedPreferences.getInstance(); String username = ""; String password= ""; try{ username = prefs.getString("username")!; password = prefs.getString("password")!; }catch(e){ } if(username.length > 3 && password.length >5){ return await Login(username, password); } return 1; } static Future Register (String username, String password) async{ Debug.Log("Starting registration"); final prefs = await SharedPreferences.getInstance(); var loginResponse = null; try { loginResponse = (await http.post( Uri.parse('${Brain.API_ENDPOINT}register.php'), body: { "username": username, "password": password })); Debug.LogError(loginResponse.body.toString()); if (loginResponse.body.toLowerCase() == "0") { prefs.setString("username", username); prefs.setString("password", password); return 0; }else{ return 5; } } catch (e) { Debug.LogError("Error while login $e"); } return 1; } static Future Login (String username, String password) async{ Debug.Log("Logging in"); final prefs = await SharedPreferences.getInstance(); var loginResponse = null; try { loginResponse = (await http.post( Uri.parse('${Brain.API_ENDPOINT}login.php'), body: { "username": username, "password": password })); Debug.LogError(loginResponse.body.toString()); try{ Brain.UserJson = jsonDecode(loginResponse.body.toString()); Brain.InitUserData(); Debug.LogResponse(Brain.UserJson); prefs.setString("username", username); prefs.setString("password", password); return 0; }catch(e){ return 5; } } catch (e) { Debug.LogError("Error while login $e"); } return 1; } }