feat: profile and subs
This commit is contained in:
@@ -9,23 +9,30 @@ enum AuthStatus { initial, loading, otpSent, authenticated, error }
|
||||
class AuthState extends Equatable {
|
||||
final AuthStatus status;
|
||||
final String mobile;
|
||||
final bool needsProfile; // پس از ورود، آیا کاربر باید نام/آواتار انتخاب کند
|
||||
final String? error;
|
||||
|
||||
const AuthState({
|
||||
this.status = AuthStatus.initial,
|
||||
this.mobile = '',
|
||||
this.needsProfile = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
AuthState copyWith({AuthStatus? status, String? mobile, String? error}) =>
|
||||
AuthState copyWith(
|
||||
{AuthStatus? status,
|
||||
String? mobile,
|
||||
bool? needsProfile,
|
||||
String? error}) =>
|
||||
AuthState(
|
||||
status: status ?? this.status,
|
||||
mobile: mobile ?? this.mobile,
|
||||
needsProfile: needsProfile ?? this.needsProfile,
|
||||
error: error,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [status, mobile, error];
|
||||
List<Object?> get props => [status, mobile, needsProfile, error];
|
||||
}
|
||||
|
||||
class AuthCubit extends Cubit<AuthState> {
|
||||
@@ -45,13 +52,25 @@ class AuthCubit extends Cubit<AuthState> {
|
||||
Future<void> verifyOtp(String code) async {
|
||||
emit(state.copyWith(status: AuthStatus.loading));
|
||||
try {
|
||||
await _repo.verifyOtp(state.mobile, code);
|
||||
emit(state.copyWith(status: AuthStatus.authenticated));
|
||||
final hasName = await _repo.verifyOtp(state.mobile, code);
|
||||
emit(state.copyWith(
|
||||
status: AuthStatus.authenticated, needsProfile: !hasName));
|
||||
} catch (e) {
|
||||
emit(state.copyWith(status: AuthStatus.error, error: _msg(e)));
|
||||
}
|
||||
}
|
||||
|
||||
/// ذخیرهی نام و آواتار؛ سپس نیازی به صفحهی پروفایل نیست.
|
||||
Future<bool> saveProfile(String name, String avatar) async {
|
||||
try {
|
||||
await _repo.updateProfile(name, avatar);
|
||||
emit(state.copyWith(needsProfile: false));
|
||||
return true;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> logout() async {
|
||||
await _repo.logout();
|
||||
emit(const AuthState());
|
||||
|
||||
@@ -13,8 +13,9 @@ class AuthRepository {
|
||||
await _api.dio.post('/auth/login-otp', data: {'mobile': mobile});
|
||||
}
|
||||
|
||||
/// اعتبارسنجی کد و ذخیرهی توکن JWT.
|
||||
Future<void> verifyOtp(String mobile, String code) async {
|
||||
/// اعتبارسنجی کد، ذخیرهی توکن JWT و بازگرداندنِ اینکه کاربر نام دارد یا نه.
|
||||
/// اگر نام نداشته باشد، فرانت کاربر را به صفحهی انتخاب نام/آواتار میبرد.
|
||||
Future<bool> verifyOtp(String mobile, String code) async {
|
||||
final res = await _api.dio.post(
|
||||
'/auth/check-otp',
|
||||
data: {'mobile': mobile, 'token': code},
|
||||
@@ -24,6 +25,15 @@ class AuthRepository {
|
||||
throw Exception('no token in response');
|
||||
}
|
||||
await _storage.write(token);
|
||||
final user = res.data['user'];
|
||||
final name = (user is Map) ? user['first_name'] : null;
|
||||
return name is String && name.trim().isNotEmpty;
|
||||
}
|
||||
|
||||
/// تنظیم نام نمایشی و آواتار.
|
||||
Future<void> updateProfile(String firstName, String avatar) async {
|
||||
await _api.dio.post('/profile',
|
||||
data: {'first_name': firstName, 'avatar': avatar});
|
||||
}
|
||||
|
||||
Future<bool> isLoggedIn() async {
|
||||
|
||||
@@ -33,7 +33,7 @@ class _OtpScreenState extends State<OtpScreen> {
|
||||
child: BlocConsumer<AuthCubit, AuthState>(
|
||||
listener: (context, state) {
|
||||
if (state.status == AuthStatus.authenticated) {
|
||||
context.go('/lobby');
|
||||
context.go(state.needsProfile ? '/setup' : '/lobby');
|
||||
} else if (state.status == AuthStatus.error) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(state.error ?? 'خطا')),
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:random_avatar/random_avatar.dart';
|
||||
|
||||
import '../../core/theme/app_theme.dart';
|
||||
import '../../core/widgets/game_ui.dart';
|
||||
import 'auth_cubit.dart';
|
||||
|
||||
/// صفحهی انتخاب نام و آواتار پس از اولین ورود.
|
||||
/// آواتارها با پکیج random_avatar تولید میشوند (رایگان، بدون نیاز به asset).
|
||||
class ProfileSetupScreen extends StatefulWidget {
|
||||
const ProfileSetupScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ProfileSetupScreen> createState() => _ProfileSetupScreenState();
|
||||
}
|
||||
|
||||
class _ProfileSetupScreenState extends State<ProfileSetupScreen> {
|
||||
final _name = TextEditingController();
|
||||
bool _saving = false;
|
||||
|
||||
// مجموعهای از seedها؛ هر seed یک آواتارِ یکتا میسازد.
|
||||
late List<String> _seeds;
|
||||
int _selected = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_seeds = List.generate(12, (i) => 'hakem-${i + 1}');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_name.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
bool get _valid => _name.text.trim().length >= 2;
|
||||
|
||||
Future<void> _save() async {
|
||||
setState(() => _saving = true);
|
||||
final ok = await context
|
||||
.read<AuthCubit>()
|
||||
.saveProfile(_name.text.trim(), _seeds[_selected]);
|
||||
if (!mounted) return;
|
||||
setState(() => _saving = false);
|
||||
if (ok) {
|
||||
context.go('/lobby');
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('ذخیره نشد، دوباره تلاش کنید')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: GameBackground(
|
||||
child: Center(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const GlowText('انتخاب نام و آواتار', size: 26),
|
||||
const SizedBox(height: 20),
|
||||
GamePanel(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// پیشنمایشِ آواتارِ انتخابشده
|
||||
Container(
|
||||
padding: const EdgeInsets.all(6),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
border: Border.all(color: AppColors.gold, width: 2),
|
||||
),
|
||||
child: RandomAvatar(_seeds[_selected],
|
||||
height: 84, width: 84),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
TextField(
|
||||
controller: _name,
|
||||
textAlign: TextAlign.center,
|
||||
maxLength: 20,
|
||||
inputFormatters: [
|
||||
LengthLimitingTextInputFormatter(20),
|
||||
],
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'نام نمایشی (۲ تا ۲۰ حرف)',
|
||||
counterText: ''),
|
||||
onChanged: (_) => setState(() {}),
|
||||
),
|
||||
const SizedBox(height: 14),
|
||||
const Text('یک آواتار انتخاب کن',
|
||||
style: TextStyle(color: AppColors.gold)),
|
||||
const SizedBox(height: 10),
|
||||
GridView.count(
|
||||
crossAxisCount: 4,
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
mainAxisSpacing: 10,
|
||||
crossAxisSpacing: 10,
|
||||
children: [
|
||||
for (var i = 0; i < _seeds.length; i++)
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _selected = i),
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: AppColors.bgDark,
|
||||
border: Border.all(
|
||||
color: _selected == i
|
||||
? AppColors.gold
|
||||
: Colors.transparent,
|
||||
width: 2.5,
|
||||
),
|
||||
),
|
||||
child: RandomAvatar(_seeds[i]),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
GameButton(
|
||||
label: _saving ? 'در حال ذخیره…' : 'تأیید و ورود',
|
||||
width: double.infinity,
|
||||
colors: const [Color(0xFF3FA34D), Color(0xFF1B5E20)],
|
||||
onTap: (!_valid || _saving) ? null : _save,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user