/
brndmgd
/
ai_architect_client
Обзор
Документация
Войти
/
brndmgd
/
ai_architect_client
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
lib/src/features/auth/auth_webview_controller.dart
76 строк
2 KB
brndmgd
feat: add github repository
15 ноя 2025, 19:06
15 ноя 2025, 19:06
435ace7
Код
Авторство
О чём код?
import 'package:flutter/material.dart'; import 'package:webview_flutter/webview_flutter.dart'; import '../../app_routes.dart'; import '../../config/api_service.dart'; import 'auth_service.dart'; class AuthWebViewController { final BuildContext context; final void Function(VoidCallback fn) setState; late final WebViewController webViewController; final AuthService _authService = AuthService(); bool isParsing = false; bool hasError = false; AuthWebViewController(this.context, this.setState); void init() { webViewController = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setNavigationDelegate( NavigationDelegate( onPageStarted: _onPageStarted, onPageFinished: _onPageFinished, onWebResourceError: _onWebResourceError, ), ) ..loadRequest(Uri.parse(ApiService.baseUrl + '/auth/yandex/login')); } void _onPageStarted(String url) { final isCallback = _authService.isAuthCallback(url); setState(() { isParsing = isCallback; hasError = false; }); } Future<void> _onPageFinished(String url) async { if (!isParsing) return; try { final String pageContent = await webViewController.runJavaScriptReturningResult( "document.body.innerText", ) as String; final cleanedJson = _authService.cleanJsonString(pageContent); final success = await _authService.handleTokens(cleanedJson); if (success) { _handleSuccess(); } else { _handleError("Couldn't authorize"); } } catch (e) { _handleError('Failed to process authentication: $e'); Navigator.of(context).pop(); } } void _onWebResourceError(WebResourceError error) { _handleError('Web resource error: ${error.description}, ${error.url}'); } void _handleSuccess() { Navigator.pushNamedAndRemoveUntil(context, AppRoutes.chat, (route) => false); } void _handleError(String errorMessage) { print(errorMessage); setState(() { hasError = true; isParsing = false; }); } }