35 lines
1.1 KiB
Dart
35 lines
1.1 KiB
Dart
|
|
// lib/services/identity_service.dart
|
||
|
|
|
||
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
|
import 'package:uuid/uuid.dart';
|
||
|
|
|
||
|
|
// 앱-고유 ID와 사용자 이름을 관리하는 서비스
|
||
|
|
class IdentityService {
|
||
|
|
static const String _userIdKey = 'app_user_id';
|
||
|
|
static const String _userNameKey = 'app_user_name';
|
||
|
|
|
||
|
|
// 1. 앱-고유 ID 가져오기 (없으면 생성)
|
||
|
|
Future<String> getOrCreateUserId() async {
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
String? userId = prefs.getString(_userIdKey);
|
||
|
|
|
||
|
|
if (userId == null) {
|
||
|
|
// ID가 없으면 V4 UUID 생성
|
||
|
|
userId = const Uuid().v4();
|
||
|
|
await prefs.setString(_userIdKey, userId);
|
||
|
|
}
|
||
|
|
return userId;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. 랭킹에 등록한 사용자 이름 가져오기
|
||
|
|
Future<String?> getSavedUserName() async {
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
return prefs.getString(_userNameKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. 랭킹 등록 성공 시, 사용자 이름 저장하기
|
||
|
|
Future<void> saveUserName(String name) async {
|
||
|
|
final prefs = await SharedPreferences.getInstance();
|
||
|
|
await prefs.setString(_userNameKey, name);
|
||
|
|
}
|
||
|
|
}
|