2024-06-01 14:00:16 +02:00
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
2024-02-11 01:08:51 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-01 14:00:16 +02:00
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.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-06-01 14:00:16 +02:00
|
|
|
import 'package:simplecloudnotifier/components/layout/scaffold.dart';
|
2024-05-26 00:20:25 +02:00
|
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
2024-06-01 14:00:16 +02:00
|
|
|
import 'package:simplecloudnotifier/state/globals.dart';
|
2024-06-02 17:09:57 +02:00
|
|
|
import 'package:simplecloudnotifier/state/app_auth.dart';
|
|
|
|
import 'package:simplecloudnotifier/state/token_source.dart';
|
2024-06-01 14:00:16 +02:00
|
|
|
import 'package:simplecloudnotifier/utils/toaster.dart';
|
2024-06-08 12:55:58 +02:00
|
|
|
import 'package:simplecloudnotifier/utils/ui.dart';
|
2024-02-11 01:08:51 +01:00
|
|
|
|
|
|
|
class AccountLoginPage extends StatefulWidget {
|
2024-06-01 14:00:16 +02:00
|
|
|
const AccountLoginPage({super.key});
|
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();
|
2024-06-01 14:00:16 +02:00
|
|
|
final TextEditingController _ctrlTokenAdmin = TextEditingController();
|
|
|
|
final TextEditingController _ctrlTokenSend = TextEditingController();
|
|
|
|
|
|
|
|
bool loading = false;
|
2024-02-11 01:08:51 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_ctrlUserID.dispose();
|
2024-06-01 14:00:16 +02:00
|
|
|
_ctrlTokenAdmin.dispose();
|
|
|
|
_ctrlTokenSend.dispose();
|
2024-02-11 01:08:51 +01:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-01 14:00:16 +02:00
|
|
|
return SCNScaffold(
|
|
|
|
title: 'Login',
|
|
|
|
showSearch: false,
|
|
|
|
child: SingleChildScrollView(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(24, 32, 24, 16),
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
children: [
|
|
|
|
if (!loading)
|
|
|
|
Center(
|
|
|
|
child: Container(
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
borderRadius: BorderRadius.circular(100),
|
|
|
|
),
|
|
|
|
child: Center(child: FaIcon(FontAwesomeIcons.solidRightToBracket, size: 96, color: Theme.of(context).colorScheme.onSecondary)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (loading)
|
|
|
|
Center(
|
|
|
|
child: Container(
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
color: Theme.of(context).colorScheme.secondary,
|
|
|
|
borderRadius: BorderRadius.circular(100),
|
|
|
|
),
|
|
|
|
child: Center(child: CircularProgressIndicator(color: Theme.of(context).colorScheme.onSecondary)),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 32),
|
|
|
|
FractionallySizedBox(
|
|
|
|
widthFactor: 1.0,
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrlUserID,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'UserID',
|
|
|
|
),
|
|
|
|
),
|
2024-02-11 01:08:51 +01:00
|
|
|
),
|
2024-06-01 14:00:16 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
FractionallySizedBox(
|
|
|
|
widthFactor: 1.0,
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrlTokenAdmin,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'Admin Token',
|
|
|
|
),
|
|
|
|
),
|
2024-02-11 01:08:51 +01:00
|
|
|
),
|
2024-06-01 14:00:16 +02:00
|
|
|
const SizedBox(height: 16),
|
|
|
|
FractionallySizedBox(
|
|
|
|
widthFactor: 1.0,
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrlTokenSend,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
labelText: 'Send Token (optional)',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
2024-06-08 12:55:58 +02:00
|
|
|
UI.button(
|
|
|
|
text: 'Login',
|
|
|
|
big: true,
|
2024-06-01 14:00:16 +02:00
|
|
|
onPressed: _login,
|
|
|
|
),
|
|
|
|
],
|
2024-02-11 01:08:51 +01:00
|
|
|
),
|
2024-06-01 14:00:16 +02:00
|
|
|
),
|
2024-02-11 01:08:51 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _login() async {
|
2024-06-02 17:09:57 +02:00
|
|
|
final acc = Provider.of<AppAuth>(context, listen: false);
|
2024-02-11 02:20:48 +01:00
|
|
|
|
|
|
|
try {
|
2024-06-01 14:00:16 +02:00
|
|
|
setState(() => loading = true);
|
|
|
|
|
2024-02-11 02:20:48 +01:00
|
|
|
final uid = _ctrlUserID.text;
|
2024-06-01 14:00:16 +02:00
|
|
|
final atokv = _ctrlTokenAdmin.text;
|
|
|
|
final stokv = _ctrlTokenSend.text;
|
2024-02-11 01:08:51 +01:00
|
|
|
|
2024-06-01 14:00:16 +02:00
|
|
|
final fcmToken = await FirebaseMessaging.instance.getToken();
|
|
|
|
|
|
|
|
if (fcmToken == null) {
|
|
|
|
Toaster.warn("Missing Token", 'No FCM Token found, please allow notifications, ensure you have a network connection and restart the app');
|
|
|
|
return;
|
2024-02-11 02:20:48 +01:00
|
|
|
}
|
2024-06-01 14:00:16 +02:00
|
|
|
|
|
|
|
final toka = await APIClient.getKeyTokenByToken(uid, atokv);
|
|
|
|
|
|
|
|
if (!toka.allChannels || toka.permissions != 'A') {
|
|
|
|
Toaster.error("Error", 'Admin token does not have required permissions');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final toks = await APIClient.getKeyTokenByToken(uid, stokv);
|
|
|
|
|
|
|
|
if (!toks.allChannels || toks.permissions != 'CS') {
|
|
|
|
Toaster.error("Error", 'Send token does not have required permissions');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
final user = await APIClient.getUser(DirectTokenSource(uid, atokv), uid);
|
2024-06-01 14:00:16 +02:00
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
final client = await APIClient.addClient(DirectTokenSource(uid, atokv), fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
|
2024-06-01 14:00:16 +02:00
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
acc.set(user, client, atokv, stokv);
|
2024-06-01 14:00:16 +02:00
|
|
|
await acc.save();
|
|
|
|
|
|
|
|
Toaster.success("Login", "Successfully logged in");
|
2024-06-01 15:37:59 +02:00
|
|
|
Navigator.popUntil(context, (route) => route.isFirst);
|
2024-05-26 00:20:25 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
ApplicationLog.error('Failed to verify token: ' + exc.toString(), trace: trace);
|
2024-06-01 14:00:16 +02:00
|
|
|
Toaster.error("Error", 'Failed to verify token');
|
|
|
|
} finally {
|
|
|
|
setState(() => loading = false);
|
2024-02-11 01:08:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|