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 // Use the existing telegram utils to get user ID
final telegramUtils = TelegramUtils(); 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'); return _badRequest('Invalid Telegram Web App data');
} }
// Find or create user based on telegram user ID // Find or create user based on telegram user ID
var user = await _userManager.findByTelegramId(userId); var (user, _) = await _userManager.createOrGetUser(
if (user == null) { externalId: userData.id,
// Create new user if doesn't exist email: '',
user = await _userManager.createFromTelegramData(userId, initData); name: userData.username,
} );
// Generate tokens // Generate tokens
final tokens = await _jwtService.generateTokens(user); 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 { try {
final tgWebAppData = Uri.decodeComponent(initialData); final tgWebAppData = Uri.decodeComponent(initialData);
// Extract hash from the data // Extract hash from the data
@ -64,7 +64,11 @@ class TelegramUtils {
final userJson = Uri.decodeComponent(userMatch.group(1)!); final userJson = Uri.decodeComponent(userMatch.group(1)!);
final userData = jsonDecode(userJson) as Map<String, dynamic>; final userData = jsonDecode(userJson) as Map<String, dynamic>;
final userId = userData['id']?.toString(); final userId = userData['id']?.toString();
return userId; final username = userData['username']?.toString();
if (userId == null) {
return null;
}
return (id: userId, username: username);
} catch (e) { } catch (e) {
return null; return null;
} }