-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It seems maybe webview isn’t needed? This only works for user/password for now, the whole thing is a mess, but it’s starting to work…
- Loading branch information
Showing
2 changed files
with
151 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:dio/dio.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class AuthFormWidget extends StatefulWidget { | ||
final Dio dio; | ||
final String apiBaseUrl; | ||
final Function onAuthSuccess; | ||
|
||
const AuthFormWidget({ | ||
Key? key, | ||
required this.dio, | ||
required this.apiBaseUrl, | ||
required this.onAuthSuccess, | ||
}) : super(key: key); | ||
|
||
@override | ||
_AuthFormWidgetState createState() => _AuthFormWidgetState(); | ||
} | ||
|
||
class _AuthFormWidgetState extends State<AuthFormWidget> { | ||
final _formKey = GlobalKey<FormState>(); | ||
final _emailController = TextEditingController(); | ||
final _passwordController = TextEditingController(); | ||
bool _isLoading = false; | ||
|
||
Future<void> _submitForm() async { | ||
if (_formKey.currentState!.validate()) { | ||
setState(() { | ||
_isLoading = true; | ||
}); | ||
|
||
try { | ||
final response = await widget.dio.post( | ||
'${widget.apiBaseUrl}/powapi/session', | ||
options: Options(headers: { | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json', | ||
}), | ||
data: { | ||
'user': { | ||
'email': _emailController.text, | ||
'password': _passwordController.text, | ||
}, | ||
}, | ||
); | ||
|
||
if (response.statusCode == 200) { | ||
final accessToken = response.data['data']['access_token']; | ||
final renewalToken = response.data['data']['renewal_token']; | ||
|
||
final prefs = await SharedPreferences.getInstance(); | ||
await prefs.setString('access_token', accessToken); | ||
await prefs.setString('renewal_token', renewalToken); | ||
|
||
widget.onAuthSuccess(); | ||
} else { | ||
// Handle error (e.g., show error message) | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar(content: Text('Authentication failed')), | ||
); | ||
} | ||
} catch (error) { | ||
print('Error during authentication: $error'); | ||
ScaffoldMessenger.of(context).showSnackBar( | ||
const SnackBar(content: Text('An error occurred')), | ||
); | ||
} finally { | ||
setState(() { | ||
_isLoading = false; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Form( | ||
key: _formKey, | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
TextFormField( | ||
controller: _emailController, | ||
decoration: const InputDecoration(labelText: 'Email'), | ||
keyboardType: TextInputType.emailAddress, | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return 'Please enter your email'; | ||
} | ||
return null; | ||
}, | ||
), | ||
TextFormField( | ||
controller: _passwordController, | ||
decoration: const InputDecoration(labelText: 'Password'), | ||
obscureText: true, | ||
validator: (value) { | ||
if (value == null || value.isEmpty) { | ||
return 'Please enter your password'; | ||
} | ||
return null; | ||
}, | ||
), | ||
const SizedBox(height: 20), | ||
ElevatedButton( | ||
onPressed: _isLoading ? null : _submitForm, | ||
child: _isLoading | ||
? const CircularProgressIndicator() | ||
: const Text('Log in'), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
_emailController.dispose(); | ||
_passwordController.dispose(); | ||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters