Files
eatvio_app/lib/features/auth/auth_screen/auth_screen.dart
2025-02-12 14:44:58 +03:00

136 lines
5.1 KiB
Dart

import 'dart:convert';
import 'package:eatvio_app/core/const.dart';
import 'package:eatvio_app/core/credentials_storage.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/features/auth/const/strings.dart';
import 'package:eatvio_app/features/bottom_navigation_bar/presentation/bloc/profile_avatar_cubit/profile_avatar_cubit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_native_splash/flutter_native_splash.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import 'package:webview_flutter/webview_flutter.dart';
class AuthScreen extends StatefulWidget {
final bool needLogout;
const AuthScreen({
this.needLogout = false,
});
@override
State<AuthScreen> createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
bool checkboxState = false;
bool hidePassword = true;
final baseUrlController = TextEditingController();
late final WebViewController _controller;
String email = '';
String password = '';
Future<void> logout() async {
await CredentialsStorage.removeCredentials();
await _controller.runJavaScript('''
document.querySelector('.header-list__link button[type="submit"]').click();
''');
}
@override
void initState() {
super.initState();
_controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..addJavaScriptChannel(
'FlutterChannel',
onMessageReceived: (JavaScriptMessage message) {
final data = jsonDecode(message.message);
email = data['email'];
password = data['password'];
},
)
..setNavigationDelegate(
NavigationDelegate(
onNavigationRequest: (NavigationRequest navigationRequest) async {
if (navigationRequest.url.contains('profile')) {
if (await CredentialsStorage.haveCredentialsInStorage()) {
email = (await CredentialsStorage.getCredentialsFromStorage())['email']!;
password = (await CredentialsStorage.getCredentialsFromStorage())['password']!;
} else {
if (email.isNotEmpty && password.isNotEmpty && !(await TokenStorage.haveTokenInStorage())) {
await CredentialsStorage.saveCredentials(email, password);
try {
final Map<String, String> credentials = {
'email': email,
'password': password,
};
final response = await http.post(
Uri.parse(baseUrl + '/api/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8,
);
var res = await jsonDecode(response.body);
final token = res['token'];
await TokenStorage.saveTokenInStorage(token);
GoRouter.of(context).go('/my-profile-screen');
await sl<ProfileAvatarCubit>().getMyProfile();
} catch (e) {
print(e);
}
} else {
await logout();
}
}
}
return NavigationDecision.navigate;
},
onPageStarted: (String url) async {
final bool tokenInStorage = await TokenStorage.haveTokenInStorage();
final bool credentialsInStorage = await CredentialsStorage.haveCredentialsInStorage();
if (tokenInStorage && credentialsInStorage) {
GoRouter.of(context).go('/my-profile-screen');
} else if (!tokenInStorage && credentialsInStorage) {
final credentialsFromStorage = await CredentialsStorage.getCredentialsFromStorage();
final Map<String, String> credentials = {
'email': credentialsFromStorage['email']!,
'password': credentialsFromStorage['password']!,
};
final response = await http.post(
Uri.parse(baseUrl + '/api/login'),
headers: {'Content-Type': 'application/json'},
body: json.encode(credentials),
encoding: utf8,
);
var res = await jsonDecode(response.body);
final token = res['token'];
await TokenStorage.saveTokenInStorage(token);
GoRouter.of(context).go('/my-profile-screen');
await sl<ProfileAvatarCubit>().getMyProfile();
}
},
onPageFinished: (String url) async {
await _controller.runJavaScript(jsChannelAuthScript);
},
),
)
..loadRequest(Uri.parse(baseUrl));
}
@override
Widget build(BuildContext context) {
FlutterNativeSplash.remove();
return Scaffold(
appBar: AppBar(
toolbarHeight: 0,
),
body: SafeArea(
child: WebViewWidget(controller: _controller),
),
);
}
}