Password reset

This commit is contained in:
LittleSheep 2024-06-30 18:03:02 +08:00
parent 9d54b04f77
commit f1abdad54d
3 changed files with 75 additions and 12 deletions

View File

@ -12,6 +12,23 @@ extension SolianExtenions on BuildContext {
ScaffoldMessenger.of(this).clearSnackBars(); ScaffoldMessenger.of(this).clearSnackBars();
} }
Future<void> showModalDialog(String title, desc) {
return showDialog<void>(
useRootNavigator: true,
context: this,
builder: (ctx) => AlertDialog(
title: Text(title),
content: Text(desc),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: Text('okay'.tr),
)
],
),
);
}
Future<void> showErrorDialog(dynamic exception) { Future<void> showErrorDialog(dynamic exception) {
return showDialog<void>( return showDialog<void>(
useRootNavigator: true, useRootNavigator: true,

View File

@ -19,6 +19,36 @@ class _SignInPopupState extends State<SignInPopup> {
final _usernameController = TextEditingController(); final _usernameController = TextEditingController();
final _passwordController = TextEditingController(); final _passwordController = TextEditingController();
void requestResetPassword(BuildContext context) async {
final username = _usernameController.value.text;
if (username.isEmpty) {
context.showErrorDialog('signinResetPasswordHint'.tr);
return;
}
setState(() => _isBusy = true);
final client = ServiceFinder.configureClient('passport');
final lookupResp = await client.get('/api/users/lookup?probe=$username');
if (lookupResp.statusCode != 200) {
context.showErrorDialog(lookupResp.bodyString);
setState(() => _isBusy = false);
return;
}
final resp = await client.post('/api/users/me/password-reset', {
'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);
}
void performAction(BuildContext context) async { void performAction(BuildContext context) async {
final AuthProvider provider = Get.find(); final AuthProvider provider = Get.find();
@ -117,9 +147,16 @@ class _SignInPopupState extends State<SignInPopup> {
onSubmitted: (_) => performAction(context), onSubmitted: (_) => performAction(context),
), ),
const SizedBox(height: 12), const SizedBox(height: 12),
Align( Row(
alignment: Alignment.centerRight, mainAxisAlignment: MainAxisAlignment.spaceBetween,
child: TextButton( children: [
TextButton(
onPressed:
_isBusy ? null : () => requestResetPassword(context),
style: TextButton.styleFrom(foregroundColor: Colors.grey),
child: Text('forgotPassword'.tr),
),
TextButton(
onPressed: _isBusy ? null : () => performAction(context), onPressed: _isBusy ? null : () => performAction(context),
child: Row( child: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
@ -129,7 +166,8 @@ class _SignInPopupState extends State<SignInPopup> {
], ],
), ),
), ),
) ],
),
], ],
), ),
), ),

View File

@ -4,6 +4,7 @@ class SolianMessages extends Translations {
@override @override
Map<String, Map<String, String>> get keys => { Map<String, Map<String, String>> get keys => {
'en_US': { 'en_US': {
'done': 'Done',
'hide': 'Hide', 'hide': 'Hide',
'okay': 'Okay', 'okay': 'Okay',
'next': 'Next', 'next': 'Next',
@ -25,6 +26,7 @@ class SolianMessages extends Translations {
'openInBrowser': 'Open in browser', 'openInBrowser': 'Open in browser',
'notification': 'Notification', 'notification': 'Notification',
'errorHappened': 'An error occurred', 'errorHappened': 'An error occurred',
'forgotPassword': 'Forgot password',
'email': 'Email', 'email': 'Email',
'username': 'Username', 'username': 'Username',
'nickname': 'Nickname', 'nickname': 'Nickname',
@ -56,6 +58,8 @@ class SolianMessages extends Translations {
'Sign in to create post, start a realm, message your friend and more!', 'Sign in to create post, start a realm, message your friend and more!',
'signinRiskDetected': 'signinRiskDetected':
'Risk detected, click Next to open a webpage and signin through it to pass security check.', 'Risk detected, click Next to open a webpage and signin through it to pass security check.',
'signinResetPasswordHint': 'Please enter username to request reset password.',
'signinResetPasswordSent': 'Reset password request sent, check your inbox!',
'signup': 'Sign up', 'signup': 'Sign up',
'signupGreeting': 'Welcome onboard', 'signupGreeting': 'Welcome onboard',
'signupCaption': 'signupCaption':
@ -241,6 +245,7 @@ class SolianMessages extends Translations {
'accountStatusPositive': 'Positive', 'accountStatusPositive': 'Positive',
}, },
'zh_CN': { 'zh_CN': {
'done': '完成',
'hide': '隐藏', 'hide': '隐藏',
'okay': '确认', 'okay': '确认',
'next': '下一步', 'next': '下一步',
@ -262,6 +267,7 @@ class SolianMessages extends Translations {
'openInBrowser': '在浏览器中打开', 'openInBrowser': '在浏览器中打开',
'notification': '通知', 'notification': '通知',
'errorHappened': '发生错误了', 'errorHappened': '发生错误了',
'forgotPassword': '忘记密码',
'email': '邮件地址', 'email': '邮件地址',
'username': '用户名', 'username': '用户名',
'nickname': '显示名', 'nickname': '显示名',
@ -289,6 +295,8 @@ class SolianMessages extends Translations {
'signinGreeting': '欢迎回来\nSolar Network', 'signinGreeting': '欢迎回来\nSolar Network',
'signinCaption': '登录以发表帖子、文章、创建领域、和你的朋友聊天,以及获取更多功能!', 'signinCaption': '登录以发表帖子、文章、创建领域、和你的朋友聊天,以及获取更多功能!',
'signinRiskDetected': '检测到风险,点击下一步按钮来打开一个网页,并通过在其上面登录来通过安全检查。', 'signinRiskDetected': '检测到风险,点击下一步按钮来打开一个网页,并通过在其上面登录来通过安全检查。',
'signinResetPasswordHint': '请先填写用户名以发送重置密码请求。',
'signinResetPasswordSent': '重置密码请求已发送,在绑定邮件收件箱可收取一份包含重置密码链接的邮件。',
'signup': '注册', 'signup': '注册',
'signupGreeting': '欢迎加入\nSolar Network', 'signupGreeting': '欢迎加入\nSolar Network',
'signupCaption': '在 Solarpass 注册一个账号以获得整个 Solar Network 的存取权!', 'signupCaption': '在 Solarpass 注册一个账号以获得整个 Solar Network 的存取权!',