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
Denys Konovalov 6ad143195a ==0.7.5==
- added A LOT, TLTD
Settings menu, updated homepage tiles, added color settings...
- TODO:
  - update README
  - update license & copyright info
  - cleanup code
  - merge code parts to server side
  - complete settings
  - fix 11/12
  - add caching
  - muuuuuuuch more...
2021-11-06 11:01:44 +01:00

167 lines
8.0 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) {
Future.delayed(Duration.zero, () {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
const Dashboard()),
);
});
}
},
child: const Text("Anmelden"))
],
),
)))));
},
);
}
}