This repository has been archived on 2020-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
hermes/lib/page/login

113 lines
3.5 KiB
Plaintext
Raw Permalink Normal View History

2019-01-30 18:36:10 +00:00
import 'dart:io';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
2019-07-30 11:28:52 +00:00
import 'roomlist';
2019-04-21 20:47:32 +00:00
2019-01-30 18:36:10 +00:00
class LoginPage extends StatefulWidget {
LoginPage({Key key, this.settings}) : super(key: key);
final SharedPreferences settings;
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
2019-04-24 12:57:24 +00:00
static const String def_home_server = "https://pink.packageloss.eu:8448";
2019-01-30 18:36:10 +00:00
2019-04-24 12:57:24 +00:00
String _matrixId;
2019-01-30 18:36:10 +00:00
String _password;
2019-04-24 12:57:24 +00:00
String _homeServer = "https://pink.packageloss.eu:8448";
2019-01-30 18:36:10 +00:00
2019-04-24 12:57:24 +00:00
String _failureMessage = "";
2019-01-30 18:36:10 +00:00
2019-04-21 20:47:32 +00:00
void _showMainView() {
2019-07-30 11:28:52 +00:00
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (BuildContext context) => MyHomePage(settings: widget.settings)));
2019-04-21 20:47:32 +00:00
}
2019-07-30 11:28:52 +00:00
2019-01-30 18:36:10 +00:00
void _doLogin() async {
var client = new HttpClient();
2019-04-24 12:57:24 +00:00
await client.postUrl(Uri.parse(_homeServer)
2019-01-30 18:36:10 +00:00
.replace(path: "/_matrix/client/r0/login")).then((req) {
req.headers.contentType = new ContentType("application", "json", charset: "utf-8");
var data = new JsonEncoder().convert(<String,dynamic>{
2020-01-27 16:18:35 +00:00
"type": "m.login.password",
"identifier": <String,String>{
"type": "m.id.user",
"user": _matrixId,
},
"password": _password,
"initial_device_display_name": "Hermes alpha",
2019-01-30 18:36:10 +00:00
});
req.write(data);
return req.close();
}).then((resp) {
2020-01-27 16:18:35 +00:00
return resp.transform(utf8.decoder).join().then((cont) {
if (resp.statusCode != 200) {
_failureMessage = "HTTP ${resp.statusCode}";
}
var json;
try {
json = new JsonDecoder().convert(cont);
}
catch(e) {
_failureMessage = "Invalid JSON!";
return;
}
// I hate switch statements
if (resp.statusCode == 403) {
_failureMessage = json["error"];
} else if (resp.statusCode == 200) {
var s = widget.settings;
s.setString("access_token", json["access_token"]);
s.setString("matrix_id", json["user_id"]);
//this means the hs uri was correct, but not neccesarily what the server gives us
s.setString("home_server", Uri.parse(_homeServer).toString()); // jep, stealing that again
//s.setString("home_server", json["home_server"]);
s.setString("device_id", json["device_id"]);
s.setBool("logged_in", true);
_showMainView();
} else {
_failureMessage = "{$resp.statusCode}: ${json}";
}
});
2019-01-30 18:36:10 +00:00
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Login"),
),
body: Center(
child: Padding(
padding: EdgeInsets.all(8.0),
2019-04-21 20:47:32 +00:00
child: ListView(
2019-01-30 18:36:10 +00:00
children: <Widget>[
2020-01-27 16:18:35 +00:00
TextField(autocorrect: false, onChanged: (s) { _matrixId = s; }, autofocus: true),
2019-04-21 20:47:32 +00:00
Text("Matrix ID"),
2019-01-30 18:36:10 +00:00
TextField(autocorrect: false, obscureText: true, onChanged: (s) { _password = s; }),
2019-04-21 20:47:32 +00:00
Text("Password"),
2019-01-30 18:36:10 +00:00
TextField(
autocorrect: false,
controller: TextEditingController(text: def_home_server),
2019-04-24 12:57:24 +00:00
onChanged: (s) { _homeServer = s; }
2019-01-30 18:36:10 +00:00
),
2019-04-21 20:47:32 +00:00
Text("Home server"),
RaisedButton(
2020-01-27 16:18:35 +00:00
child: Text("Login"),
onPressed: () { setState(() { _doLogin(); }); },
2019-01-30 18:36:10 +00:00
),
Text(_failureMessage),
2019-01-30 18:36:10 +00:00
],
),
),
),
);
}
}