forked from eatvio/eatvio_app
Merge pull request 'fix: webview logic' (#225) from BLoC/eatvio_app:main into main
Reviewed-on: eatvio/eatvio_app#225
This commit is contained in:
@@ -30,17 +30,38 @@ class _AuthScreenState extends State<AuthScreen> {
|
||||
|
||||
String email = '';
|
||||
String password = '';
|
||||
bool needLogout = false;
|
||||
|
||||
Future<void> logout() async {
|
||||
Future<void> logoutFromWeb() async {
|
||||
await CredentialsStorage.removeCredentials();
|
||||
await _controller.runJavaScript('''
|
||||
document.querySelector('.header-list__link button[type="submit"]').click();
|
||||
''');
|
||||
}
|
||||
|
||||
Future<void> loginInApplication() async {
|
||||
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);
|
||||
await Future.delayed(Duration.zero, () => GoRouter.of(context).go('/my-profile-screen'));
|
||||
await sl<ProfileAvatarCubit>().getMyProfile();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
needLogout = widget.needLogout;
|
||||
|
||||
_controller = WebViewController()
|
||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||
@@ -54,66 +75,25 @@ class _AuthScreenState extends State<AuthScreen> {
|
||||
)
|
||||
..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();
|
||||
}
|
||||
onPageFinished: (String url) async {
|
||||
if (needLogout) {
|
||||
await logoutFromWeb();
|
||||
needLogout = false;
|
||||
} else if (!url.contains('profile') && !url.contains('registration')) {
|
||||
await _controller.runJavaScript(jsChannelAuthScript);
|
||||
} else if (url.contains('profile')) {
|
||||
await CredentialsStorage.saveCredentials(email, password);
|
||||
await loginInApplication();
|
||||
} else {
|
||||
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) {
|
||||
await loginInApplication();
|
||||
}
|
||||
}
|
||||
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);
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user