fix: separate server setup page, fix already-logged-in precondition error
This commit is contained in:
+17
-3
@@ -2,6 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:pyramid/features/auth/login_notifier.dart';
|
import 'package:pyramid/features/auth/login_notifier.dart';
|
||||||
import 'package:pyramid/features/auth/login_page.dart';
|
import 'package:pyramid/features/auth/login_page.dart';
|
||||||
|
import 'package:pyramid/features/auth/server_page.dart';
|
||||||
import 'package:pyramid/features/chat/chat_page.dart';
|
import 'package:pyramid/features/chat/chat_page.dart';
|
||||||
import 'package:pyramid/features/rooms/rooms_page.dart';
|
import 'package:pyramid/features/rooms/rooms_page.dart';
|
||||||
import 'package:pyramid/features/settings/settings_page.dart';
|
import 'package:pyramid/features/settings/settings_page.dart';
|
||||||
@@ -9,16 +10,29 @@ import 'package:pyramid/layout/shell_page.dart';
|
|||||||
|
|
||||||
final routerProvider = Provider<GoRouter>((ref) {
|
final routerProvider = Provider<GoRouter>((ref) {
|
||||||
final isLoggedIn = ref.watch(isLoggedInProvider);
|
final isLoggedIn = ref.watch(isLoggedInProvider);
|
||||||
|
final homeserverAsync = ref.watch(homeserverProvider);
|
||||||
|
final hasServer = homeserverAsync.valueOrNull != null;
|
||||||
|
|
||||||
return GoRouter(
|
return GoRouter(
|
||||||
initialLocation: '/rooms',
|
initialLocation: '/rooms',
|
||||||
redirect: (context, state) {
|
redirect: (context, state) {
|
||||||
final loggingIn = state.matchedLocation == '/login';
|
final loc = state.matchedLocation;
|
||||||
if (!isLoggedIn && !loggingIn) return '/login';
|
final onAuth = loc == '/login' || loc == '/server';
|
||||||
if (isLoggedIn && loggingIn) return '/rooms';
|
|
||||||
|
if (!isLoggedIn) {
|
||||||
|
if (!hasServer && loc != '/server') return '/server';
|
||||||
|
if (hasServer && loc != '/login' && !onAuth) return '/login';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLoggedIn && onAuth) return '/rooms';
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
routes: [
|
routes: [
|
||||||
|
GoRoute(
|
||||||
|
path: '/server',
|
||||||
|
builder: (context, state) => const ServerPage(),
|
||||||
|
),
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: '/login',
|
path: '/login',
|
||||||
builder: (context, state) => const LoginPage(),
|
builder: (context, state) => const LoginPage(),
|
||||||
|
|||||||
@@ -1,6 +1,20 @@
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
import 'package:pyramid/core/matrix_client.dart';
|
import 'package:pyramid/core/matrix_client.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
const _prefHomeserver = 'homeserver_url';
|
||||||
|
|
||||||
|
// Gespeicherter Homeserver aus SharedPreferences
|
||||||
|
final homeserverProvider = FutureProvider<String?>((ref) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
return prefs.getString(_prefHomeserver);
|
||||||
|
});
|
||||||
|
|
||||||
|
Future<void> saveHomeserver(String url) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
await prefs.setString(_prefHomeserver, url);
|
||||||
|
}
|
||||||
|
|
||||||
enum LoginStatus { idle, loading, error }
|
enum LoginStatus { idle, loading, error }
|
||||||
|
|
||||||
@@ -19,18 +33,17 @@ class LoginState {
|
|||||||
|
|
||||||
class LoginNotifier extends StateNotifier<LoginState> {
|
class LoginNotifier extends StateNotifier<LoginState> {
|
||||||
final Ref _ref;
|
final Ref _ref;
|
||||||
|
|
||||||
LoginNotifier(this._ref) : super(const LoginState());
|
LoginNotifier(this._ref) : super(const LoginState());
|
||||||
|
|
||||||
Future<void> login({
|
Future<void> login({
|
||||||
required String userId,
|
required String username,
|
||||||
required String password,
|
required String password,
|
||||||
|
required String homeserver,
|
||||||
String? deviceName,
|
String? deviceName,
|
||||||
}) async {
|
}) async {
|
||||||
state = state.copyWith(status: LoginStatus.loading, errorMessage: null);
|
state = state.copyWith(status: LoginStatus.loading, errorMessage: null);
|
||||||
|
|
||||||
final clientAsync = _ref.read(matrixClientProvider);
|
final client = _ref.read(matrixClientProvider).valueOrNull;
|
||||||
final client = clientAsync.valueOrNull;
|
|
||||||
if (client == null) {
|
if (client == null) {
|
||||||
state = state.copyWith(
|
state = state.copyWith(
|
||||||
status: LoginStatus.error,
|
status: LoginStatus.error,
|
||||||
@@ -39,20 +52,21 @@ class LoginNotifier extends StateNotifier<LoginState> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (client.isLogged()) {
|
||||||
|
state = state.copyWith(status: LoginStatus.idle);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Auto-discover homeserver from MXID
|
final serverUri = Uri.https(
|
||||||
if (userId.isValidMatrixId) {
|
homeserver.replaceAll(RegExp(r'^https?://'), ''),
|
||||||
final domain = Uri.https(userId.domain!, '');
|
'',
|
||||||
try {
|
);
|
||||||
await client.checkHomeserver(domain);
|
await client.checkHomeserver(serverUri);
|
||||||
} catch (_) {
|
|
||||||
client.homeserver = domain;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await client.login(
|
await client.login(
|
||||||
LoginType.mLoginPassword,
|
LoginType.mLoginPassword,
|
||||||
identifier: AuthenticationUserIdentifier(user: userId),
|
identifier: AuthenticationUserIdentifier(user: username),
|
||||||
password: password,
|
password: password,
|
||||||
initialDeviceDisplayName: deviceName ?? 'Pyramid',
|
initialDeviceDisplayName: deviceName ?? 'Pyramid',
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -12,22 +12,26 @@ class LoginPage extends ConsumerStatefulWidget {
|
|||||||
|
|
||||||
class _LoginPageState extends ConsumerState<LoginPage> {
|
class _LoginPageState extends ConsumerState<LoginPage> {
|
||||||
final _formKey = GlobalKey<FormState>();
|
final _formKey = GlobalKey<FormState>();
|
||||||
final _userIdCtrl = TextEditingController();
|
final _userCtrl = TextEditingController();
|
||||||
final _passwordCtrl = TextEditingController();
|
final _passCtrl = TextEditingController();
|
||||||
bool _obscurePassword = true;
|
bool _obscure = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_userIdCtrl.dispose();
|
_userCtrl.dispose();
|
||||||
_passwordCtrl.dispose();
|
_passCtrl.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _submit() async {
|
Future<void> _submit() async {
|
||||||
if (!_formKey.currentState!.validate()) return;
|
if (!_formKey.currentState!.validate()) return;
|
||||||
|
|
||||||
|
final homeserver = ref.read(homeserverProvider).valueOrNull ?? 'matrix.org';
|
||||||
|
|
||||||
await ref.read(loginNotifierProvider.notifier).login(
|
await ref.read(loginNotifierProvider.notifier).login(
|
||||||
userId: _userIdCtrl.text.trim(),
|
username: _userCtrl.text.trim(),
|
||||||
password: _passwordCtrl.text,
|
password: _passCtrl.text,
|
||||||
|
homeserver: homeserver,
|
||||||
);
|
);
|
||||||
|
|
||||||
final state = ref.read(loginNotifierProvider);
|
final state = ref.read(loginNotifierProvider);
|
||||||
@@ -38,8 +42,10 @@ class _LoginPageState extends ConsumerState<LoginPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final state = ref.watch(loginNotifierProvider);
|
final theme = Theme.of(context);
|
||||||
final loading = state.status == LoginStatus.loading;
|
final loginState = ref.watch(loginNotifierProvider);
|
||||||
|
final loading = loginState.status == LoginStatus.loading;
|
||||||
|
final homeserver = ref.watch(homeserverProvider).valueOrNull ?? 'matrix.org';
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: Center(
|
body: Center(
|
||||||
@@ -53,43 +59,73 @@ class _LoginPageState extends ConsumerState<LoginPage> {
|
|||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
const _PyramidLogo(),
|
Icon(
|
||||||
|
Icons.change_history_rounded,
|
||||||
|
size: 72,
|
||||||
|
color: theme.colorScheme.primary,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'Pyramid',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: theme.textTheme.headlineMedium
|
||||||
|
?.copyWith(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 4),
|
||||||
|
// Server-Anzeige mit Wechsel-Link
|
||||||
|
Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Icon(Icons.dns_outlined,
|
||||||
|
size: 14,
|
||||||
|
color: theme.colorScheme.onSurfaceVariant),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
Text(
|
||||||
|
homeserver,
|
||||||
|
style: theme.textTheme.bodySmall?.copyWith(
|
||||||
|
color: theme.colorScheme.onSurfaceVariant,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 4),
|
||||||
|
GestureDetector(
|
||||||
|
onTap: () => context.go('/server'),
|
||||||
|
child: Text(
|
||||||
|
'ändern',
|
||||||
|
style: theme.textTheme.bodySmall?.copyWith(
|
||||||
|
color: theme.colorScheme.primary,
|
||||||
|
decoration: TextDecoration.underline,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
const SizedBox(height: 32),
|
const SizedBox(height: 32),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _userIdCtrl,
|
controller: _userCtrl,
|
||||||
decoration: const InputDecoration(
|
decoration: const InputDecoration(
|
||||||
labelText: 'Matrix ID',
|
labelText: 'Nutzername',
|
||||||
hintText: '@name:server.de',
|
hintText: 'dein_name',
|
||||||
prefixIcon: Icon(Icons.person_outline),
|
prefixIcon: Icon(Icons.person_outline),
|
||||||
border: OutlineInputBorder(),
|
border: OutlineInputBorder(),
|
||||||
),
|
),
|
||||||
keyboardType: TextInputType.emailAddress,
|
|
||||||
autofillHints: const [AutofillHints.username],
|
autofillHints: const [AutofillHints.username],
|
||||||
validator: (v) {
|
validator: (v) => (v == null || v.trim().isEmpty)
|
||||||
if (v == null || v.trim().isEmpty) {
|
? 'Nutzername eingeben'
|
||||||
return 'Matrix ID eingeben';
|
: null,
|
||||||
}
|
|
||||||
if (!v.trim().startsWith('@') ||
|
|
||||||
!v.trim().contains(':')) {
|
|
||||||
return 'Format: @name:server.de';
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
TextFormField(
|
TextFormField(
|
||||||
controller: _passwordCtrl,
|
controller: _passCtrl,
|
||||||
obscureText: _obscurePassword,
|
obscureText: _obscure,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: 'Passwort',
|
labelText: 'Passwort',
|
||||||
prefixIcon: const Icon(Icons.lock_outline),
|
prefixIcon: const Icon(Icons.lock_outline),
|
||||||
border: const OutlineInputBorder(),
|
border: const OutlineInputBorder(),
|
||||||
suffixIcon: IconButton(
|
suffixIcon: IconButton(
|
||||||
icon: Icon(_obscurePassword
|
icon: Icon(_obscure
|
||||||
? Icons.visibility_outlined
|
? Icons.visibility_outlined
|
||||||
: Icons.visibility_off_outlined),
|
: Icons.visibility_off_outlined),
|
||||||
onPressed: () => setState(
|
onPressed: () => setState(() => _obscure = !_obscure),
|
||||||
() => _obscurePassword = !_obscurePassword),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
autofillHints: const [AutofillHints.password],
|
autofillHints: const [AutofillHints.password],
|
||||||
@@ -97,12 +133,11 @@ class _LoginPageState extends ConsumerState<LoginPage> {
|
|||||||
(v == null || v.isEmpty) ? 'Passwort eingeben' : null,
|
(v == null || v.isEmpty) ? 'Passwort eingeben' : null,
|
||||||
onFieldSubmitted: (_) => _submit(),
|
onFieldSubmitted: (_) => _submit(),
|
||||||
),
|
),
|
||||||
if (state.errorMessage != null) ...[
|
if (loginState.errorMessage != null) ...[
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
Text(
|
Text(
|
||||||
state.errorMessage!,
|
loginState.errorMessage!,
|
||||||
style: TextStyle(
|
style: TextStyle(color: theme.colorScheme.error),
|
||||||
color: Theme.of(context).colorScheme.error),
|
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -126,28 +161,3 @@ class _LoginPageState extends ConsumerState<LoginPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _PyramidLogo extends StatelessWidget {
|
|
||||||
const _PyramidLogo();
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Column(
|
|
||||||
children: [
|
|
||||||
Icon(
|
|
||||||
Icons.change_history_rounded,
|
|
||||||
size: 72,
|
|
||||||
color: Theme.of(context).colorScheme.primary,
|
|
||||||
),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Text(
|
|
||||||
'Pyramid',
|
|
||||||
style: Theme.of(context)
|
|
||||||
.textTheme
|
|
||||||
.headlineMedium
|
|
||||||
?.copyWith(fontWeight: FontWeight.bold),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,128 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
import 'package:pyramid/features/auth/login_notifier.dart';
|
||||||
|
|
||||||
|
class ServerPage extends ConsumerStatefulWidget {
|
||||||
|
const ServerPage({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
ConsumerState<ServerPage> createState() => _ServerPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ServerPageState extends ConsumerState<ServerPage> {
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
final _ctrl = TextEditingController(text: 'matrix.org');
|
||||||
|
bool _loading = false;
|
||||||
|
String? _error;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
ref.read(homeserverProvider.future).then((saved) {
|
||||||
|
if (saved != null && mounted) {
|
||||||
|
_ctrl.text = saved.replaceAll(RegExp(r'^https?://'), '');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_ctrl.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _continue() async {
|
||||||
|
if (!_formKey.currentState!.validate()) return;
|
||||||
|
setState(() { _loading = true; _error = null; });
|
||||||
|
|
||||||
|
final input = _ctrl.text.trim().replaceAll(RegExp(r'^https?://'), '');
|
||||||
|
try {
|
||||||
|
await saveHomeserver(input);
|
||||||
|
if (mounted) context.go('/login');
|
||||||
|
} catch (e) {
|
||||||
|
setState(() => _error = e.toString());
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _loading = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final theme = Theme.of(context);
|
||||||
|
|
||||||
|
return Scaffold(
|
||||||
|
body: Center(
|
||||||
|
child: ConstrainedBox(
|
||||||
|
constraints: const BoxConstraints(maxWidth: 400),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Form(
|
||||||
|
key: _formKey,
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Icon(
|
||||||
|
Icons.change_history_rounded,
|
||||||
|
size: 72,
|
||||||
|
color: theme.colorScheme.primary,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'Pyramid',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: theme.textTheme.headlineMedium
|
||||||
|
?.copyWith(fontWeight: FontWeight.bold),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
Text(
|
||||||
|
'Server',
|
||||||
|
style: theme.textTheme.titleMedium,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
TextFormField(
|
||||||
|
controller: _ctrl,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
hintText: 'matrix.org',
|
||||||
|
prefixText: 'https://',
|
||||||
|
prefixIcon: Icon(Icons.dns_outlined),
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
keyboardType: TextInputType.url,
|
||||||
|
validator: (v) {
|
||||||
|
if (v == null || v.trim().isEmpty) {
|
||||||
|
return 'Server-Adresse eingeben';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
onFieldSubmitted: (_) => _continue(),
|
||||||
|
),
|
||||||
|
if (_error != null) ...[
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Text(
|
||||||
|
_error!,
|
||||||
|
style: TextStyle(color: theme.colorScheme.error),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: _loading ? null : _continue,
|
||||||
|
child: _loading
|
||||||
|
? const SizedBox(
|
||||||
|
height: 20,
|
||||||
|
width: 20,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
)
|
||||||
|
: const Text('Weiter'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user