fbmobile/lib/core/viewmodels/login_model.dart

181 lines
5.7 KiB
Dart
Raw Normal View History

import 'dart:async';
2021-02-02 14:33:23 +00:00
import 'dart:io';
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';
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();
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-02-02 14:33:23 +00:00
TextEditingController get uriController => _uriController;
TextEditingController get userNameController => _userNameController;
TextEditingController get passwordController => _passwordController;
2021-02-02 14:33:23 +00:00
TextEditingController get apiKeyController => _apiKeyController;
2021-02-02 14:33:23 +00:00
final SessionService _sessionService = locator<SessionService>();
final StorageService _storageService = locator<StorageService>();
final UserService _userService = locator<UserService>();
2021-02-02 14:33:23 +00:00
final Logger _logger = getLogger();
bool useCredentialsLogin = true;
String? errorMessage;
2021-02-02 14:33:23 +00:00
void toggleLoginMethod() {
2023-01-04 20:17:54 +00:00
setStateView(ViewState.busy);
useCredentialsLogin = !useCredentialsLogin;
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
}
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);
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
}
}
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);
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;
}
if (useCredentialsLogin) {
if (username.isEmpty) {
errorMessage = translate('login.errors.empty_username');
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
return false;
}
if (password.isEmpty) {
errorMessage = translate('login.errors.empty_password');
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
return false;
}
} else {
if (apiKey.isEmpty) {
errorMessage = translate('login.errors.empty_apikey');
2023-01-04 20:17:54 +00:00
setStateView(ViewState.idle);
return false;
}
2021-02-02 14:33:23 +00:00
}
var success = false;
try {
if (useCredentialsLogin) {
CreateApiKeyResponse apiKeyResponse = await _userService.createApiKey(
2023-01-16 00:44:34 +00:00
url,
username,
password,
'apikey',
'fbmobile-${DateTime.now().millisecondsSinceEpoch}');
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'));
}
} else {
_sessionService.setApiConfig(url, apiKey);
await _userService.checkAccessLevelIsAtLeastApiKey();
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');
}
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) {
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);
_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
}
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;
}
}