2021-11-29 23:44:22 +00:00
|
|
|
import 'dart:async';
|
2021-02-02 14:33:23 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
import 'package:flutter_translate/flutter_translate.dart';
|
|
|
|
import 'package:logger/logger.dart';
|
|
|
|
import 'package:validators/sanitizers.dart';
|
|
|
|
import 'package:validators/validators.dart';
|
|
|
|
|
|
|
|
import '../../core/services/session_service.dart';
|
|
|
|
import '../../core/services/storage_service.dart';
|
|
|
|
import '../../locator.dart';
|
|
|
|
import '../enums/error_code.dart';
|
|
|
|
import '../enums/viewstate.dart';
|
|
|
|
import '../error/rest_service_exception.dart';
|
|
|
|
import '../error/service_exception.dart';
|
2021-02-13 14:09:40 +00:00
|
|
|
import '../models/rest/create_apikey_response.dart';
|
|
|
|
import '../services/user_service.dart';
|
2021-02-02 14:33:23 +00:00
|
|
|
import '../util/logger.dart';
|
|
|
|
import 'base_model.dart';
|
|
|
|
|
|
|
|
class LoginModel extends BaseModel {
|
2023-01-04 20:17:54 +00:00
|
|
|
TextEditingController _uriController = TextEditingController();
|
2021-04-05 20:06:54 +00:00
|
|
|
|
2023-01-04 20:17:54 +00:00
|
|
|
final TextEditingController _userNameController = TextEditingController();
|
|
|
|
final TextEditingController _passwordController = TextEditingController();
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2023-01-04 20:17:54 +00:00
|
|
|
final TextEditingController _apiKeyController = TextEditingController();
|
2021-04-05 20:06:54 +00:00
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
TextEditingController get uriController => _uriController;
|
|
|
|
|
2021-02-13 14:09:40 +00:00
|
|
|
TextEditingController get userNameController => _userNameController;
|
|
|
|
|
|
|
|
TextEditingController get passwordController => _passwordController;
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
TextEditingController get apiKeyController => _apiKeyController;
|
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
final SessionService _sessionService = locator<SessionService>();
|
|
|
|
final StorageService _storageService = locator<StorageService>();
|
2021-02-13 14:09:40 +00:00
|
|
|
final UserService _userService = locator<UserService>();
|
2021-02-02 14:33:23 +00:00
|
|
|
final Logger _logger = getLogger();
|
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
bool useCredentialsLogin = true;
|
2021-11-29 23:44:22 +00:00
|
|
|
String? errorMessage;
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
void toggleLoginMethod() {
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.busy);
|
2021-04-05 20:06:54 +00:00
|
|
|
useCredentialsLogin = !useCredentialsLogin;
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-04-05 20:06:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 14:33:23 +00:00
|
|
|
void init() async {
|
|
|
|
bool hasLastUrl = await _storageService.hasLastUrl();
|
|
|
|
|
|
|
|
if (hasLastUrl) {
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.busy);
|
2021-11-29 23:44:22 +00:00
|
|
|
var s = await (_storageService.retrieveLastUrl() as FutureOr<String>);
|
2021-02-02 14:33:23 +00:00
|
|
|
|
|
|
|
if (s.isNotEmpty) {
|
2023-01-04 20:17:54 +00:00
|
|
|
_uriController = TextEditingController(text: s);
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
Future<bool> login() async {
|
|
|
|
var url = uriController.text;
|
|
|
|
var username = userNameController.text;
|
|
|
|
var password = passwordController.text;
|
|
|
|
var apiKey = apiKeyController.text;
|
2021-02-02 14:33:23 +00:00
|
|
|
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.busy);
|
2021-02-02 14:33:23 +00:00
|
|
|
url = trim(url);
|
2021-02-13 14:09:40 +00:00
|
|
|
username = trim(username);
|
2021-02-02 14:33:23 +00:00
|
|
|
|
|
|
|
if (url.isEmpty) {
|
|
|
|
errorMessage = translate('login.errors.empty_url');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-02-02 14:33:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!url.contains("https://") && !url.contains("http://")) {
|
|
|
|
errorMessage = translate('login.errors.no_protocol');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-02-02 14:33:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validUri = Uri.parse(url).isAbsolute;
|
|
|
|
if (!validUri || !isURL(url)) {
|
|
|
|
errorMessage = translate('login.errors.invalid_url');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-02-02 14:33:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
if (useCredentialsLogin) {
|
|
|
|
if (username.isEmpty) {
|
|
|
|
errorMessage = translate('login.errors.empty_username');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-04-05 20:06:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-13 14:09:40 +00:00
|
|
|
|
2021-04-05 20:06:54 +00:00
|
|
|
if (password.isEmpty) {
|
|
|
|
errorMessage = translate('login.errors.empty_password');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-04-05 20:06:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (apiKey.isEmpty) {
|
|
|
|
errorMessage = translate('login.errors.empty_apikey');
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-04-05 20:06:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var success = false;
|
|
|
|
try {
|
2021-04-05 20:06:54 +00:00
|
|
|
if (useCredentialsLogin) {
|
|
|
|
CreateApiKeyResponse apiKeyResponse = await _userService.createApiKey(
|
2023-01-16 00:44:34 +00:00
|
|
|
url,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
'apikey',
|
|
|
|
'fbmobile-${DateTime.now().millisecondsSinceEpoch}');
|
2021-11-29 23:44:22 +00:00
|
|
|
|
|
|
|
var newKey = apiKeyResponse.data['new_key'];
|
|
|
|
if (newKey != null) {
|
|
|
|
success = await _sessionService.login(url, newKey);
|
|
|
|
} else {
|
2023-01-16 00:44:34 +00:00
|
|
|
throw ServiceException(
|
|
|
|
code: ErrorCode.invalidApiKey,
|
|
|
|
message: translate('login.errors.invalid_api_key'));
|
2021-11-29 23:44:22 +00:00
|
|
|
}
|
2021-04-05 20:06:54 +00:00
|
|
|
} else {
|
|
|
|
_sessionService.setApiConfig(url, apiKey);
|
2021-05-09 08:59:14 +00:00
|
|
|
await _userService.checkAccessLevelIsAtLeastApiKey();
|
2021-04-05 20:06:54 +00:00
|
|
|
success = await _sessionService.login(url, apiKey);
|
|
|
|
}
|
2021-02-02 14:33:23 +00:00
|
|
|
errorMessage = null;
|
|
|
|
} catch (e) {
|
|
|
|
if (e is RestServiceException) {
|
|
|
|
if (e.statusCode == HttpStatus.unauthorized) {
|
|
|
|
errorMessage = translate('login.errors.wrong_credentials');
|
2023-01-16 00:44:34 +00:00
|
|
|
} else if (e.statusCode != HttpStatus.unauthorized &&
|
|
|
|
e.statusCode == HttpStatus.forbidden) {
|
2021-02-02 14:33:23 +00:00
|
|
|
errorMessage = translate('login.errors.forbidden');
|
|
|
|
} else if (e.statusCode == HttpStatus.notFound) {
|
|
|
|
errorMessage = translate('api.incompatible_error_not_found');
|
2021-02-13 14:09:40 +00:00
|
|
|
}
|
|
|
|
if (e.statusCode == HttpStatus.badRequest) {
|
2023-01-16 00:44:34 +00:00
|
|
|
errorMessage = translate('api.bad_request',
|
|
|
|
args: {'reason': e.responseBody.message});
|
2021-02-02 14:33:23 +00:00
|
|
|
} else {
|
|
|
|
errorMessage = translate('api.general_rest_error');
|
|
|
|
}
|
2023-01-04 20:17:54 +00:00
|
|
|
} else if (e is ServiceException && e.code == ErrorCode.invalidApiKey) {
|
2021-04-05 20:06:54 +00:00
|
|
|
errorMessage = translate('login.errors.invalid_api_key');
|
2023-01-04 20:17:54 +00:00
|
|
|
} else if (e is ServiceException && e.code == ErrorCode.socketError) {
|
2021-02-02 14:33:23 +00:00
|
|
|
errorMessage = translate('api.socket_error');
|
2023-01-04 20:17:54 +00:00
|
|
|
} else if (e is ServiceException && e.code == ErrorCode.socketTimeout) {
|
2021-02-02 14:33:23 +00:00
|
|
|
errorMessage = translate('api.socket_timeout');
|
|
|
|
} else {
|
|
|
|
errorMessage = translate('app.unknown_error');
|
|
|
|
_sessionService.logout();
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2023-07-26 20:42:51 +00:00
|
|
|
_logger.e('An unknown error occurred', error: e);
|
2023-01-04 20:17:54 +00:00
|
|
|
rethrow;
|
2021-02-02 14:33:23 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 23:44:22 +00:00
|
|
|
if (errorMessage!.isNotEmpty) {
|
2021-02-02 14:33:23 +00:00
|
|
|
_sessionService.logout();
|
|
|
|
}
|
|
|
|
|
2023-01-04 20:17:54 +00:00
|
|
|
setStateView(ViewState.idle);
|
2021-02-02 14:33:23 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
}
|