Skip to content

Commit b2a502a

Browse files
committed
#12 feat: kerio auto login
1 parent e0c2b8d commit b2a502a

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

lib/views/kerio_login.dart

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:http/http.dart' as http;
3+
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
4+
5+
class KerioLoginPage extends StatefulWidget {
6+
@override
7+
_KerioLoginPageState createState() => _KerioLoginPageState();
8+
}
9+
10+
class _KerioLoginPageState extends State<KerioLoginPage> {
11+
final TextEditingController _ipController = TextEditingController();
12+
final TextEditingController _usernameController = TextEditingController();
13+
final TextEditingController _passwordController = TextEditingController();
14+
final _storage = FlutterSecureStorage();
15+
16+
@override
17+
void initState() {
18+
super.initState();
19+
_attemptAutoLogin();
20+
}
21+
22+
Future<void> _attemptAutoLogin() async {
23+
final ip = await _storage.read(key: 'kerio_ip');
24+
final username = await _storage.read(key: 'kerio_username');
25+
final password = await _storage.read(key: 'kerio_password');
26+
27+
if (ip != null && username != null && password != null) {
28+
_ipController.text = ip;
29+
_usernameController.text = username;
30+
_passwordController.text = password;
31+
_login();
32+
}
33+
}
34+
35+
void _login() async {
36+
final ip = _ipController.text;
37+
final username = _usernameController.text;
38+
final password = _passwordController.text;
39+
40+
if (ip.isEmpty || username.isEmpty || password.isEmpty) {
41+
_showMessage('Please fill in all fields');
42+
return;
43+
}
44+
45+
// Save credentials for auto-login
46+
await _storage.write(key: 'kerio_ip', value: ip);
47+
await _storage.write(key: 'kerio_username', value: username);
48+
await _storage.write(key: 'kerio_password', value: password);
49+
50+
final url = 'http://$ip/internal/dologin.php';
51+
final response = await http.post(
52+
Uri.parse(url),
53+
body: {
54+
'kerio_username': username,
55+
'kerio_password': password,
56+
},
57+
);
58+
59+
if (response.statusCode == 200) {
60+
_showMessage('Login successful');
61+
} else {
62+
_showMessage('Login failed');
63+
}
64+
}
65+
66+
void _showMessage(String message) {
67+
showDialog(
68+
context: context,
69+
builder: (context) => AlertDialog(
70+
content: Text(message),
71+
actions: [
72+
TextButton(
73+
onPressed: () => Navigator.of(context).pop(),
74+
child: Text('OK'),
75+
),
76+
],
77+
),
78+
);
79+
}
80+
81+
@override
82+
Widget build(BuildContext context) {
83+
return Scaffold(
84+
appBar: AppBar(
85+
title: Text('Kerio Login'),
86+
),
87+
body: Padding(
88+
padding: const EdgeInsets.all(16.0),
89+
child: Column(
90+
crossAxisAlignment: CrossAxisAlignment.stretch,
91+
children: [
92+
TextField(
93+
controller: _ipController,
94+
decoration: InputDecoration(labelText: 'Kerio Login Page IP'),
95+
keyboardType: TextInputType.url,
96+
),
97+
TextField(
98+
controller: _usernameController,
99+
decoration: InputDecoration(labelText: 'Username'),
100+
),
101+
TextField(
102+
controller: _passwordController,
103+
decoration: InputDecoration(labelText: 'Password'),
104+
obscureText: true,
105+
),
106+
SizedBox(height: 20),
107+
ElevatedButton(
108+
onPressed: _login,
109+
child: Text('Login'),
110+
),
111+
],
112+
),
113+
),
114+
);
115+
}
116+
}

0 commit comments

Comments
 (0)