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 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 signOut() async { await AccountCredentialsHelper().set(null); } }