feat: Matrix login flow with auth guard and client provider
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||||
|
|
||||||
|
final matrixClientProvider = FutureProvider<Client>((ref) async {
|
||||||
|
if (!kIsWeb && (Platform.isWindows || Platform.isLinux)) {
|
||||||
|
sqfliteFfiInit();
|
||||||
|
databaseFactory = databaseFactoryFfi;
|
||||||
|
}
|
||||||
|
|
||||||
|
final appDir = await getApplicationSupportDirectory();
|
||||||
|
final dbPath = p.join(appDir.path, 'pyramid.sqlite');
|
||||||
|
|
||||||
|
final db = await databaseFactoryFfi.openDatabase(dbPath);
|
||||||
|
final sdkDb = await MatrixSdkDatabase.init('pyramid', database: db);
|
||||||
|
|
||||||
|
final client = Client('Pyramid', database: sdkDb);
|
||||||
|
await client.init();
|
||||||
|
return client;
|
||||||
|
});
|
||||||
|
|
||||||
|
final secureStorageProvider = Provider<FlutterSecureStorage>((ref) {
|
||||||
|
return const FlutterSecureStorage();
|
||||||
|
});
|
||||||
+16
-2
@@ -1,14 +1,28 @@
|
|||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:pyramid/layout/shell_page.dart';
|
import 'package:pyramid/features/auth/login_notifier.dart';
|
||||||
import 'package:pyramid/features/rooms/rooms_page.dart';
|
import 'package:pyramid/features/auth/login_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/settings/settings_page.dart';
|
import 'package:pyramid/features/settings/settings_page.dart';
|
||||||
|
import 'package:pyramid/layout/shell_page.dart';
|
||||||
|
|
||||||
final routerProvider = Provider<GoRouter>((ref) {
|
final routerProvider = Provider<GoRouter>((ref) {
|
||||||
|
final isLoggedIn = ref.watch(isLoggedInProvider);
|
||||||
|
|
||||||
return GoRouter(
|
return GoRouter(
|
||||||
initialLocation: '/rooms',
|
initialLocation: '/rooms',
|
||||||
|
redirect: (context, state) {
|
||||||
|
final loggingIn = state.matchedLocation == '/login';
|
||||||
|
if (!isLoggedIn && !loggingIn) return '/login';
|
||||||
|
if (isLoggedIn && loggingIn) return '/rooms';
|
||||||
|
return null;
|
||||||
|
},
|
||||||
routes: [
|
routes: [
|
||||||
|
GoRoute(
|
||||||
|
path: '/login',
|
||||||
|
builder: (context, state) => const LoginPage(),
|
||||||
|
),
|
||||||
ShellRoute(
|
ShellRoute(
|
||||||
builder: (context, state, child) => ShellPage(child: child),
|
builder: (context, state, child) => ShellPage(child: child),
|
||||||
routes: [
|
routes: [
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:pyramid/core/matrix_client.dart';
|
||||||
|
|
||||||
|
enum LoginStatus { idle, loading, error }
|
||||||
|
|
||||||
|
class LoginState {
|
||||||
|
final LoginStatus status;
|
||||||
|
final String? errorMessage;
|
||||||
|
|
||||||
|
const LoginState({this.status = LoginStatus.idle, this.errorMessage});
|
||||||
|
|
||||||
|
LoginState copyWith({LoginStatus? status, String? errorMessage}) =>
|
||||||
|
LoginState(
|
||||||
|
status: status ?? this.status,
|
||||||
|
errorMessage: errorMessage,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoginNotifier extends StateNotifier<LoginState> {
|
||||||
|
final Ref _ref;
|
||||||
|
|
||||||
|
LoginNotifier(this._ref) : super(const LoginState());
|
||||||
|
|
||||||
|
Future<void> login({
|
||||||
|
required String userId,
|
||||||
|
required String password,
|
||||||
|
String? deviceName,
|
||||||
|
}) async {
|
||||||
|
state = state.copyWith(status: LoginStatus.loading, errorMessage: null);
|
||||||
|
|
||||||
|
final clientAsync = _ref.read(matrixClientProvider);
|
||||||
|
final client = clientAsync.valueOrNull;
|
||||||
|
if (client == null) {
|
||||||
|
state = state.copyWith(
|
||||||
|
status: LoginStatus.error,
|
||||||
|
errorMessage: 'Client nicht initialisiert',
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Auto-discover homeserver from MXID
|
||||||
|
if (userId.isValidMatrixId) {
|
||||||
|
final domain = Uri.https(userId.domain!, '');
|
||||||
|
try {
|
||||||
|
await client.checkHomeserver(domain);
|
||||||
|
} catch (_) {
|
||||||
|
client.homeserver = domain;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await client.login(
|
||||||
|
LoginType.mLoginPassword,
|
||||||
|
identifier: AuthenticationUserIdentifier(user: userId),
|
||||||
|
password: password,
|
||||||
|
initialDeviceDisplayName: deviceName ?? 'Pyramid',
|
||||||
|
);
|
||||||
|
|
||||||
|
state = state.copyWith(status: LoginStatus.idle);
|
||||||
|
} on MatrixException catch (e) {
|
||||||
|
state = state.copyWith(
|
||||||
|
status: LoginStatus.error,
|
||||||
|
errorMessage: e.errorMessage,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
state = state.copyWith(
|
||||||
|
status: LoginStatus.error,
|
||||||
|
errorMessage: e.toString(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final loginNotifierProvider =
|
||||||
|
StateNotifierProvider<LoginNotifier, LoginState>((ref) {
|
||||||
|
return LoginNotifier(ref);
|
||||||
|
});
|
||||||
|
|
||||||
|
final isLoggedInProvider = Provider<bool>((ref) {
|
||||||
|
final client = ref.watch(matrixClientProvider).valueOrNull;
|
||||||
|
return client?.isLogged() ?? false;
|
||||||
|
});
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
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 LoginPage extends ConsumerStatefulWidget {
|
||||||
|
const LoginPage({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
ConsumerState<LoginPage> createState() => _LoginPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _LoginPageState extends ConsumerState<LoginPage> {
|
||||||
|
final _formKey = GlobalKey<FormState>();
|
||||||
|
final _userIdCtrl = TextEditingController();
|
||||||
|
final _passwordCtrl = TextEditingController();
|
||||||
|
bool _obscurePassword = true;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_userIdCtrl.dispose();
|
||||||
|
_passwordCtrl.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _submit() async {
|
||||||
|
if (!_formKey.currentState!.validate()) return;
|
||||||
|
await ref.read(loginNotifierProvider.notifier).login(
|
||||||
|
userId: _userIdCtrl.text.trim(),
|
||||||
|
password: _passwordCtrl.text,
|
||||||
|
);
|
||||||
|
|
||||||
|
final state = ref.read(loginNotifierProvider);
|
||||||
|
if (state.status == LoginStatus.idle && mounted) {
|
||||||
|
context.go('/rooms');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final state = ref.watch(loginNotifierProvider);
|
||||||
|
final loading = state.status == LoginStatus.loading;
|
||||||
|
|
||||||
|
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: [
|
||||||
|
const _PyramidLogo(),
|
||||||
|
const SizedBox(height: 32),
|
||||||
|
TextFormField(
|
||||||
|
controller: _userIdCtrl,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Matrix ID',
|
||||||
|
hintText: '@name:server.de',
|
||||||
|
prefixIcon: Icon(Icons.person_outline),
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
),
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
autofillHints: const [AutofillHints.username],
|
||||||
|
validator: (v) {
|
||||||
|
if (v == null || v.trim().isEmpty) {
|
||||||
|
return 'Matrix ID eingeben';
|
||||||
|
}
|
||||||
|
if (!v.trim().startsWith('@') ||
|
||||||
|
!v.trim().contains(':')) {
|
||||||
|
return 'Format: @name:server.de';
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextFormField(
|
||||||
|
controller: _passwordCtrl,
|
||||||
|
obscureText: _obscurePassword,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
labelText: 'Passwort',
|
||||||
|
prefixIcon: const Icon(Icons.lock_outline),
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
|
suffixIcon: IconButton(
|
||||||
|
icon: Icon(_obscurePassword
|
||||||
|
? Icons.visibility_outlined
|
||||||
|
: Icons.visibility_off_outlined),
|
||||||
|
onPressed: () => setState(
|
||||||
|
() => _obscurePassword = !_obscurePassword),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
autofillHints: const [AutofillHints.password],
|
||||||
|
validator: (v) =>
|
||||||
|
(v == null || v.isEmpty) ? 'Passwort eingeben' : null,
|
||||||
|
onFieldSubmitted: (_) => _submit(),
|
||||||
|
),
|
||||||
|
if (state.errorMessage != null) ...[
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Text(
|
||||||
|
state.errorMessage!,
|
||||||
|
style: TextStyle(
|
||||||
|
color: Theme.of(context).colorScheme.error),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
const SizedBox(height: 24),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: loading ? null : _submit,
|
||||||
|
child: loading
|
||||||
|
? const SizedBox(
|
||||||
|
height: 20,
|
||||||
|
width: 20,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
)
|
||||||
|
: const Text('Anmelden'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
-1
@@ -1,8 +1,32 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:pyramid/core/app.dart';
|
import 'package:pyramid/core/app.dart';
|
||||||
|
import 'package:pyramid/core/matrix_client.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
runApp(const ProviderScope(child: PyramidApp()));
|
runApp(const ProviderScope(child: PyramidBootstrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
class PyramidBootstrap extends ConsumerWidget {
|
||||||
|
const PyramidBootstrap({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final clientAsync = ref.watch(matrixClientProvider);
|
||||||
|
|
||||||
|
return clientAsync.when(
|
||||||
|
loading: () => const MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: Center(child: CircularProgressIndicator()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
error: (e, _) => MaterialApp(
|
||||||
|
home: Scaffold(
|
||||||
|
body: Center(child: Text('Fehler: $e')),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
data: (_) => const PyramidApp(),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user