mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-26 06:49:22 +00:00
42 lines
1.2 KiB
Dart
42 lines
1.2 KiB
Dart
import 'package:comunic/helpers/account_credentials_helper.dart';
|
|
import 'package:comunic/helpers/api_helper.dart';
|
|
import 'package:comunic/models/api_request.dart';
|
|
import 'package:comunic/models/authentication_details.dart';
|
|
import 'package:comunic/models/login_tokens.dart';
|
|
|
|
/// Account helper
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
enum AuthResult {
|
|
SUCCESS,
|
|
TOO_MANY_ATTEMPTS,
|
|
NETWORK_ERROR,
|
|
INVALID_CREDENTIALS
|
|
}
|
|
|
|
class AccountHelper {
|
|
/// Sign in user
|
|
Future<AuthResult> signIn(AuthenticationDetails auth) async {
|
|
final request = APIRequest(uri: "account/login");
|
|
request.addString("userMail", auth.email);
|
|
request.addString("userPassword", auth.password);
|
|
|
|
final response = await APIHelper().exec(request);
|
|
|
|
// Handle errors
|
|
if (response.code == 401)
|
|
return AuthResult.INVALID_CREDENTIALS;
|
|
else if (response.code == 429)
|
|
return AuthResult.TOO_MANY_ATTEMPTS;
|
|
else if (response.code != 200) return AuthResult.NETWORK_ERROR;
|
|
|
|
//Save login tokens
|
|
final tokensObj = response.getObject()["tokens"];
|
|
await AccountCredentialsHelper()
|
|
.set(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
|
|
|
|
return AuthResult.SUCCESS;
|
|
}
|
|
}
|