bae624dd67
- added A LOT, TLTD - TODO: - update README - update license & copyright info - cleanup code - merge code parts to server side - add more settings - fix 11/12 - add more timetable options - add timetable additional info - muuuuuuuch more...
165 lines
7.8 KiB
Dart
165 lines
7.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:http/http.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'networking.dart';
|
|
import 'dashboard.dart';
|
|
|
|
Future<bool> checkKey() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? apiKey = prefs.getString('api_key');
|
|
return apiKey != null && apiKey.isNotEmpty;
|
|
}
|
|
|
|
class Login extends StatelessWidget {
|
|
final userController = TextEditingController();
|
|
final passwordController = TextEditingController();
|
|
final otpController = TextEditingController();
|
|
final devIdController = TextEditingController();
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
double widgetWidth = constraints.maxWidth;
|
|
double widgetHeight = constraints.maxHeight;
|
|
var factor;
|
|
|
|
if (widgetWidth <= 600) {
|
|
factor = 1;
|
|
} else if (widgetWidth <= 1400) {
|
|
factor = 2;
|
|
} else if (widgetWidth <= 2000) {
|
|
factor = 3;
|
|
}
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text("Anmelden"),
|
|
),
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
child: Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
|
|
child: Container(
|
|
constraints: BoxConstraints(
|
|
maxWidth:
|
|
MediaQuery.of(context).size.width / factor,
|
|
),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Image.asset("assets/images/meincantor_r.png",
|
|
height: 192, width: 192),
|
|
const Divider(),
|
|
AutofillGroup(
|
|
child: Column(
|
|
children: [
|
|
TextField(
|
|
autofillHints: const [
|
|
AutofillHints.username
|
|
],
|
|
decoration: const InputDecoration(
|
|
icon: Icon(CupertinoIcons.person),
|
|
border: OutlineInputBorder(),
|
|
labelText: 'Benutzername',
|
|
),
|
|
controller: userController,
|
|
),
|
|
const Divider(),
|
|
TextField(
|
|
autofillHints: const [
|
|
AutofillHints.password
|
|
],
|
|
decoration: const InputDecoration(
|
|
icon: Icon(CupertinoIcons.lock),
|
|
border: OutlineInputBorder(),
|
|
labelText: 'Passwort',
|
|
),
|
|
obscureText: true,
|
|
controller: passwordController,
|
|
),
|
|
],
|
|
)),
|
|
const Divider(),
|
|
TextField(
|
|
decoration: const InputDecoration(
|
|
icon: Icon(CupertinoIcons.lock),
|
|
border: OutlineInputBorder(),
|
|
labelText: '2F2-Code (OTP) [falls aktiviert]',
|
|
),
|
|
obscureText: true,
|
|
controller: otpController,
|
|
),
|
|
const Divider(),
|
|
TextField(
|
|
decoration: const InputDecoration(
|
|
icon: Icon(CupertinoIcons.device_laptop),
|
|
border: OutlineInputBorder(),
|
|
labelText: 'Gerätebezeichnung',
|
|
),
|
|
controller: devIdController,
|
|
),
|
|
const Divider(),
|
|
OutlinedButton(
|
|
onPressed: () async {
|
|
SharedPreferences prefs =
|
|
await SharedPreferences.getInstance();
|
|
Response loginResponse = await getToken(
|
|
userController.text,
|
|
passwordController.text,
|
|
otpController.text,
|
|
devIdController.text);
|
|
if (loginResponse.statusCode == 200) {
|
|
String apiKey = jsonDecode(utf8.decode(
|
|
loginResponse.bodyBytes))['token'];
|
|
print('Set new API key to $apiKey');
|
|
await prefs.setString('api_key', apiKey);
|
|
dynamic userinfo = jsonDecode(
|
|
await getUserInfo(
|
|
userController.text,
|
|
passwordController.text,
|
|
otpController.text,
|
|
devIdController.text));
|
|
await prefs.setString('user',
|
|
userinfo['preferred_username']);
|
|
await prefs.setString(
|
|
'name', userinfo['name']);
|
|
await prefs.setString(
|
|
'class_num',
|
|
userinfo['groups'][0]
|
|
.replaceAll("_", "/"));
|
|
} else if (loginResponse.statusCode ==
|
|
401) {
|
|
String text = loginResponse.body;
|
|
final snackBar = SnackBar(
|
|
content: Text('Fehler: $text'));
|
|
|
|
// Find the ScaffoldMessenger in the widget tree
|
|
// and use it to show a SnackBar.
|
|
ScaffoldMessenger.of(context)
|
|
.showSnackBar(snackBar);
|
|
}
|
|
if (prefs.getString('api_key') != null &&
|
|
prefs
|
|
.getString('api_key')!
|
|
.isNotEmpty) {
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) =>
|
|
const Dashboard()),
|
|
);
|
|
}
|
|
},
|
|
child: const Text("Anmelden"))
|
|
],
|
|
),
|
|
)))));
|
|
},
|
|
);
|
|
}
|
|
}
|