2024-02-11 01:08:51 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-02-11 02:20:48 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2024-02-11 01:08:51 +01:00
|
|
|
import 'package:simplecloudnotifier/api/api_client.dart';
|
2024-02-11 02:20:48 +01:00
|
|
|
import 'package:simplecloudnotifier/models/key_token_auth.dart';
|
2024-05-26 00:20:25 +02:00
|
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
2024-02-11 02:20:48 +01:00
|
|
|
import 'package:simplecloudnotifier/state/user_account.dart';
|
2024-02-11 01:08:51 +01:00
|
|
|
|
|
|
|
class AccountLoginPage extends StatefulWidget {
|
2024-02-11 02:20:48 +01:00
|
|
|
final void Function()? onLogin;
|
|
|
|
|
|
|
|
const AccountLoginPage({super.key, this.onLogin});
|
2024-02-11 01:08:51 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<AccountLoginPage> createState() => _AccountLoginPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AccountLoginPageState extends State<AccountLoginPage> {
|
2024-03-02 00:26:47 +01:00
|
|
|
final TextEditingController _ctrlUserID = TextEditingController();
|
|
|
|
final TextEditingController _ctrlToken = TextEditingController();
|
2024-02-11 01:08:51 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_ctrlUserID.dispose();
|
|
|
|
_ctrlToken.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-02-18 17:36:58 +01:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
2024-02-11 01:08:51 +01:00
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
2024-02-18 17:36:58 +01:00
|
|
|
FractionallySizedBox(
|
|
|
|
widthFactor: 1.0,
|
2024-02-11 01:08:51 +01:00
|
|
|
child: TextField(
|
|
|
|
controller: _ctrlUserID,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'UserID',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2024-02-18 17:36:58 +01:00
|
|
|
FractionallySizedBox(
|
|
|
|
widthFactor: 1.0,
|
2024-02-11 01:08:51 +01:00
|
|
|
child: TextField(
|
|
|
|
controller: _ctrlToken,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'Token',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2024-06-01 03:06:02 +02:00
|
|
|
FilledButton(
|
|
|
|
style: FilledButton.styleFrom(textStyle: const TextStyle(fontSize: 20)),
|
2024-02-11 01:08:51 +01:00
|
|
|
onPressed: _login,
|
|
|
|
child: const Text('Login'),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _login() async {
|
|
|
|
final msgr = ScaffoldMessenger.of(context);
|
2024-06-01 03:06:02 +02:00
|
|
|
final acc = Provider.of<UserAccount>(context, listen: false);
|
2024-02-11 02:20:48 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
final uid = _ctrlUserID.text;
|
|
|
|
final tok = _ctrlToken.text;
|
2024-02-11 01:08:51 +01:00
|
|
|
|
2024-03-02 00:26:47 +01:00
|
|
|
final verified = await APIClient.verifyToken(uid, tok); //TODO verify that this is an perm=ADMIN key
|
2024-02-11 02:20:48 +01:00
|
|
|
if (verified) {
|
|
|
|
msgr.showSnackBar(
|
|
|
|
const SnackBar(
|
2024-05-26 00:20:25 +02:00
|
|
|
content: Text('Data ok'), //TODO use toast?
|
2024-02-11 02:20:48 +01:00
|
|
|
),
|
|
|
|
);
|
2024-06-01 03:06:02 +02:00
|
|
|
acc.setToken(KeyTokenAuth(userId: uid, tokenAdmin: tok, tokenSend: '')); //TOTO send token
|
|
|
|
await acc.save();
|
2024-02-11 02:20:48 +01:00
|
|
|
widget.onLogin?.call();
|
|
|
|
} else {
|
|
|
|
msgr.showSnackBar(
|
|
|
|
const SnackBar(
|
2024-05-26 00:20:25 +02:00
|
|
|
content: Text('Failed to verify token'), //TODO use toast?
|
2024-02-11 02:20:48 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-26 00:20:25 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
ApplicationLog.error('Failed to verify token: ' + exc.toString(), trace: trace);
|
2024-02-11 01:08:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|