telegram loging

This commit is contained in:
Dmitry 2025-11-28 00:58:56 +03:00
parent c678479137
commit 29e35c5244
2 changed files with 13 additions and 9 deletions

View file

@ -244,18 +244,18 @@ class AuthApiV2 {
// Use the existing telegram utils to get user ID
final telegramUtils = TelegramUtils();
final userId = await telegramUtils.getUserId(initData);
final userData = await telegramUtils.getUserId(initData);
if (userId == null) {
if (userData == null) {
return _badRequest('Invalid Telegram Web App data');
}
// Find or create user based on telegram user ID
var user = await _userManager.findByTelegramId(userId);
if (user == null) {
// Create new user if doesn't exist
user = await _userManager.createFromTelegramData(userId, initData);
}
var (user, _) = await _userManager.createOrGetUser(
externalId: userData.id,
email: '',
name: userData.username,
);
// Generate tokens
final tokens = await _jwtService.generateTokens(user);

View file

@ -41,7 +41,7 @@ class TelegramUtils {
}
}
Future<String?> getUserId(String initialData) async {
Future<({String id, String? username})?> getUserId(String initialData) async {
try {
final tgWebAppData = Uri.decodeComponent(initialData);
// Extract hash from the data
@ -64,7 +64,11 @@ class TelegramUtils {
final userJson = Uri.decodeComponent(userMatch.group(1)!);
final userData = jsonDecode(userJson) as Map<String, dynamic>;
final userId = userData['id']?.toString();
return userId;
final username = userData['username']?.toString();
if (userId == null) {
return null;
}
return (id: userId, username: username);
} catch (e) {
return null;
}