245 lines
9.3 KiB
Dart
245 lines
9.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import 'dart:math';
|
|
import '../blocs/auth/auth_bloc.dart';
|
|
import '../blocs/auth/auth_event.dart';
|
|
import '../blocs/auth/auth_state.dart';
|
|
import '../blocs/session/session_bloc.dart';
|
|
import '../blocs/session/session_event.dart';
|
|
import '../theme/app_theme.dart';
|
|
import '../theme/modern_glass_button.dart';
|
|
|
|
class LoginForm extends StatefulWidget {
|
|
const LoginForm({super.key});
|
|
|
|
@override
|
|
State<LoginForm> createState() => _LoginFormState();
|
|
}
|
|
|
|
class _LoginFormState extends State<LoginForm> {
|
|
final _usernameController = TextEditingController();
|
|
final _passwordController = TextEditingController();
|
|
bool _usePasskey = true;
|
|
|
|
@override
|
|
void dispose() {
|
|
_usernameController.dispose();
|
|
_passwordController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String _generateRandomHex(int bytes) {
|
|
final random = Random();
|
|
final values = List<int>.generate(bytes, (i) => random.nextInt(256));
|
|
return values.map((v) => v.toRadixString(16).padLeft(2, '0')).join();
|
|
}
|
|
|
|
Future<void> _handleAuthentication(
|
|
BuildContext context,
|
|
AuthenticationChallengeReceived state,
|
|
) async {
|
|
try {
|
|
// Simulate WebAuthn authentication by generating fake assertion data
|
|
// In a real implementation, this would call native WebAuthn APIs
|
|
final credentialId = state.credentialIds.isNotEmpty
|
|
? state.credentialIds.first
|
|
: _generateRandomHex(64);
|
|
|
|
if (context.mounted) {
|
|
context.read<AuthBloc>().add(
|
|
AuthenticationResponseSubmitted(
|
|
username: _usernameController.text,
|
|
challenge: state.challenge,
|
|
credentialId: credentialId,
|
|
authenticatorData: _generateRandomHex(37),
|
|
clientDataJSON:
|
|
'{"type":"webauthn.get","challenge":"${state.challenge}","origin":"https://b0esche.cloud"}',
|
|
signature: _generateRandomHex(128),
|
|
),
|
|
);
|
|
}
|
|
} catch (e) {
|
|
if (context.mounted) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text('Authentication failed: $e')));
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return BlocListener<AuthBloc, AuthState>(
|
|
listener: (context, state) {
|
|
if (state is AuthFailure) {
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(state.error)));
|
|
} else if (state is AuthenticationChallengeReceived) {
|
|
_handleAuthentication(context, state);
|
|
} else if (state is AuthAuthenticated) {
|
|
// Start session
|
|
context.read<SessionBloc>().add(SessionStarted(state.token));
|
|
context.go('/');
|
|
}
|
|
},
|
|
child: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
const Text(
|
|
'sign in',
|
|
style: TextStyle(fontSize: 24, color: AppTheme.primaryText),
|
|
),
|
|
const SizedBox(height: 24),
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
|
|
borderRadius: BorderRadius.circular(16),
|
|
border: Border.all(
|
|
color: AppTheme.accentColor.withValues(alpha: 0.3),
|
|
),
|
|
),
|
|
child: TextField(
|
|
controller: _usernameController,
|
|
textInputAction: TextInputAction.done,
|
|
keyboardType: TextInputType.text,
|
|
cursorColor: AppTheme.accentColor,
|
|
decoration: InputDecoration(
|
|
hintText: 'username',
|
|
hintStyle: TextStyle(color: AppTheme.secondaryText),
|
|
contentPadding: const EdgeInsets.all(12),
|
|
border: InputBorder.none,
|
|
prefixIcon: Icon(
|
|
Icons.person_outline,
|
|
color: AppTheme.primaryText,
|
|
size: 20,
|
|
),
|
|
),
|
|
style: const TextStyle(color: AppTheme.primaryText),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
if (!_usePasskey)
|
|
Container(
|
|
decoration: BoxDecoration(
|
|
color: AppTheme.primaryBackground.withValues(alpha: 0.5),
|
|
borderRadius: BorderRadius.circular(16),
|
|
border: Border.all(
|
|
color: AppTheme.accentColor.withValues(alpha: 0.3),
|
|
),
|
|
),
|
|
child: TextField(
|
|
controller: _passwordController,
|
|
textInputAction: TextInputAction.done,
|
|
keyboardType: TextInputType.visiblePassword,
|
|
obscureText: true,
|
|
cursorColor: AppTheme.accentColor,
|
|
decoration: InputDecoration(
|
|
hintText: 'password',
|
|
hintStyle: TextStyle(color: AppTheme.secondaryText),
|
|
contentPadding: const EdgeInsets.all(12),
|
|
border: InputBorder.none,
|
|
prefixIcon: Icon(
|
|
Icons.lock_outline,
|
|
color: AppTheme.primaryText,
|
|
size: 20,
|
|
),
|
|
),
|
|
style: const TextStyle(color: AppTheme.primaryText),
|
|
),
|
|
),
|
|
if (!_usePasskey) const SizedBox(height: 16),
|
|
SizedBox(
|
|
width: 150,
|
|
child: BlocBuilder<AuthBloc, AuthState>(
|
|
builder: (context, state) {
|
|
return ModernGlassButton(
|
|
isLoading: state is AuthLoading,
|
|
onPressed: () {
|
|
if (_usernameController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Username is required'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
if (!_usePasskey && _passwordController.text.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Password is required'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
if (_usePasskey) {
|
|
context.read<AuthBloc>().add(
|
|
LoginRequested(username: _usernameController.text),
|
|
);
|
|
} else {
|
|
context.read<AuthBloc>().add(
|
|
PasswordLoginRequested(
|
|
username: _usernameController.text,
|
|
password: _passwordController.text,
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('sign in'),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () => setState(() => _usePasskey = !_usePasskey),
|
|
child: Text(
|
|
_usePasskey ? 'use password' : 'use passkey',
|
|
style: TextStyle(
|
|
color: AppTheme.accentColor,
|
|
decoration: TextDecoration.underline,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text(
|
|
'don\'t have an account?',
|
|
style: TextStyle(color: AppTheme.secondaryText),
|
|
),
|
|
const SizedBox(width: 8),
|
|
GestureDetector(
|
|
onTap: () => context.go('/signup'),
|
|
child: Text(
|
|
'create one',
|
|
style: TextStyle(
|
|
color: AppTheme.accentColor,
|
|
decoration: TextDecoration.underline,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|