login #1
					 1 changed files with 153 additions and 18 deletions
				
			
		
							
								
								
									
										171
									
								
								lib/login.dart
									
										
									
									
									
								
							
							
						
						
									
										171
									
								
								lib/login.dart
									
										
									
									
									
								
							| 
						 | 
					@ -1,9 +1,10 @@
 | 
				
			||||||
import 'dart:convert';
 | 
					import 'dart:convert';
 | 
				
			||||||
 | 
					import 'package:crab_ui/configManager.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					// import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
 | 
				
			||||||
import 'package:http/http.dart' as http;
 | 
					import 'package:http/http.dart' as http;
 | 
				
			||||||
import 'package:shared_preferences/shared_preferences.dart';
 | 
					// import 'package:shared_preferences/shared_preferences.dart';
 | 
				
			||||||
import 'dart:io';
 | 
					import 'package:flutter/services.dart' show rootBundle;
 | 
				
			||||||
import 'package:path_provider/path_provider.dart';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AuthService {
 | 
					class AuthService {
 | 
				
			||||||
  Future<bool> isUserLoggedIn() async {
 | 
					  Future<bool> isUserLoggedIn() async {
 | 
				
			||||||
| 
						 | 
					@ -24,6 +25,29 @@ class AuthService {
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<String> readConfFile() async {
 | 
				
			||||||
 | 
					    final content = await rootBundle.loadString('/crabmail.conf');
 | 
				
			||||||
 | 
					    return content;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Map<String, String> parseConfFile(String content) {
 | 
				
			||||||
 | 
					    final Map<String, String> config = {};
 | 
				
			||||||
 | 
					    final lines = content.split('\n');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (var line in lines) {
 | 
				
			||||||
 | 
					      line = line.trim();
 | 
				
			||||||
 | 
					      if (line.isEmpty || line.startsWith('#')) continue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      final parts = line.split('=');
 | 
				
			||||||
 | 
					      if (parts.length == 2) {
 | 
				
			||||||
 | 
					        final key = parts[0].trim();
 | 
				
			||||||
 | 
					        final value = parts[1].trim();
 | 
				
			||||||
 | 
					        config[key] = value;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return config;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Future<bool> loginUser(String email, String password) async {
 | 
					  Future<bool> loginUser(String email, String password) async {
 | 
				
			||||||
    try {} catch (e) {}
 | 
					    try {} catch (e) {}
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
| 
						 | 
					@ -57,7 +81,6 @@ class _SplashScreenState extends State<SplashScreen> {
 | 
				
			||||||
    bool isLoggedIn = await _authService.isUserLoggedIn();
 | 
					    bool isLoggedIn = await _authService.isUserLoggedIn();
 | 
				
			||||||
    print("is loogeed in $isLoggedIn");
 | 
					    print("is loogeed in $isLoggedIn");
 | 
				
			||||||
    if (isLoggedIn) {
 | 
					    if (isLoggedIn) {
 | 
				
			||||||
      print('here');
 | 
					 | 
				
			||||||
      Navigator.pushReplacementNamed(context, '/home');
 | 
					      Navigator.pushReplacementNamed(context, '/home');
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      Navigator.pushReplacementNamed(context, '/login');
 | 
					      Navigator.pushReplacementNamed(context, '/login');
 | 
				
			||||||
| 
						 | 
					@ -77,12 +100,29 @@ class _SplashScreenState extends State<SplashScreen> {
 | 
				
			||||||
class _LoginPageState extends State<LoginPage> {
 | 
					class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
  final AuthService _authService = AuthService();
 | 
					  final AuthService _authService = AuthService();
 | 
				
			||||||
  // Controllers for capturing user input
 | 
					  // Controllers for capturing user input
 | 
				
			||||||
 | 
					  final TextEditingController _ipController = TextEditingController();
 | 
				
			||||||
 | 
					  final TextEditingController _portController = TextEditingController();
 | 
				
			||||||
  final TextEditingController _emailController = TextEditingController();
 | 
					  final TextEditingController _emailController = TextEditingController();
 | 
				
			||||||
  final TextEditingController _passwordController = TextEditingController();
 | 
					  final TextEditingController _passwordController = TextEditingController();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // final ConfigManager _configManager =
 | 
				
			||||||
 | 
					  //     ConfigManager("${Directory.current.parent}../crabmail2.conf");
 | 
				
			||||||
  // Key to identify the form
 | 
					  // Key to identify the form
 | 
				
			||||||
  final _formKey = GlobalKey<FormState>();
 | 
					  final _formKey = GlobalKey<FormState>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<bool> setIp(String ip) async {
 | 
				
			||||||
 | 
					    // _configManager.setField("api_addr", ip);
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  Future<bool> setPort(String port) async {
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Future<bool> _checkConfiguration() async {
 | 
				
			||||||
 | 
					  //   return false;
 | 
				
			||||||
 | 
					  // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // void checkLogin() async {
 | 
					  // void checkLogin() async {
 | 
				
			||||||
  //   try {
 | 
					  //   try {
 | 
				
			||||||
  //     var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
 | 
					  //     var url = Uri.http('127.0.0.1:3001', 'is_logged_in');
 | 
				
			||||||
| 
						 | 
					@ -98,24 +138,74 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
  // }
 | 
					  // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Function to handle login action
 | 
					  // Function to handle login action
 | 
				
			||||||
  void _handleLogin() {
 | 
					  Future<void> _handleLogin() async {
 | 
				
			||||||
    if (_formKey.currentState!.validate()) {
 | 
					    if (_formKey.currentState!.validate()) {
 | 
				
			||||||
      // Perform login action (e.g., authenticate with backend)
 | 
					      // Perform login action (e.g., authenticate with backend)
 | 
				
			||||||
 | 
					      String ip = _ipController.text;
 | 
				
			||||||
 | 
					      String port = _portController.text;
 | 
				
			||||||
      String email = _emailController.text;
 | 
					      String email = _emailController.text;
 | 
				
			||||||
      String password = _passwordController.text;
 | 
					      String password = _passwordController.text;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // For demonstration, just print the values
 | 
					      // For demonstration, just print the values
 | 
				
			||||||
      print('Email: $email');
 | 
					      print('Email: $email');
 | 
				
			||||||
      print('Password: $password');
 | 
					      print('Password: $password');
 | 
				
			||||||
 | 
					      print(ip);
 | 
				
			||||||
 | 
					      print(port);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      final String baseUrl = "http://127.0.0.1:5000";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        final response = await http.get(Uri.parse('${baseUrl}/read-config'));
 | 
				
			||||||
 | 
					        print(response.statusCode);
 | 
				
			||||||
 | 
					        print(response.body);
 | 
				
			||||||
 | 
					        if (response.statusCode == 200) {
 | 
				
			||||||
 | 
					          final data = jsonDecode(response.body);
 | 
				
			||||||
 | 
					          // return data['config'];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } catch (e) {
 | 
				
			||||||
 | 
					        print("caught in catch");
 | 
				
			||||||
 | 
					        print(e);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      Map<String, dynamic> updates = {
 | 
				
			||||||
 | 
					        "username": email,
 | 
				
			||||||
 | 
					        "password": password,
 | 
				
			||||||
 | 
					        "api_addr": ip,
 | 
				
			||||||
 | 
					        "api_port": port,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					      print("past");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        final sending = await http.post(Uri.parse('$baseUrl/update-config'),
 | 
				
			||||||
 | 
					            headers: {'Content-Type': "application/json"},
 | 
				
			||||||
 | 
					            body: jsonEncode(updates));
 | 
				
			||||||
 | 
					        print("sending");
 | 
				
			||||||
 | 
					      } catch (e) {
 | 
				
			||||||
 | 
					        print(e);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // final content = await _authService.readConfFile();
 | 
				
			||||||
 | 
					      // final config = await _authService.parseConfFile(content);
 | 
				
			||||||
 | 
					      // print("BASE URL ${config["base_url"]}");
 | 
				
			||||||
 | 
					      // print("api address ${config["api_addr"]}");
 | 
				
			||||||
 | 
					      // print(config);
 | 
				
			||||||
 | 
					      // const url = ''
 | 
				
			||||||
      // Clear the input fields
 | 
					      // Clear the input fields
 | 
				
			||||||
      _emailController.clear();
 | 
					      // _ipController.clear();
 | 
				
			||||||
      _passwordController.clear();
 | 
					      // _portController.clear();
 | 
				
			||||||
 | 
					      // _emailController.clear();
 | 
				
			||||||
 | 
					      // _passwordController.clear();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
  Widget build(BuildContext context) {
 | 
					  Widget build(BuildContext context) {
 | 
				
			||||||
 | 
					    // try {
 | 
				
			||||||
 | 
					    //   _configManager.loadConfig();
 | 
				
			||||||
 | 
					    //   print(_configManager.getField('base_url'));
 | 
				
			||||||
 | 
					    // } catch (e) {
 | 
				
			||||||
 | 
					    //   print("broke at build $e");
 | 
				
			||||||
 | 
					    // }
 | 
				
			||||||
 | 
					    // _configManager.
 | 
				
			||||||
    return Center(
 | 
					    return Center(
 | 
				
			||||||
      child: Scaffold(
 | 
					      child: Scaffold(
 | 
				
			||||||
        appBar: AppBar(
 | 
					        appBar: AppBar(
 | 
				
			||||||
| 
						 | 
					@ -136,6 +226,50 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
                    SizedBox(
 | 
					                    SizedBox(
 | 
				
			||||||
                      height: 5,
 | 
					                      height: 5,
 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
 | 
					                    Container(
 | 
				
			||||||
 | 
					                      width: 200,
 | 
				
			||||||
 | 
					                      child: TextFormField(
 | 
				
			||||||
 | 
					                        controller: _ipController,
 | 
				
			||||||
 | 
					                        decoration: const InputDecoration(
 | 
				
			||||||
 | 
					                          labelText: "IP",
 | 
				
			||||||
 | 
					                          hintText: 'Enter your IP for the backend',
 | 
				
			||||||
 | 
					                          helperMaxLines: 1,
 | 
				
			||||||
 | 
					                        ),
 | 
				
			||||||
 | 
					                        validator: (value) {
 | 
				
			||||||
 | 
					                          if (value == null || value.isEmpty) {
 | 
				
			||||||
 | 
					                            return 'Please enter your ip';
 | 
				
			||||||
 | 
					                          }
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        // onSaved: (value) async {
 | 
				
			||||||
 | 
					                        //   final content = await _authService.readConfFile();
 | 
				
			||||||
 | 
					                        //   final config =
 | 
				
			||||||
 | 
					                        //       await _authService.parseConfFile(content);
 | 
				
			||||||
 | 
					                        //   print("BASE URL ${config["base_url"]}");
 | 
				
			||||||
 | 
					                        //   print("api address ${config["api_addr"]}");
 | 
				
			||||||
 | 
					                        //   //TODO: call a function to set the field ip in conf
 | 
				
			||||||
 | 
					                        // },
 | 
				
			||||||
 | 
					                      ),
 | 
				
			||||||
 | 
					                    ),
 | 
				
			||||||
 | 
					                    Container(
 | 
				
			||||||
 | 
					                      width: 200,
 | 
				
			||||||
 | 
					                      child: TextFormField(
 | 
				
			||||||
 | 
					                        controller: _portController,
 | 
				
			||||||
 | 
					                        decoration: const InputDecoration(
 | 
				
			||||||
 | 
					                          labelText: "Port",
 | 
				
			||||||
 | 
					                          hintText: 'Enter the port',
 | 
				
			||||||
 | 
					                          helperMaxLines: 1,
 | 
				
			||||||
 | 
					                        ),
 | 
				
			||||||
 | 
					                        validator: (value) {
 | 
				
			||||||
 | 
					                          if (value == null || value.isEmpty) {
 | 
				
			||||||
 | 
					                            return 'Please enter your port';
 | 
				
			||||||
 | 
					                          }
 | 
				
			||||||
 | 
					                          return null;
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        onSaved: (value) {
 | 
				
			||||||
 | 
					                          //TODO: call a function to set the field port in conf
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                      ),
 | 
				
			||||||
 | 
					                    ),
 | 
				
			||||||
                    // Email Field
 | 
					                    // Email Field
 | 
				
			||||||
                    Container(
 | 
					                    Container(
 | 
				
			||||||
                      width: 200,
 | 
					                      width: 200,
 | 
				
			||||||
| 
						 | 
					@ -170,6 +304,7 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
                          hintText: 'Enter your password',
 | 
					                          hintText: 'Enter your password',
 | 
				
			||||||
                        ),
 | 
					                        ),
 | 
				
			||||||
                        obscureText: true, // Hide the password text
 | 
					                        obscureText: true, // Hide the password text
 | 
				
			||||||
 | 
					                        autofillHints: null,
 | 
				
			||||||
                        validator: (value) {
 | 
					                        validator: (value) {
 | 
				
			||||||
                          // Simple password validation
 | 
					                          // Simple password validation
 | 
				
			||||||
                          if (value == null || value.isEmpty) {
 | 
					                          if (value == null || value.isEmpty) {
 | 
				
			||||||
| 
						 | 
					@ -191,17 +326,17 @@ class _LoginPageState extends State<LoginPage> {
 | 
				
			||||||
                        child: const Text('Login'),
 | 
					                        child: const Text('Login'),
 | 
				
			||||||
                      ),
 | 
					                      ),
 | 
				
			||||||
                    ),
 | 
					                    ),
 | 
				
			||||||
                    SizedBox(
 | 
					                    // SizedBox(
 | 
				
			||||||
                      width: 200,
 | 
					                    //   width: 200,
 | 
				
			||||||
                      child: ElevatedButton(
 | 
					                    //   child: ElevatedButton(
 | 
				
			||||||
                        // onPressed: checkLogin,
 | 
					                    //     // onPressed: checkLogin,
 | 
				
			||||||
                        onPressed: () async {
 | 
					                    //     onPressed: () async {
 | 
				
			||||||
                          await _authService.isUserLoggedIn();
 | 
					                    //       await _authService.isUserLoggedIn();
 | 
				
			||||||
                          // print(result);
 | 
					                    //       // print(result);
 | 
				
			||||||
                        },
 | 
					                    //     },
 | 
				
			||||||
                        child: const Text('checker'),
 | 
					                    //     child: const Text('checker'),
 | 
				
			||||||
                      ),
 | 
					                    //   ),
 | 
				
			||||||
                    )
 | 
					                    // )
 | 
				
			||||||
                  ],
 | 
					                  ],
 | 
				
			||||||
                ),
 | 
					                ),
 | 
				
			||||||
              ),
 | 
					              ),
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue