2023-03-17 21:07:05 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2023-03-25 14:27:47 +01:00
|
|
|
import 'package:outbag_app/backend/crypto.dart';
|
2023-03-17 21:07:05 +01:00
|
|
|
import 'package:outbag_app/backend/request.dart';
|
2023-03-18 20:27:20 +01:00
|
|
|
import 'package:outbag_app/backend/user.dart';
|
2023-03-23 14:48:53 +01:00
|
|
|
import 'package:outbag_app/tools/fetch_wrapper.dart';
|
|
|
|
import 'package:outbag_app/tools/snackbar.dart';
|
2023-03-29 15:14:27 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-03-17 21:07:05 +01:00
|
|
|
import '../backend/resolve_url.dart';
|
|
|
|
|
|
|
|
enum Mode {
|
|
|
|
signin,
|
|
|
|
signup,
|
|
|
|
signupOTA,
|
|
|
|
}
|
|
|
|
|
|
|
|
class AuthPage extends StatefulWidget {
|
|
|
|
final Mode mode;
|
2023-03-29 18:02:00 +02:00
|
|
|
Function refresh;
|
|
|
|
AuthPage({super.key, required this.mode, required this.refresh});
|
2023-03-17 21:07:05 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _AuthPageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _AuthPageState extends State<AuthPage> {
|
|
|
|
final TextEditingController _ctrServer = TextEditingController();
|
|
|
|
final TextEditingController _ctrUsername = TextEditingController();
|
|
|
|
final TextEditingController _ctrPassword = TextEditingController();
|
|
|
|
final TextEditingController _ctrPasswordRpt = TextEditingController();
|
|
|
|
final TextEditingController _ctrOTA = TextEditingController();
|
|
|
|
|
|
|
|
bool showSpinner = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-03-29 15:14:27 +02:00
|
|
|
String modeName = AppLocalizations.of(context)!.signIn;
|
2023-03-17 21:07:05 +01:00
|
|
|
if (widget.mode != Mode.signin) {
|
2023-03-29 15:14:27 +02:00
|
|
|
modeName = AppLocalizations.of(context)!.signUp;
|
2023-03-17 21:07:05 +01:00
|
|
|
}
|
2023-03-29 15:14:27 +02:00
|
|
|
String modeDescription = AppLocalizations.of(context)!.logIntoAccount;
|
2023-03-17 21:07:05 +01:00
|
|
|
if (widget.mode != Mode.signin) {
|
2023-03-29 15:14:27 +02:00
|
|
|
modeDescription = AppLocalizations.of(context)!.createNewAccount;
|
2023-03-17 21:07:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
final textTheme = Theme.of(context)
|
|
|
|
.textTheme
|
|
|
|
.apply(displayColor: Theme.of(context).colorScheme.onSurface);
|
|
|
|
|
|
|
|
return showSpinner
|
|
|
|
? Scaffold(
|
|
|
|
body: Center(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
const CircularProgressIndicator(),
|
2023-03-29 18:02:00 +02:00
|
|
|
Text(AppLocalizations.of(context)!.loading,
|
|
|
|
style: textTheme.titleLarge),
|
2023-03-17 21:07:05 +01:00
|
|
|
])))
|
|
|
|
: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(modeName),
|
|
|
|
),
|
|
|
|
body: Center(
|
2023-03-25 14:27:47 +01:00
|
|
|
child: ConstrainedBox(
|
|
|
|
constraints: const BoxConstraints(maxWidth: 400),
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrServer,
|
|
|
|
keyboardType: TextInputType.url,
|
2023-03-29 15:14:27 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.dns),
|
2023-03-29 18:02:00 +02:00
|
|
|
labelText: AppLocalizations.of(context)!
|
|
|
|
.inputServerLabel,
|
|
|
|
hintText:
|
|
|
|
AppLocalizations.of(context)!.inputServerHint,
|
|
|
|
helperText:
|
|
|
|
AppLocalizations.of(context)!.inputServerHelp,
|
2023-03-29 15:14:27 +02:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-25 14:27:47 +01:00
|
|
|
),
|
|
|
|
),
|
2023-03-24 16:42:12 +01:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: TextField(
|
2023-03-25 14:27:47 +01:00
|
|
|
controller: _ctrUsername,
|
|
|
|
keyboardType: TextInputType.emailAddress,
|
2023-03-29 15:14:27 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.person),
|
2023-03-29 18:02:00 +02:00
|
|
|
labelText: AppLocalizations.of(context)!
|
|
|
|
.inputUsernameLabel,
|
|
|
|
hintText: AppLocalizations.of(context)!
|
|
|
|
.inputUsernameHint,
|
|
|
|
helperText: AppLocalizations.of(context)!
|
|
|
|
.inputUsernameHelp,
|
|
|
|
border: const OutlineInputBorder(),
|
2023-03-17 21:07:05 +01:00
|
|
|
),
|
2023-03-24 16:42:12 +01:00
|
|
|
),
|
2023-03-25 14:27:47 +01:00
|
|
|
),
|
2023-03-24 16:42:12 +01:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: TextField(
|
2023-03-25 14:27:47 +01:00
|
|
|
controller: _ctrPassword,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
obscureText: true,
|
2023-03-29 15:14:27 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.lock),
|
2023-03-29 18:02:00 +02:00
|
|
|
labelText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordLabel,
|
|
|
|
hintText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordHint,
|
|
|
|
helperText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordHelp,
|
2023-03-29 15:14:27 +02:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-17 21:07:05 +01:00
|
|
|
),
|
2023-03-24 16:42:12 +01:00
|
|
|
),
|
2023-03-25 14:27:47 +01:00
|
|
|
),
|
|
|
|
// ONLY SIGNUP
|
|
|
|
...((widget.mode != Mode.signin)
|
|
|
|
? [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrPasswordRpt,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
|
|
|
obscureText: true,
|
2023-03-29 15:14:27 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.lock),
|
2023-03-29 18:02:00 +02:00
|
|
|
labelText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordRepeatLabel,
|
|
|
|
hintText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordRepeatHint,
|
|
|
|
helperText: AppLocalizations.of(context)!
|
|
|
|
.inputPasswordRepeatHelp,
|
2023-03-29 15:14:27 +02:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-25 14:27:47 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
: []),
|
|
|
|
// ONLY SIGNUP OTA
|
|
|
|
...((widget.mode == Mode.signupOTA)
|
|
|
|
? [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8),
|
|
|
|
child: TextField(
|
|
|
|
controller: _ctrOTA,
|
|
|
|
keyboardType: TextInputType.visiblePassword,
|
2023-03-29 15:14:27 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.key),
|
2023-03-29 18:02:00 +02:00
|
|
|
labelText: AppLocalizations.of(context)!
|
|
|
|
.inputOTALabel,
|
|
|
|
hintText: AppLocalizations.of(context)!
|
|
|
|
.inputOTAHint,
|
|
|
|
helperText: AppLocalizations.of(context)!
|
|
|
|
.inputOTAHelp,
|
2023-03-29 15:14:27 +02:00
|
|
|
border: const OutlineInputBorder(),
|
2023-03-25 14:27:47 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]
|
|
|
|
: []),
|
|
|
|
],
|
|
|
|
))),
|
2023-03-17 21:07:05 +01:00
|
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
|
|
onPressed: () async {
|
|
|
|
setState(() {
|
|
|
|
showSpinner = true;
|
|
|
|
});
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
final scaffMgr = ScaffoldMessenger.of(context);
|
|
|
|
|
2023-03-17 21:07:05 +01:00
|
|
|
// verify that both passwords are the same
|
|
|
|
if (widget.mode != Mode.signin) {
|
|
|
|
if (_ctrPassword.text != _ctrPasswordRpt.text) {
|
|
|
|
setState(() {
|
|
|
|
showSpinner = false;
|
|
|
|
});
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
showSimpleSnackbar(scaffMgr,
|
2023-03-29 18:02:00 +02:00
|
|
|
text: AppLocalizations.of(context)!
|
|
|
|
.errorPasswordsDoNotMatch,
|
2023-03-29 15:14:27 +02:00
|
|
|
action: AppLocalizations.of(context)!.dismiss);
|
2023-03-17 21:07:05 +01:00
|
|
|
|
|
|
|
_ctrPasswordRpt.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// password has to be at least 6 characters long
|
|
|
|
if (_ctrPassword.text.length < 6) {
|
|
|
|
setState(() {
|
|
|
|
showSpinner = false;
|
|
|
|
});
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
showSimpleSnackbar(scaffMgr,
|
2023-03-29 15:14:27 +02:00
|
|
|
text: AppLocalizations.of(context)!.errorPasswordLength,
|
|
|
|
action: AppLocalizations.of(context)!.dismiss);
|
2023-03-17 21:07:05 +01:00
|
|
|
|
|
|
|
_ctrPasswordRpt.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
// resolve homeserver url
|
2023-03-17 21:07:05 +01:00
|
|
|
OutbagServer server;
|
|
|
|
try {
|
|
|
|
server = await getOutbagServerUrl(_ctrServer.text);
|
|
|
|
} catch (e) {
|
|
|
|
// unable to find outbag server
|
|
|
|
// at given server address
|
|
|
|
// stop authentification
|
|
|
|
setState(() {
|
|
|
|
showSpinner = false;
|
|
|
|
});
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
showSimpleSnackbar(scaffMgr,
|
2023-03-29 18:02:00 +02:00
|
|
|
text: AppLocalizations.of(context)!
|
|
|
|
.errorInvalidServer(_ctrServer.text),
|
2023-03-29 15:14:27 +02:00
|
|
|
action: AppLocalizations.of(context)!.dismiss);
|
2023-03-17 21:07:05 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-23 14:48:53 +01:00
|
|
|
// hash password
|
2023-03-25 14:27:47 +01:00
|
|
|
final password = hashPassword(_ctrPassword.text);
|
2023-03-23 14:48:53 +01:00
|
|
|
|
2023-03-25 17:18:46 +01:00
|
|
|
doNetworkRequest(scaffMgr, req: () {
|
2023-03-25 14:27:47 +01:00
|
|
|
if (widget.mode == Mode.signin) {
|
|
|
|
return postUnauthorized(
|
2023-03-17 21:07:05 +01:00
|
|
|
target: server,
|
|
|
|
path: 'signin',
|
|
|
|
body: {
|
|
|
|
'name': _ctrUsername.text,
|
|
|
|
'server': server.tag,
|
|
|
|
'accountKey': password
|
2023-03-25 14:27:47 +01:00
|
|
|
});
|
|
|
|
} else if (widget.mode == Mode.signup) {
|
|
|
|
return postUnauthorized(
|
2023-03-17 21:07:05 +01:00
|
|
|
target: server,
|
|
|
|
path: 'signup',
|
|
|
|
body: {
|
|
|
|
'name': _ctrUsername.text,
|
|
|
|
'server': server.tag,
|
|
|
|
'accountKey': password
|
2023-03-25 14:27:47 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// signup OTA
|
|
|
|
return postUnauthorized(
|
2023-03-17 21:07:05 +01:00
|
|
|
target: server,
|
|
|
|
path: 'signupOTA',
|
|
|
|
body: {
|
|
|
|
'name': _ctrUsername.text,
|
|
|
|
'server': server.tag,
|
|
|
|
'accountKey': password,
|
|
|
|
'OTA': _ctrOTA.text
|
2023-03-25 14:27:47 +01:00
|
|
|
});
|
2023-03-17 21:07:05 +01:00
|
|
|
}
|
2023-03-25 14:27:47 +01:00
|
|
|
}, onOK: (body) async {
|
|
|
|
// authorize user
|
|
|
|
await User(
|
|
|
|
username: _ctrUsername.text,
|
|
|
|
password: password,
|
|
|
|
server: server)
|
|
|
|
.toDisk();
|
2023-03-29 18:02:00 +02:00
|
|
|
widget.refresh();
|
2023-03-25 14:27:47 +01:00
|
|
|
}, after: () {
|
|
|
|
setState(() {
|
|
|
|
showSpinner = false;
|
|
|
|
});
|
|
|
|
});
|
2023-03-17 21:07:05 +01:00
|
|
|
},
|
|
|
|
label: Text(modeName),
|
|
|
|
icon: const Icon(Icons.check),
|
|
|
|
tooltip: modeDescription,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|