139 lines
4.3 KiB
Dart
139 lines
4.3 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import '../services/authentication_manager.dart';
|
|
import '../services/brandmeister_client.dart';
|
|
|
|
class AuthView extends StatefulWidget {
|
|
const AuthView({super.key});
|
|
|
|
@override
|
|
State<AuthView> createState() => _AuthViewState();
|
|
}
|
|
|
|
class _AuthViewState extends State<AuthView> {
|
|
final TextEditingController _tokenController = TextEditingController();
|
|
final FocusNode _focusNode = FocusNode();
|
|
bool _isLoading = false;
|
|
String? _errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_focusNode.requestFocus();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_tokenController.dispose();
|
|
_focusNode.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _verifyToken() async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
_errorMessage = null;
|
|
});
|
|
|
|
try {
|
|
final authManager = context.read<AuthenticationManager>();
|
|
await authManager.verifyAndSaveToken(_tokenController.text);
|
|
} on BrandmeisterError catch (e) {
|
|
setState(() {
|
|
_errorMessage = e.message;
|
|
_isLoading = false;
|
|
});
|
|
} catch (e) {
|
|
setState(() {
|
|
_errorMessage = 'Authentication failed: ${e.toString()}';
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
body: SafeArea(
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(24.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Icon(
|
|
Icons.radio,
|
|
size: 80,
|
|
color: Theme.of(context).colorScheme.primary,
|
|
),
|
|
const SizedBox(height: 24),
|
|
Text(
|
|
'Sign In',
|
|
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
'Enter your BrandMeister API token',
|
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
|
color: Colors.grey[600],
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
const SizedBox(height: 48),
|
|
TextField(
|
|
controller: _tokenController,
|
|
focusNode: _focusNode,
|
|
obscureText: true,
|
|
enabled: !_isLoading,
|
|
decoration: InputDecoration(
|
|
labelText: 'API Token',
|
|
hintText: 'Enter your BrandMeister API token',
|
|
border: const OutlineInputBorder(),
|
|
prefixIcon: const Icon(Icons.key),
|
|
errorText: _errorMessage,
|
|
),
|
|
onSubmitted: (_) {
|
|
if (_tokenController.text.isNotEmpty && !_isLoading) {
|
|
_verifyToken();
|
|
}
|
|
},
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: _tokenController.text.isEmpty || _isLoading
|
|
? null
|
|
: _verifyToken,
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(vertical: 16),
|
|
),
|
|
child: _isLoading
|
|
? const SizedBox(
|
|
height: 20,
|
|
width: 20,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Text('Sign In'),
|
|
),
|
|
const SizedBox(height: 24),
|
|
TextButton.icon(
|
|
onPressed: () {
|
|
// Open BrandMeister website
|
|
},
|
|
icon: const Icon(Icons.open_in_new),
|
|
label: const Text('Get API Token from BrandMeister'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|