This repository has been archived on 2023-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
meincantor-app/lib/login.dart

165 lines
7.9 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:meincantor/networking.dart';
import 'package:meincantor/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 {
Login({Key? key}) : super(key: key);
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;
2021-11-19 20:12:42 +01:00
int factor;
if (widgetWidth <= 600) {
factor = 1;
} else if (widgetWidth <= 1400) {
factor = 2;
} else if (widgetWidth <= 2000) {
factor = 3;
2021-11-19 20:12:42 +01:00
} else {
factor = 4;
}
return Scaffold(
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: [
MediaQuery.of(context).platformBrightness ==
Brightness.light
? Image.asset(
"assets/images/meincantor-big.png",
width: 256)
: Image.asset(
"assets/images/meincantor-big-dark.png",
width: 256),
const Divider(),
AutofillGroup(
child: Column(
children: [
TextField(
autofillHints: const [
AutofillHints.username
],
decoration: const InputDecoration(
icon: Icon(MdiIcons.identifier),
border: OutlineInputBorder(),
labelText: 'Benutzername',
),
controller: userController,
),
const Divider(),
TextField(
autofillHints: const [
AutofillHints.password
],
decoration: const InputDecoration(
icon: Icon(MdiIcons.lock),
border: OutlineInputBorder(),
labelText: 'Passwort',
),
obscureText: true,
controller: passwordController,
),
],
)),
const Divider(),
TextField(
decoration: const InputDecoration(
icon: Icon(MdiIcons.twoFactorAuthentication),
border: OutlineInputBorder(),
labelText: '2F2-Code',
),
obscureText: true,
controller: otpController,
),
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'];
prefs.setString('api_key', apiKey);
dynamic userinfo = jsonDecode(
await getUserInfo(
userController.text,
passwordController.text,
otpController.text,
devIdController.text));
prefs.setString('user',
userinfo['preferred_username']);
prefs.setString('user',
userinfo['preferred_username']);
prefs.setString('name', userinfo['name']);
prefs.setString(
'email', userinfo['email']);
prefs.setString(
'class_num',
userinfo['groups'][0]
.replaceAll("_", "/"));
} else if (loginResponse.statusCode ==
401) {
String text = loginResponse.body;
final snackBar = SnackBar(
content: Text('Fehler: $text'));
ScaffoldMessenger.of(context)
.showSnackBar(snackBar);
}
if (prefs.getString('api_key') != null &&
prefs
.getString('api_key')!
.isNotEmpty) {
Future.delayed(Duration.zero, () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
const Dashboard()),
);
});
}
},
child: const Text("Anmelden"))
],
),
)))));
},
);
}
}