364 lines
11 KiB
Dart
364 lines
11 KiB
Dart
import 'dart:convert';
|
|
// import 'package:crab_ui/api_service.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'api_service.dart';
|
|
// import '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;
|
|
// import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:flutter/services.dart' show rootBundle;
|
|
|
|
class AuthService extends ChangeNotifier {
|
|
Future<bool> isUserLoggedIn() async {
|
|
// ApiService.ip = '127.0.0.1';
|
|
// ApiService.port = '3001';
|
|
// print("setted up");
|
|
|
|
// return true;
|
|
try {
|
|
final response =
|
|
await http.get(Uri.http('127.0.0.1:6767', 'login_check'));
|
|
|
|
print(response.statusCode);
|
|
print(response.body);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
// print(data['state']);
|
|
// return true;
|
|
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<String, dynamic> json = jsonDecode(jsonOuter);
|
|
// print(json["is_logged_in"]);
|
|
ApiService.ip = data['ip'];
|
|
print("setting ip " + ApiService.ip);
|
|
ApiService.port = data['port'];
|
|
print("setting port " + data['port']);
|
|
return json["is_logged_in"];
|
|
}
|
|
} catch (er) {
|
|
print(er);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('caughtther');
|
|
print(e);
|
|
}
|
|
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 {
|
|
try {} catch (e) {}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
const LoginPage({super.key});
|
|
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class SplashScreen extends StatefulWidget {
|
|
//entry point
|
|
@override
|
|
_SplashScreenState createState() => _SplashScreenState();
|
|
}
|
|
|
|
class _SplashScreenState extends State<SplashScreen> {
|
|
final AuthService _authService = AuthService();
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_checkLoginStatus();
|
|
});
|
|
}
|
|
|
|
Future<void> _checkLoginStatus() async {
|
|
try {
|
|
final response =
|
|
await http.get(Uri.parse('http://127.0.0.1:6767/login_check'));
|
|
print(response.statusCode);
|
|
print(response.body);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
print(data['state']);
|
|
if (data['state']) {
|
|
context.go("/home");
|
|
} else {
|
|
context.go("/login");
|
|
}
|
|
} else {
|
|
context.go("/login");
|
|
}
|
|
} catch (e) {
|
|
print("caught in checkloginstatus in login $e");
|
|
context.go("/login");
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Scaffold(
|
|
body: Center(child: CircularProgressIndicator()), //nothing happens
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
final AuthService _authService = AuthService();
|
|
// Controllers for capturing user input
|
|
final TextEditingController _ipController = TextEditingController();
|
|
final TextEditingController _portController = TextEditingController();
|
|
final TextEditingController _emailController = TextEditingController();
|
|
final TextEditingController _passwordController = TextEditingController();
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
Future<bool> setIp(String ip) async {
|
|
//this is not done :sob: :skull:
|
|
return false;
|
|
}
|
|
|
|
Future<bool> setPort(String port) async {
|
|
return false;
|
|
}
|
|
|
|
Future<void> login() async {
|
|
var result = await _handleLogin();
|
|
if (result[0]) {
|
|
ApiService.ip = result[1];
|
|
ApiService.port = result[2];
|
|
context.go('/home');
|
|
}
|
|
}
|
|
|
|
// Function to handle login action
|
|
Future<List> _handleLogin() async {
|
|
if (_formKey.currentState!.validate()) {
|
|
// Perform login action (e.g., authenticate with backend)
|
|
String ip = _ipController.text;
|
|
String port = _portController.text;
|
|
String email = _emailController.text;
|
|
String password = _passwordController.text;
|
|
|
|
// For demonstration, just print the values
|
|
print('Email: $email');
|
|
print('Password: $password');
|
|
print(ip);
|
|
print(port);
|
|
|
|
try {
|
|
Map<String, String> requestBody = {
|
|
"user": email,
|
|
"password": password,
|
|
"ip": ip,
|
|
"port": port,
|
|
};
|
|
var url = Uri.http("$ip:6767", "login");
|
|
|
|
final response = await http.post(url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: jsonEncode(
|
|
requestBody)); //keep the port but change the ip to the server that will process it?
|
|
print(response.statusCode);
|
|
print(response.body);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
if (data["state"] == true) {
|
|
try {
|
|
final response = await http
|
|
.get(Uri.parse('http://127.0.0.1:6767/login_check'));
|
|
print(response.statusCode);
|
|
print(response.body);
|
|
if (response.statusCode == 200) {
|
|
final data = jsonDecode(response.body);
|
|
print(data['state']);
|
|
if (data['state']) {
|
|
context.go("/home");
|
|
} else {
|
|
context.go("/login");
|
|
}
|
|
} else {
|
|
context.go("/login");
|
|
}
|
|
} catch (e) {
|
|
print("caught in checkloginstatus in login $e");
|
|
context.go("/login");
|
|
}
|
|
}
|
|
return [true, ip, port];
|
|
}
|
|
return [false];
|
|
} catch (e) {
|
|
print("caught in catch");
|
|
print(e);
|
|
return [false];
|
|
}
|
|
}
|
|
return [false];
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
_ipController.text = "127.0.0.1";
|
|
_portController.text = "3001";
|
|
|
|
return Center(
|
|
child: Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Login Page'),
|
|
),
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Form(
|
|
key: _formKey,
|
|
child: Align(
|
|
alignment: Alignment.center,
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Sign in to your email'),
|
|
SizedBox(
|
|
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';
|
|
}
|
|
},
|
|
),
|
|
),
|
|
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
|
|
Container(
|
|
width: 200,
|
|
child: TextFormField(
|
|
controller: _emailController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Email',
|
|
hintText: 'Enter your email',
|
|
helperMaxLines: 1,
|
|
),
|
|
validator: (value) {
|
|
// Simple email validation
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter your email';
|
|
}
|
|
if (!RegExp(r'^\S+@\S+\.\S+$').hasMatch(value)) {
|
|
// print(value);
|
|
return 'Please enter a valid email address';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 16.0),
|
|
// Password Field
|
|
Container(
|
|
width: 200,
|
|
child: TextFormField(
|
|
controller: _passwordController,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Password',
|
|
hintText: 'Enter your password',
|
|
),
|
|
obscureText: true, // Hide the password text
|
|
autofillHints: null,
|
|
validator: (value) {
|
|
// Simple password validation
|
|
if (value == null || value.isEmpty) {
|
|
return 'Please enter your password';
|
|
}
|
|
if (value.length < 6) {
|
|
return 'Password must be at least 6 characters long';
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 32.0),
|
|
// Login Button
|
|
SizedBox(
|
|
width: 200,
|
|
child: ElevatedButton(
|
|
onPressed: login,
|
|
child: const Text('Login'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|