Files
school-attendance/lib/screens/teacher_register_screen.dart
T

153 lines
5.2 KiB
Dart

// 👨‍🏫 교사 회원가입 화면. 교사 인증 코드 확인 후 신규 교사 계정을 생성한다.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import '../config.dart';
class TeacherRegisterScreen extends StatefulWidget {
const TeacherRegisterScreen({super.key});
@override
State<TeacherRegisterScreen> createState() => _TeacherRegisterScreenState();
}
class _TeacherRegisterScreenState extends State<TeacherRegisterScreen> {
final _idController = TextEditingController();
final _pwController = TextEditingController();
final _nameController = TextEditingController();
final _secretController = TextEditingController();
bool _isLoading = false;
Future<void> _registerTeacher() async {
String id = _idController.text.trim();
String pw = _pwController.text.trim();
String name = _nameController.text.trim();
String secret = _secretController.text.trim();
// 💡 테스트용 고유값 (실제 디바이스 UUID 연동 로직이 있다면 그걸 넣으세요)
String dummyUuid = "TEACHER_PHONE_$id";
if (id.isEmpty || pw.isEmpty || name.isEmpty || secret.isEmpty) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('⚠️ 모든 빈칸을 입력해 주세요.')));
return;
}
setState(() => _isLoading = true);
try {
final response = await http.post(
Uri.parse('$baseUrl/api/users/register-teacher'),
headers: {"Content-Type": "application/json"},
body: jsonEncode({
"teacherId": id,
"password": pw,
"name": name,
"secretCode": secret,
"deviceUuid": dummyUuid,
}),
);
final res = jsonDecode(response.body);
if (response.statusCode == 200 && res['status'] == 'success') {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('✅ ${res['message']}')));
Navigator.pop(context); // 가입 성공 시 로그인 화면으로 복귀
} else {
// 백엔드에서 보낸 에러 메시지(detail 혹은 message) 출력
String errorMsg = res['detail'] ?? res['message'] ?? '회원가입에 실패했습니다.';
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('❌ $errorMsg')));
}
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(const SnackBar(content: Text('❌ 서버와 통신에 실패했습니다.')));
} finally {
setState(() => _isLoading = false);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('교사 회원가입'),
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'👨‍🏫 교직원 전용 인증',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 4),
const Text(
'학교에서 발급한 교사 가입 비밀코드가 필요합니다.',
style: TextStyle(color: Colors.grey),
),
const SizedBox(height: 24),
TextField(
controller: _secretController,
obscureText: true,
decoration: const InputDecoration(
labelText: '🔑 교사 인증 비밀코드 입력',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 16),
const Divider(),
const SizedBox(height: 16),
TextField(
controller: _idController,
decoration: const InputDecoration(
labelText: '교직원 번호 (ID로 사용)',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
controller: _nameController,
decoration: const InputDecoration(
labelText: '선생님 성함',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
TextField(
controller: _pwController,
obscureText: true,
decoration: const InputDecoration(
labelText: '사용할 비밀번호 입력',
border: OutlineInputBorder(),
),
),
const SizedBox(height: 24),
SizedBox(
width: double.infinity,
height: 50,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.indigo,
foregroundColor: Colors.white,
),
onPressed: _isLoading ? null : _registerTeacher,
child: _isLoading
? const CircularProgressIndicator(color: Colors.white)
: const Text('교사 계정 생성하기'),
),
),
],
),
),
);
}
}