新增忘記密碼頁面/忘記密碼,傳送臨時密碼
This commit is contained in:
parent
351f280927
commit
b81dcf999f
217
lib/ForgetPasswordPage.dart
Normal file
217
lib/ForgetPasswordPage.dart
Normal file
|
|
@ -0,0 +1,217 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:mysql_client/mysql_client.dart';
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
import 'package:mailer/mailer.dart';
|
||||||
|
import 'package:mailer/smtp_server.dart';
|
||||||
|
import 'package:validators/validators.dart' as validator;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
runApp(MaterialApp(
|
||||||
|
home: ForgetPasswordPage(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
class ForgetPasswordPage extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
_ForgetPasswordPageState createState() => _ForgetPasswordPageState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ForgetPasswordPageState extends State<ForgetPasswordPage> {
|
||||||
|
late FocusNode _emailFocusNode;
|
||||||
|
final TextEditingController _emailController = TextEditingController();
|
||||||
|
|
||||||
|
bool _isButtonEnabled = true;
|
||||||
|
String _generatedCode = ''; // 用來存儲生成的臨時密碼
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_fetchData();
|
||||||
|
_emailFocusNode = FocusNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _fetchData() async {
|
||||||
|
final conn = await MySQLConnection.createConnection(
|
||||||
|
host: '203.64.84.154',
|
||||||
|
port: 33061,
|
||||||
|
userName: 'root',
|
||||||
|
password: 'Topic@2024',
|
||||||
|
databaseName: 'care',
|
||||||
|
);
|
||||||
|
await conn.connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
String _generateVerificationCode() {
|
||||||
|
final random = Random();
|
||||||
|
const availableChars = '0123456789';
|
||||||
|
return List.generate(
|
||||||
|
6, (index) => availableChars[random.nextInt(availableChars.length)])
|
||||||
|
.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _sendEmail(String recipientEmail, String code) async {
|
||||||
|
final smtpServer = SmtpServer(
|
||||||
|
'smtp.gmail.com',
|
||||||
|
port: 587,
|
||||||
|
username: 'kueikuei8011@gmail.com',
|
||||||
|
password: 'yqns onwf tydq obzl',
|
||||||
|
ssl: false,
|
||||||
|
allowInsecure: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
final message = Message()
|
||||||
|
..from = Address('kueikuei8011@gmail.com', 'Kuei')
|
||||||
|
..recipients.add(recipientEmail)
|
||||||
|
..subject = '您的臨時密碼'
|
||||||
|
..text = '您的臨時密碼是: $code';
|
||||||
|
|
||||||
|
try {
|
||||||
|
await send(message, smtpServer);
|
||||||
|
print('臨時密碼發送成功');
|
||||||
|
} catch (e) {
|
||||||
|
print('臨時密碼發送失敗: $e');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _sendVerificationCode() async {// 發送並更新資料庫中的密碼
|
||||||
|
final email = _emailController.text;
|
||||||
|
if (email.isEmpty) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('請輸入電子信箱地址')),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
} else if (!validator.isEmail(email)) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('請輸入正確的電子信箱')),
|
||||||
|
);
|
||||||
|
_emailFocusNode.requestFocus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//_generatedCode = _generateVerificationCode();
|
||||||
|
//_sendEmail(email, _generatedCode);
|
||||||
|
|
||||||
|
final conn = await MySQLConnection.createConnection(
|
||||||
|
host: '203.64.84.154',
|
||||||
|
port: 33061,
|
||||||
|
userName: 'root',
|
||||||
|
password: 'Topic@2024',
|
||||||
|
databaseName: 'care',
|
||||||
|
);
|
||||||
|
await conn.connect();
|
||||||
|
try {
|
||||||
|
var result = await conn.execute(
|
||||||
|
'SELECT * FROM HomeLogin WHERE homeEmail = :email',
|
||||||
|
{'email': email},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.rows.isEmpty) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('失敗')),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
_generatedCode = _generateVerificationCode();
|
||||||
|
await conn.execute(
|
||||||
|
'UPDATE HomeLogin SET homePassword = :tempPassword WHERE homeEmail = :email',
|
||||||
|
{'tempPassword': _generatedCode, 'email': email},
|
||||||
|
);
|
||||||
|
|
||||||
|
// 發送臨時密碼到用戶電子信箱
|
||||||
|
_sendEmail(email, _generatedCode);
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('臨時密碼已發送到您的電子信箱')),
|
||||||
|
);
|
||||||
|
|
||||||
|
_emailController.clear();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
print('資料庫錯誤: $e');
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('註冊失敗:系統錯誤')),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
await conn.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
super.dispose();
|
||||||
|
_emailFocusNode.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
body: SafeArea(
|
||||||
|
child: Center(
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 32.0),
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
Container(
|
||||||
|
margin: EdgeInsets.only(bottom: 40),
|
||||||
|
height: 100,
|
||||||
|
child: Icon(
|
||||||
|
Icons.account_circle,
|
||||||
|
size: 100,
|
||||||
|
color: Color(0xFF4FC3F7),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Text(
|
||||||
|
'全方位照護守護者',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 32,
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
color: Colors.black,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
TextField(
|
||||||
|
controller: _emailController,
|
||||||
|
focusNode: _emailFocusNode,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
border: OutlineInputBorder(),
|
||||||
|
prefixIcon: Icon(Icons.email_outlined),
|
||||||
|
labelText: '電子信箱',
|
||||||
|
),
|
||||||
|
keyboardType: TextInputType.emailAddress,
|
||||||
|
),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: _sendVerificationCode,
|
||||||
|
child: Text('送出'),
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
backgroundColor: Color(0xFF4FC3F7),
|
||||||
|
padding:
|
||||||
|
EdgeInsets.symmetric(horizontal: 50, vertical: 15),
|
||||||
|
textStyle: TextStyle(fontSize: 18),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(height: 10),
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
Navigator.pop(context);
|
||||||
|
},
|
||||||
|
child: Text('取消'),
|
||||||
|
style: TextButton.styleFrom(
|
||||||
|
backgroundColor: Colors.transparent,
|
||||||
|
textStyle: TextStyle(fontSize: 18),
|
||||||
|
shadowColor: Colors.transparent,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -470,11 +470,6 @@ class _AccountPageState extends State<AccountPage> {
|
||||||
subtitle: _isEditing
|
subtitle: _isEditing
|
||||||
? TextField(
|
? TextField(
|
||||||
controller: _emailController,
|
controller: _emailController,
|
||||||
onChanged: (value) {
|
|
||||||
setState(() {
|
|
||||||
widget.email = value;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
: Text(widget.email),
|
: Text(widget.email),
|
||||||
),
|
),
|
||||||
|
|
@ -495,11 +490,7 @@ class _AccountPageState extends State<AccountPage> {
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onChanged: (value) {
|
|
||||||
setState(() {
|
|
||||||
widget.password = value;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
: Text(widget.password),
|
: Text(widget.password),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import 'package:topic/HomePage.dart';
|
||||||
import 'package:mysql_client/mysql_client.dart';
|
import 'package:mysql_client/mysql_client.dart';
|
||||||
import 'package:topic/NoSwipeBackRoute.dart';
|
import 'package:topic/NoSwipeBackRoute.dart';
|
||||||
import 'package:topic/RegisterPage.dart';
|
import 'package:topic/RegisterPage.dart';
|
||||||
|
import 'package:topic/ForgetPasswordPage.dart';
|
||||||
import 'package:validators/validators.dart' as validator;
|
import 'package:validators/validators.dart' as validator;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -243,9 +244,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => HomePage(
|
builder: (context) => ForgetPasswordPage()),
|
||||||
email: _emailController.text,
|
|
||||||
)),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
child: Text('忘記密碼'),
|
child: Text('忘記密碼'),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user