Add button to connect to demo server

Closes #13
This commit is contained in:
Lea 2023-02-05 16:54:49 +01:00
parent 781551c95f
commit 35218c2e52
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

View file

@ -4,7 +4,7 @@ import 'package:shared_preferences/shared_preferences.dart';
class LoginPage extends StatefulWidget {
final FeverAPI api;
const LoginPage({ super.key, required this.api });
const LoginPage({super.key, required this.api});
@override
State<LoginPage> createState() => _LoginPageState();
@ -72,9 +72,10 @@ class _LoginPageState extends State<LoginPage> {
border: const OutlineInputBorder(),
contentPadding: const EdgeInsets.all(8.0),
hintText: 'https://among-us.morbius.sus/api/fever.php',
errorText: _validAPI == false ? 'Invalid or unsupported API' : null,
)
),
errorText: _validAPI == false
? 'Invalid or unsupported API'
: null,
)),
],
),
),
@ -87,7 +88,8 @@ class _LoginPageState extends State<LoginPage> {
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Flexible(child: TextField(
Flexible(
child: TextField(
controller: _usernameController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
@ -96,7 +98,8 @@ class _LoginPageState extends State<LoginPage> {
),
)),
const SizedBox(width: 8),
Flexible(child: TextField(
Flexible(
child: TextField(
controller: _passwordController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
@ -110,14 +113,63 @@ class _LoginPageState extends State<LoginPage> {
],
),
),
Container(
margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 4.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
ElevatedButton(
onPressed: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("Connect to demo instance"),
content: const Text(
"If you want to try out this application before setting "
"up your own RSS aggregator, you can connect to our demo instance.\n\n"
"Please note that the demo server is not intended "
"for normal usage. If you intend to use this app, "
"please set up your own server."),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text("Cancel"),
),
TextButton(
onPressed: () async {
var apiUrl = "https://rss.amogus.cloud/api/fever.php";
var apiKey = FeverAPI.generateApiKey("demo", "PF6bdYLH");
final prefs = await SharedPreferences.getInstance();
await prefs.setString('apiUrl', apiUrl);
await prefs.setString('apiKey', apiKey);
await prefs.setBool('isDemo', true);
widget.api.apiUrl = apiUrl;
widget.api.apiKey = apiKey;
// ignore: use_build_context_synchronously
Navigator.of(context).popUntil((route) => route.isFirst);
},
child: const Text("Connect!"),
)
],
),
);
},
child: const Text("Demo"),
),
ElevatedButton(
onPressed: () async {
if (_urlController.text.isEmpty) return;
_validAPI ??= await checkUrl();
if (_validAPI != true || _usernameController.text.isEmpty || _passwordController.text.isEmpty) return;
if (_validAPI != true ||
_usernameController.text.isEmpty ||
_passwordController.text.isEmpty) return;
var apiKey = FeverAPI.generateApiKey(_usernameController.text, _passwordController.text);
var isAuthenticated = await FeverAPI.isAuthenticated(_urlController.text, apiKey);
var apiKey = FeverAPI.generateApiKey(
_usernameController.text, _passwordController.text);
var isAuthenticated = await FeverAPI.isAuthenticated(
_urlController.text, apiKey);
if (!isAuthenticated) {
// ignore: use_build_context_synchronously
@ -126,13 +178,16 @@ class _LoginPageState extends State<LoginPage> {
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Authentication failed'),
content: const Text('The API URL seems to be valid, but the provided credentials appear to be incorrect.'),
content: const Text(
'The API URL seems to be valid, but the provided credentials appear to be incorrect.'),
actions: [
TextButton(onPressed: () => Navigator.of(context).pop(), child: const Text('Got it!')),
TextButton(
onPressed: () =>
Navigator.of(context).pop(),
child: const Text('Got it!')),
],
);
}
);
});
return;
}
@ -150,6 +205,9 @@ class _LoginPageState extends State<LoginPage> {
),
],
),
),
],
),
);
}
}