auto login
This commit is contained in:
parent
a3a63fea61
commit
a807d0f90f
7 changed files with 155 additions and 16 deletions
|
|
@ -38,6 +38,6 @@ _flutter.buildConfig = {"engineRevision":"d3d45dcf251823c1769909cd43698d126db38d
|
|||
|
||||
_flutter.loader.load({
|
||||
serviceWorkerSettings: {
|
||||
serviceWorkerVersion: "2889484362"
|
||||
serviceWorkerVersion: "3923216343"
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import 'dart:developer';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mnemo_cards_web_v2/di/user_scope/user_scope.dart';
|
||||
import 'package:telegram_web_app/telegram_web_app.dart';
|
||||
import 'package:yx_scope_flutter/yx_scope_flutter.dart';
|
||||
import 'package:yx_state_flutter/yx_state_flutter.dart';
|
||||
|
||||
|
|
@ -65,10 +66,47 @@ class _AppInitializerState extends State<_AppInitializer> {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> _tryTelegramWebAppAuth() async {
|
||||
try {
|
||||
log('Checking Telegram Web App authentication...', name: 'App');
|
||||
|
||||
// Try to authenticate with Telegram Web App
|
||||
final user = await widget.appScope.authService.loginWithTelegramWebApp();
|
||||
|
||||
if (user != null) {
|
||||
log('Telegram Web App authentication successful', name: 'App');
|
||||
|
||||
// Create UserScope if it doesn't exist
|
||||
if (widget.appScope.userScopeHolder.scope == null) {
|
||||
await widget.appScope.userScopeHolder.create();
|
||||
}
|
||||
|
||||
// Set authenticated user
|
||||
final userScope = widget.appScope.userScopeHolder.scope;
|
||||
if (userScope != null) {
|
||||
userScope.userStateManager.setUser(user);
|
||||
// Notify router about auth change
|
||||
widget.appScope.userScopeHolder.notifyAuthChanged();
|
||||
}
|
||||
|
||||
// Don't continue with normal auto-login if Telegram auth succeeded
|
||||
return;
|
||||
} else {
|
||||
log('Telegram Web App authentication not available or failed', name: 'App');
|
||||
}
|
||||
} catch (e, s) {
|
||||
log('Error during Telegram Web App authentication', error: e, stackTrace: s);
|
||||
// Continue with normal flow - this is not a critical error
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _initialize() async {
|
||||
try {
|
||||
log('Starting app initialization...', name: 'App');
|
||||
|
||||
|
||||
// Try Telegram Web App authentication first
|
||||
await _tryTelegramWebAppAuth();
|
||||
|
||||
// Try auto-login first with timeout
|
||||
log('Attempting auto-login...', name: 'App');
|
||||
UserDto? user;
|
||||
|
|
@ -89,7 +127,7 @@ class _AppInitializerState extends State<_AppInitializer> {
|
|||
log('Creating UserScope...', name: 'App');
|
||||
await widget.appScope.userScopeHolder.create();
|
||||
}
|
||||
|
||||
|
||||
if (user != null) {
|
||||
log('Auto-login successful, creating UserScope', name: 'App');
|
||||
// Create UserScope only for authenticated users
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import 'package:google_sign_in/google_sign_in.dart';
|
|||
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
||||
import 'package:mnemo_cards_web_v2/domain/models/telegram_auth_code_status.dart';
|
||||
import 'package:mnemo_cards_web_v2/domain/services/http_repository_v2.dart';
|
||||
import 'package:telegram_web_app/telegram_web_app.dart';
|
||||
|
||||
/// Service for user authentication
|
||||
///
|
||||
|
|
@ -22,6 +23,53 @@ class AuthService {
|
|||
final GoogleSignIn _googleSignIn;
|
||||
final HttpRepositoryV2 _httpRepository;
|
||||
|
||||
/// Login with Telegram Web App
|
||||
///
|
||||
/// Automatically authenticates user using Telegram Web App data
|
||||
/// Returns the authenticated user or null if login failed
|
||||
Future<UserDto?> loginWithTelegramWebApp() async {
|
||||
try {
|
||||
log('Starting Telegram Web App authentication', name: 'AuthService');
|
||||
|
||||
// Check if Telegram Web App is supported
|
||||
if (!TelegramWebApp.instance.isSupported) {
|
||||
log('Telegram Web App is not supported', name: 'AuthService');
|
||||
return null;
|
||||
}
|
||||
|
||||
// Get init data from Telegram Web App
|
||||
final webAppData = TelegramWebApp.instance;
|
||||
final initData = webAppData.initData.toString();
|
||||
if (initData.isEmpty) {
|
||||
log('No Telegram Web App init data available', name: 'AuthService');
|
||||
return null;
|
||||
}
|
||||
|
||||
log('Telegram Web App init data received, sending to backend', name: 'AuthService');
|
||||
|
||||
// Authenticate with backend using Telegram Web App data
|
||||
final authResponse = await _httpRepository.authenticateWithTelegramWebApp(
|
||||
initData: initData,
|
||||
);
|
||||
|
||||
if (authResponse == null) {
|
||||
log('Telegram Web App authentication failed - no response', name: 'AuthService');
|
||||
return null;
|
||||
}
|
||||
|
||||
log('Telegram Web App authentication successful', name: 'AuthService');
|
||||
return authResponse.user;
|
||||
} catch (e, s) {
|
||||
log(
|
||||
'Error during Telegram Web App authentication',
|
||||
error: e,
|
||||
stackTrace: s,
|
||||
name: 'AuthService',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Login with Google account
|
||||
///
|
||||
/// Returns the authenticated user or null if login failed/cancelled
|
||||
|
|
|
|||
|
|
@ -394,6 +394,56 @@ class HttpRepositoryV2 {
|
|||
|
||||
// ==================== Authentication Endpoints ====================
|
||||
|
||||
/// Authenticate with Telegram Web App
|
||||
/// Returns auth response with user, access token, and refresh token
|
||||
Future<AuthResponseV2?> authenticateWithTelegramWebApp({
|
||||
required String initData,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _dio.post<Map<String, dynamic>>(
|
||||
'/auth/telegram/web-app',
|
||||
data: {'initData': initData},
|
||||
);
|
||||
|
||||
if (response.data != null) {
|
||||
final data = response.data!;
|
||||
|
||||
final userDto = UserDto.fromJson(data['user'] as Map<String, dynamic>);
|
||||
final accessToken = data['accessToken'] as String;
|
||||
final refreshToken = data['refreshToken'] as String;
|
||||
final expiresIn = data['expiresIn'] as int?;
|
||||
|
||||
// Save tokens
|
||||
await saveTokens(
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
expiresIn: expiresIn,
|
||||
);
|
||||
|
||||
final expiresAt = expiresIn != null
|
||||
? DateTime.now().add(Duration(seconds: expiresIn))
|
||||
: null;
|
||||
|
||||
return AuthResponseV2(
|
||||
user: userDto,
|
||||
accessToken: accessToken,
|
||||
refreshToken: refreshToken,
|
||||
expiresAt: expiresAt,
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
} on DioException catch (e) {
|
||||
if (e.error is ApiException) {
|
||||
rethrow;
|
||||
}
|
||||
throw NetworkException(
|
||||
message: e.message ?? 'Failed to authenticate with Telegram Web App',
|
||||
originalError: e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Authenticate with Google ID token
|
||||
/// Returns auth response with user, access token, and refresh token
|
||||
Future<AuthResponseV2> authenticateWithGoogle(String googleIdToken) async {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ import 'dart:developer';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
// Telegram integration temporarily disabled due to compilation errors
|
||||
// TODO: Re-enable when telegram_web_app package is updated
|
||||
// import 'package:telegram_web_app/telegram_web_app.dart';
|
||||
import 'package:telegram_web_app/telegram_web_app.dart';
|
||||
|
||||
import 'app.dart';
|
||||
import 'di/app_scope/app_scope_holder.dart';
|
||||
|
|
@ -15,13 +13,12 @@ final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
|||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// Telegram Web App integration temporarily disabled
|
||||
// TODO: Re-enable when telegram_web_app package is updated
|
||||
// if (TelegramWebApp.instance.isSupported) {
|
||||
// TelegramWebApp.instance.ready();
|
||||
// await Future<void>.delayed(const Duration(seconds: 1));
|
||||
// TelegramWebApp.instance.disableVerticalSwipes();
|
||||
// }
|
||||
// Telegram Web App integration
|
||||
if (TelegramWebApp.instance.isSupported) {
|
||||
TelegramWebApp.instance.ready();
|
||||
await Future<void>.delayed(const Duration(seconds: 1));
|
||||
TelegramWebApp.instance.disableVerticalSwipes();
|
||||
}
|
||||
|
||||
// Настройка логирования ошибок
|
||||
FlutterError.onError = (details) {
|
||||
|
|
|
|||
|
|
@ -1216,6 +1216,14 @@ packages:
|
|||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.1"
|
||||
telegram_web_app:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: telegram_web_app
|
||||
sha256: "5c33aaa07d776552ce55b49b401d0e835115fbeb136877c6d19b3b1491000535"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.3.3"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
|
|
|||
|
|
@ -11,9 +11,7 @@ dependencies:
|
|||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
# Telegram integration temporarily disabled due to compilation errors
|
||||
# TODO: Re-enable when package is updated or find alternative
|
||||
# telegram_web_app: ^0.3.3
|
||||
telegram_web_app: ^0.3.3
|
||||
|
||||
# YX Framework
|
||||
yx_scope: ^1.1.2
|
||||
|
|
|
|||
Loading…
Reference in a new issue