1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00
comunicmobile/lib/helpers/account_helper.dart

48 lines
1.3 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;
}
/// Sign out user
Future<void> signOut() async {
await AccountCredentialsHelper().set(null);
}
}