1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 16:26:27 +00:00
comunicmobile/lib/helpers/account_helper.dart

48 lines
1.3 KiB
Dart
Raw Normal View History

2019-04-22 17:16:26 +00:00
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;
}
2019-04-23 09:48:49 +00:00
/// Sign out user
Future<void> signOut() async {
await AccountCredentialsHelper().set(null);
}
2019-04-22 17:16:26 +00:00
}