loris/lib/pages/login.dart

169 lines
5.1 KiB
Dart
Raw Normal View History

2022-09-03 19:08:53 +00:00
import 'package:flutter/foundation.dart';
2022-06-16 20:30:02 +00:00
import 'package:flutter/material.dart';
2022-06-17 10:38:28 +00:00
import 'package:localization/localization.dart';
2022-09-03 19:08:53 +00:00
import 'package:loris/pages/login/weblogin.dart';
2022-06-28 20:05:24 +00:00
import '../business_logic/auth/oauth.dart' as oauth;
2022-06-16 20:30:02 +00:00
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@override
State<Login> createState() => _LoginState();
}
class _LoginState extends State<Login> {
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Padding(
padding: EdgeInsets.all(24),
child: LoginForm(),
),
);
}
}
class LoginForm extends StatefulWidget {
const LoginForm({Key? key}) : super(key: key);
@override
State<LoginForm> createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final formKey = GlobalKey<FormState>();
2022-08-14 11:32:26 +00:00
String instanceUrl = "";
2022-06-16 20:30:02 +00:00
@override
Widget build(BuildContext context) {
return Form(
onChanged: () {
formKey.currentState!.validate();
},
key: formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
2022-07-02 22:03:10 +00:00
Text("greeting".i18n(), style: Theme.of(context).textTheme.headline1),
2022-06-16 20:30:02 +00:00
TextFormField(
2022-06-30 15:34:09 +00:00
onSaved: (value) async {
2022-08-14 11:32:26 +00:00
instanceUrl = value ?? "";
2022-06-30 15:34:09 +00:00
},
2022-06-17 10:38:28 +00:00
decoration: InputDecoration(
2022-07-03 13:47:24 +00:00
labelText: "instance-url".i18n(),
hintText: "instance-url-example".i18n(),
icon: const Icon(Icons.home),
prefixText: "https://",
2022-06-16 20:30:02 +00:00
),
autofocus: true,
),
TextButton.icon(
onPressed: () {
bool isValid = formKey.currentState!.validate();
if (!isValid) {
2022-08-13 16:14:12 +00:00
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
"login-failed-snackbar-text".i18n(),
),
),
);
2022-06-28 20:05:24 +00:00
} else {
2022-06-30 15:34:09 +00:00
formKey.currentState?.save();
2022-09-03 19:08:53 +00:00
if (kIsWeb) {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => Weblogin(url: instanceUrl),
));
} else {
Navigator.pushReplacement(
2022-06-28 20:05:24 +00:00
context,
MaterialPageRoute(
2022-08-14 11:32:26 +00:00
builder: (context) => AuthPage(url: instanceUrl),
2022-09-03 19:08:53 +00:00
),
);
}
}
2022-06-16 20:30:02 +00:00
},
2022-06-17 10:38:28 +00:00
icon: const Icon(Icons.login),
label: Text("authorize-in-browser".i18n()))
2022-06-16 20:30:02 +00:00
],
),
);
}
}
2022-06-28 20:05:24 +00:00
2022-06-30 15:34:09 +00:00
/*
page that handles authenticating user
*/
2022-06-28 20:05:24 +00:00
class AuthPage extends StatefulWidget {
2022-08-14 11:32:26 +00:00
const AuthPage({required this.url, Key? key}) : super(key: key);
final String url;
2022-06-28 20:05:24 +00:00
@override
State<AuthPage> createState() => _AuthPageState();
}
class _AuthPageState extends State<AuthPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
2022-06-28 20:47:35 +00:00
body: Padding(
2022-06-30 15:34:09 +00:00
padding: const EdgeInsets.all(24.0),
2022-06-28 20:47:35 +00:00
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
2022-07-01 15:14:44 +00:00
FutureBuilder<int>(
2022-08-14 11:32:26 +00:00
future: oauth.handleFullOauth(widget.url),
2022-07-01 15:14:44 +00:00
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text("login-failed-snackbar-text".i18n());
} else if (snapshot.hasData) {
if (snapshot.data != null) {
if (snapshot.data != 200) {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.error),
const SizedBox(
width: 24,
),
Text("error ${snapshot.data}"),
],
);
}
}
return TextButton.icon(
2022-08-14 11:32:26 +00:00
onPressed: () async {
await Navigator.pushReplacementNamed(context, "/");
},
icon: const Icon(Icons.arrow_forward),
label: Text(
"confirm".i18n(),
),
);
2022-07-01 15:14:44 +00:00
} else {
return const CircularProgressIndicator();
}
},
),
2022-06-30 15:34:09 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
TextButton.icon(
2022-08-14 11:32:26 +00:00
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(Icons.arrow_back),
label: Text(
"back-button".i18n(),
),
),
2022-06-30 15:34:09 +00:00
],
)
2022-06-28 20:47:35 +00:00
],
),
2022-06-28 20:05:24 +00:00
),
);
}
}