forked from eatvio/eatvio_app
feat: auto login after registration
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:eatvio_app/core/const.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/features/auth/presentation/bloc/auth_cubit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
|
||||
class WebViewRegistrationScreen extends StatefulWidget {
|
||||
@@ -12,23 +15,71 @@ class WebViewRegistrationScreen extends StatefulWidget {
|
||||
|
||||
class _WebViewRegistrationScreenState extends State<WebViewRegistrationScreen> {
|
||||
late final WebViewController _controller;
|
||||
late final AuthCubit authCubit;
|
||||
String? email;
|
||||
String? password;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
authCubit = sl<AuthCubit>();
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
..addJavaScriptChannel(
|
||||
'flutter_inject',
|
||||
onMessageReceived: (JavaScriptMessage message) {
|
||||
final data = jsonDecode(message.message);
|
||||
if (data['type'] == 'email') {
|
||||
email = data['value'];
|
||||
print("Email: $email");
|
||||
} else if (data['type'] == 'password') {
|
||||
password = data['value'];
|
||||
print("Password: $password");
|
||||
}
|
||||
},
|
||||
)
|
||||
..setNavigationDelegate(
|
||||
NavigationDelegate(
|
||||
onNavigationRequest: (NavigationRequest request) {
|
||||
if (request.url.contains('profile')) {
|
||||
GoRouter.of(context).go('authScreen');
|
||||
authCubit.getTokenByCredentials(
|
||||
email!,
|
||||
password!,
|
||||
);
|
||||
}
|
||||
return NavigationDecision.navigate;
|
||||
},
|
||||
),
|
||||
)
|
||||
..loadRequest(Uri.parse(baseUrl + '/registration/step1'));
|
||||
|
||||
_injectJavaScript();
|
||||
}
|
||||
|
||||
void _injectJavaScript() async {
|
||||
await Future.delayed(Duration(seconds: 2));
|
||||
_controller.runJavaScript('''
|
||||
function sendMessageToFlutter(data) {
|
||||
window.flutter_inject.postMessage(JSON.stringify(data));
|
||||
}
|
||||
|
||||
function trackInputChanges() {
|
||||
let emailField = document.getElementById('inputEmail');
|
||||
let passwordField = document.getElementById('inputPassword');
|
||||
|
||||
if (emailField && passwordField) {
|
||||
emailField.addEventListener('input', function() {
|
||||
sendMessageToFlutter({ type: 'email', value: emailField.value });
|
||||
});
|
||||
|
||||
passwordField.addEventListener('input', function() {
|
||||
sendMessageToFlutter({ type: 'password', value: passwordField.value });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
trackInputChanges();
|
||||
''');
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user