import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'dart:convert' show utf8; import 'package:crypto/crypto.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'id_finding_page.dart'; import 'pw_finding_page.dart'; import 'signup_page.dart'; class LoginPage extends StatefulWidget { const LoginPage({Key? key}) : super(key: key); @override _LoginPageState createState() => _LoginPageState(); } class _LoginPageState extends State { final TextEditingController idController = TextEditingController(); final TextEditingController passwordController = TextEditingController(); bool autoLogin = false; String loginErrorMessage = ''; Future _login() async { String id = idController.text; String password = passwordController.text; String autoLoginStatus = autoLogin ? 'Y' : 'N'; var bytes = utf8.encode(password); var digest = sha256.convert(bytes); try { final response = await http.post( Uri.parse('https://eldsoft.com:8097/user/login'), headers: { 'Content-Type': 'application/json', }, body: jsonEncode({ 'user_id': id, 'user_pw': digest.toString(), }), ).timeout(const Duration(seconds: 10)); String responseBody = utf8.decode(response.bodyBytes); if (response.statusCode == 200) { final Map jsonResponse = jsonDecode(responseBody); if (jsonResponse['result'] == 'OK') { print('로그인 성공'); SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.setString('auth_token', jsonResponse['auth']['token']); await prefs.setBool('auto_login', autoLogin); } else if (jsonResponse['response_info']['msg_title'] == '로그인 실패') { setState(() { loginErrorMessage = '회원정보를 다시 확인해주세요.'; }); } } else { _showDialog('오류', '로그인에 실패했습니다. 관리자에게 문의해주세요.'); } } catch (e) { _showDialog('오류', '요청이 실패했습니다. 관리자에게 문의해주세요.'); } } void _showDialog(String title, String content) { showDialog( context: context, builder: (BuildContext context) { return AlertDialog( backgroundColor: Colors.white, title: Text(title, style: const TextStyle(color: Colors.black)), content: Text(content, style: const TextStyle(color: Colors.black)), actions: [ Center( child: TextButton( style: TextButton.styleFrom( backgroundColor: Colors.black, foregroundColor: Colors.white, ), child: const Text('확인'), onPressed: () { Navigator.of(context).pop(); }, ), ), ], ); }, ); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: const Text('ALL SCORE', style: TextStyle(color: Colors.white)), backgroundColor: Colors.black, ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( '로그인', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black, ), ), const SizedBox(height: 32), TextField( controller: idController, decoration: InputDecoration( labelText: 'ID', labelStyle: const TextStyle(color: Colors.black), border: OutlineInputBorder(), focusedBorder: OutlineInputBorder( borderSide: const BorderSide(color: Colors.black, width: 2.0), ), ), ), const SizedBox(height: 16), TextField( controller: passwordController, obscureText: true, decoration: InputDecoration( labelText: 'PW', labelStyle: const TextStyle(color: Colors.black), border: OutlineInputBorder(), focusedBorder: OutlineInputBorder( borderSide: const BorderSide(color: Colors.black, width: 2.0), ), ), ), if (loginErrorMessage.isNotEmpty) Padding( padding: const EdgeInsets.only(top: 8.0), child: Text( loginErrorMessage, style: const TextStyle(color: Colors.red), ), ), Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Checkbox( value: autoLogin, onChanged: (bool? value) { setState(() { autoLogin = value ?? false; }); }, ), const Text('자동로그인', style: TextStyle(color: Colors.black)), ], ), const SizedBox(height: 16), ElevatedButton( onPressed: _login, style: ElevatedButton.styleFrom( backgroundColor: Colors.black, foregroundColor: Colors.white, ), child: const Text('로그인'), ), const SizedBox(height: 16), TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const IdFindingPage()), ); }, child: const Text('ID 찾기', style: TextStyle(color: Colors.black)), ), TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const PwFindingPage()), ); }, child: const Text('PW 찾기', style: TextStyle(color: Colors.black)), ), TextButton( onPressed: () { Navigator.push( context, MaterialPageRoute(builder: (context) => const SignUpPage()), ); }, child: const Text('회원가입', style: TextStyle(color: Colors.black)), ), ], ), ), ); } }