113 lines
3.5 KiB
Bash
113 lines
3.5 KiB
Bash
import 'dart:io';
|
|
import 'dart:convert';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'roomlist';
|
|
|
|
|
|
class LoginPage extends StatefulWidget {
|
|
LoginPage({Key key, this.settings}) : super(key: key);
|
|
|
|
final SharedPreferences settings;
|
|
|
|
@override
|
|
_LoginPageState createState() => _LoginPageState();
|
|
}
|
|
|
|
class _LoginPageState extends State<LoginPage> {
|
|
static const String def_home_server = "https://pink.packageloss.eu:8448";
|
|
|
|
String _matrixId;
|
|
String _password;
|
|
String _homeServer = "https://pink.packageloss.eu:8448";
|
|
|
|
String _failureMessage = "";
|
|
|
|
void _showMainView() {
|
|
Navigator.pushReplacement(
|
|
context, MaterialPageRoute(builder: (BuildContext context) => MyHomePage(settings: widget.settings)));
|
|
}
|
|
|
|
void _doLogin() async {
|
|
var client = new HttpClient();
|
|
await client.postUrl(Uri.parse(_homeServer)
|
|
.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>{
|
|
"type": "m.login.password",
|
|
"identifier": <String,String>{
|
|
"type": "m.id.user",
|
|
"user": _matrixId,
|
|
},
|
|
"password": _password,
|
|
"initial_device_display_name": "Hermes alpha",
|
|
});
|
|
req.write(data);
|
|
return req.close();
|
|
}).then((resp) {
|
|
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}";
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text("Login"),
|
|
),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: ListView(
|
|
children: <Widget>[
|
|
TextField(autocorrect: false, onChanged: (s) { _matrixId = s; }, autofocus: true),
|
|
Text("Matrix ID"),
|
|
TextField(autocorrect: false, obscureText: true, onChanged: (s) { _password = s; }),
|
|
Text("Password"),
|
|
TextField(
|
|
autocorrect: false,
|
|
controller: TextEditingController(text: def_home_server),
|
|
onChanged: (s) { _homeServer = s; }
|
|
),
|
|
Text("Home server"),
|
|
RaisedButton(
|
|
child: Text("Login"),
|
|
onPressed: () { setState(() { _doLogin(); }); },
|
|
),
|
|
Text(_failureMessage),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|