Tried to set up auth tokens
I always get invalid token error when I try to login with mLoginToken type
This commit is contained in:
parent
ea7587fe2d
commit
3728172844
|
@ -1,4 +1,5 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:zenith/rooms.dart';
|
||||
|
@ -18,7 +19,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||
bool loggingIn = false;
|
||||
String failedMessage = '';
|
||||
|
||||
void connectMatrix() async {
|
||||
void login() async {
|
||||
setState(() {
|
||||
loggingIn = true;
|
||||
});
|
||||
|
@ -26,10 +27,22 @@ class _LoginPageState extends State<LoginPage> {
|
|||
try {
|
||||
final provider =
|
||||
Provider.of<ZenithClientProvider>(context, listen: false);
|
||||
await provider.initialize(serverController.text, usernameController.text,
|
||||
passwordController.text);
|
||||
final client = Client("zenith");
|
||||
await client.checkHomeserver(Uri.parse(serverController.text));
|
||||
final result = await client.login(
|
||||
LoginType.mLoginPassword,
|
||||
password: passwordController.text,
|
||||
identifier: AuthenticationUserIdentifier(user: usernameController.text),
|
||||
);
|
||||
provider.setClient(client);
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString("homeserver", serverController.text);
|
||||
prefs.setString("username", usernameController.text);
|
||||
prefs.setString("accessToken", result.accessToken);
|
||||
prefs.setString("refreshToken", result.refreshToken ?? "");
|
||||
} catch (error) {
|
||||
print("Error signing in $error");
|
||||
print(error);
|
||||
setState(() {
|
||||
failedMessage = error.toString();
|
||||
loggingIn = false;
|
||||
|
@ -47,11 +60,6 @@ class _LoginPageState extends State<LoginPage> {
|
|||
});
|
||||
}
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.setString("homeserver", serverController.text);
|
||||
prefs.setString("username", usernameController.text);
|
||||
prefs.setString("password", passwordController.text);
|
||||
|
||||
if (!mounted) return;
|
||||
Navigator.pushAndRemoveUntil(
|
||||
context,
|
||||
|
@ -92,7 +100,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||
controller: passwordController,
|
||||
decoration: const InputDecoration(labelText: 'Password'),
|
||||
obscureText: true,
|
||||
onFieldSubmitted: (value) => connectMatrix(),
|
||||
onFieldSubmitted: (value) => login(),
|
||||
),
|
||||
Text(failedMessage,
|
||||
style: Theme.of(context).textTheme.headlineSmall)
|
||||
|
@ -101,7 +109,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: loggingIn ? null : connectMatrix,
|
||||
onPressed: loggingIn ? null : login,
|
||||
tooltip: 'Log in',
|
||||
child: loggingIn
|
||||
? const CircularProgressIndicator()
|
||||
|
|
|
@ -37,22 +37,35 @@ class MyHomePage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _MyHomePageState extends State<MyHomePage> {
|
||||
bool savedCreds = false;
|
||||
bool showRooms = false;
|
||||
String errorMessage = "";
|
||||
|
||||
void getCreds() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final homeserver = prefs.getString("homeserver");
|
||||
final username = prefs.getString("username");
|
||||
final password = prefs.getString("password");
|
||||
final accessToken = prefs.getString("accessToken");
|
||||
final refreshToken = prefs.getString("refreshToken");
|
||||
|
||||
if (homeserver == null || username == null || password == null) return;
|
||||
setState(() {
|
||||
savedCreds = true;
|
||||
});
|
||||
if (homeserver == null || username == null || accessToken == null) return;
|
||||
|
||||
if (!mounted) return;
|
||||
final provider = Provider.of<ZenithClientProvider>(context, listen: false);
|
||||
await provider.initialize(homeserver, username, password);
|
||||
final client = Client("zenith");
|
||||
|
||||
try {
|
||||
await client.checkHomeserver(Uri.parse(homeserver));
|
||||
await client.login(LoginType.mLoginToken, token: accessToken);
|
||||
provider.setClient(client);
|
||||
setState(() {
|
||||
showRooms = true;
|
||||
});
|
||||
} catch (error) {
|
||||
print(error);
|
||||
setState(() {
|
||||
errorMessage = error.toString();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
@ -63,10 +76,9 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (!savedCreds) {
|
||||
return const LoginPage();
|
||||
} else {
|
||||
if (showRooms)
|
||||
return const RoomsPage();
|
||||
}
|
||||
else
|
||||
return const LoginPage();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ class RoomsPage extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _RoomsPageState extends State<RoomsPage> {
|
||||
bool loadingRooms = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
@ -79,7 +81,13 @@ class _RoomsPageState extends State<RoomsPage> {
|
|||
padding: const EdgeInsets.all(16.0),
|
||||
child: Center(child: Consumer<ZenithClientProvider>(
|
||||
builder: (context, provider, child) {
|
||||
if (provider.loading)
|
||||
provider.client.roomsLoading?.then((value) {
|
||||
setState(() {
|
||||
loadingRooms = false;
|
||||
});
|
||||
});
|
||||
|
||||
if (provider.client.roomsLoading != null && loadingRooms)
|
||||
return const CircularProgressIndicator();
|
||||
else
|
||||
return ListView.builder(
|
||||
|
|
|
@ -3,28 +3,11 @@ import 'package:matrix/matrix.dart';
|
|||
|
||||
class ZenithClientProvider extends ChangeNotifier {
|
||||
late Client _client;
|
||||
bool _loading = true;
|
||||
|
||||
Client get client => _client;
|
||||
|
||||
bool get loading => _loading;
|
||||
|
||||
Future<void> initialize(
|
||||
String homeserver, String username, String password) async {
|
||||
_loading = true;
|
||||
_client = Client("zenith");
|
||||
try {
|
||||
print("Checking homeserver...");
|
||||
await client.checkHomeserver(Uri.parse(homeserver));
|
||||
print("Logging in...");
|
||||
await client.login(LoginType.mLoginPassword,
|
||||
identifier: AuthenticationUserIdentifier(user: username),
|
||||
password: password);
|
||||
await client.roomsLoading;
|
||||
await client.accountDataLoading;
|
||||
} finally {
|
||||
_loading = false;
|
||||
}
|
||||
void setClient(Client value) {
|
||||
_client = value;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user