Solian/lib/screens/auth/signin.dart

205 lines
6.4 KiB
Dart
Raw Normal View History

2024-05-18 16:56:32 +00:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
2024-07-06 12:55:53 +00:00
import 'package:protocol_handler/protocol_handler.dart';
2024-05-19 16:08:20 +00:00
import 'package:solian/exts.dart';
import 'package:solian/providers/websocket.dart';
2024-05-18 16:56:32 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
2024-07-06 12:55:53 +00:00
import 'package:url_launcher/url_launcher.dart';
2024-05-18 16:56:32 +00:00
import 'package:url_launcher/url_launcher_string.dart';
class SignInPopup extends StatefulWidget {
const SignInPopup({super.key});
2024-05-18 16:56:32 +00:00
@override
State<SignInPopup> createState() => _SignInPopupState();
2024-05-18 16:56:32 +00:00
}
2024-07-06 12:55:53 +00:00
class _SignInPopupState extends State<SignInPopup> with ProtocolListener {
bool _isBusy = false;
2024-05-18 16:56:32 +00:00
final _usernameController = TextEditingController();
final _passwordController = TextEditingController();
2024-07-06 12:55:53 +00:00
void requestResetPassword() async {
2024-06-30 10:03:02 +00:00
final username = _usernameController.value.text;
if (username.isEmpty) {
context.showErrorDialog('signinResetPasswordHint'.tr);
return;
}
setState(() => _isBusy = true);
final client = ServiceFinder.configureClient('auth');
final lookupResp = await client.get('/users/lookup?probe=$username');
2024-06-30 10:03:02 +00:00
if (lookupResp.statusCode != 200) {
context.showErrorDialog(lookupResp.bodyString);
setState(() => _isBusy = false);
return;
}
final resp = await client.post('/users/me/password-reset', {
2024-06-30 10:03:02 +00:00
'user_id': lookupResp.body['id'],
});
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
setState(() => _isBusy = false);
return;
}
setState(() => _isBusy = false);
context.showModalDialog('done'.tr, 'signinResetPasswordSent'.tr);
}
2024-07-06 12:55:53 +00:00
void performAction() async {
2024-07-26 09:35:54 +00:00
final AuthProvider auth = Get.find();
2024-05-18 16:56:32 +00:00
final username = _usernameController.value.text;
final password = _passwordController.value.text;
if (username.isEmpty || password.isEmpty) return;
setState(() => _isBusy = true);
try {
2024-07-26 09:35:54 +00:00
await auth.signin(context, username, password);
await Future.delayed(const Duration(milliseconds: 250), () async {
await auth.refreshAuthorizeStatus();
await auth.refreshUserProfile();
});
} on RiskyAuthenticateException catch (e) {
showDialog(
2024-06-06 16:00:28 +00:00
context: context,
builder: (context) {
return AlertDialog(
title: Text('riskDetection'.tr),
content: Text('signinRiskDetected'.tr),
actions: [
TextButton(
child: Text('next'.tr),
onPressed: () {
const redirect = 'solink://auth?status=done';
launchUrlString(
ServiceFinder.buildUrl('passport',
'/mfa?redirect_uri=$redirect&ticketId=${e.ticketId}'),
mode: LaunchMode.inAppWebView,
);
Navigator.pop(context);
},
)
],
);
},
2024-06-06 16:00:28 +00:00
);
return;
} catch (e) {
context.showErrorDialog(e);
return;
} finally {
setState(() => _isBusy = false);
}
2024-06-06 16:00:28 +00:00
Get.find<WebSocketProvider>().registerPushNotifications();
Navigator.pop(context, true);
2024-05-18 16:56:32 +00:00
}
2024-07-06 12:55:53 +00:00
@override
void initState() {
protocolHandler.addListener(this);
super.initState();
}
@override
void dispose() {
protocolHandler.removeListener(this);
super.dispose();
}
@override
void onProtocolUrlReceived(String url) {
final uri = url.replaceFirst('solink://', '');
if (uri == 'auth?status=done') {
closeInAppWebView();
performAction();
}
}
2024-05-18 16:56:32 +00:00
@override
Widget build(BuildContext context) {
return SizedBox(
height: MediaQuery.of(context).size.height * 0.9,
2024-05-18 16:56:32 +00:00
child: Center(
child: Container(
width: MediaQuery.of(context).size.width * 0.6,
constraints: const BoxConstraints(maxWidth: 360),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
2024-05-18 16:56:32 +00:00
children: [
Image.asset('assets/logo.png', width: 64, height: 64)
.paddingOnly(bottom: 4),
Text(
'signinGreeting'.tr,
style: const TextStyle(
fontSize: 28,
fontWeight: FontWeight.w900,
),
).paddingOnly(left: 4, bottom: 16),
2024-05-18 16:56:32 +00:00
TextField(
autocorrect: false,
enableSuggestions: false,
controller: _usernameController,
autofillHints: const [AutofillHints.username],
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'username'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-05-18 16:56:32 +00:00
),
const SizedBox(height: 12),
TextField(
obscureText: true,
autocorrect: false,
enableSuggestions: false,
autofillHints: const [AutofillHints.password],
controller: _passwordController,
decoration: InputDecoration(
isDense: true,
border: const OutlineInputBorder(),
labelText: 'password'.tr,
),
onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(),
2024-07-06 12:55:53 +00:00
onSubmitted: (_) => performAction(),
2024-05-18 16:56:32 +00:00
),
const SizedBox(height: 12),
2024-06-30 10:03:02 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton(
2024-07-06 12:55:53 +00:00
onPressed: _isBusy ? null : () => requestResetPassword(),
2024-06-30 10:03:02 +00:00
style: TextButton.styleFrom(foregroundColor: Colors.grey),
child: Text('forgotPassword'.tr),
),
2024-06-30 10:03:02 +00:00
TextButton(
2024-07-06 12:55:53 +00:00
onPressed: _isBusy ? null : () => performAction(),
2024-06-30 10:03:02 +00:00
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Text('next'.tr),
const Icon(Icons.chevron_right),
],
),
),
],
),
2024-05-18 16:56:32 +00:00
],
),
),
),
);
}
}