mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 00:05:16 +00:00
Can sign in user
This commit is contained in:
@ -1,3 +1,8 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:comunic/models/login_tokens.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// Accounts credentials helper
|
||||
///
|
||||
/// Stores current account tokens
|
||||
@ -8,7 +13,27 @@ class AccountCredentialsHelper {
|
||||
|
||||
/// Checkout whether current user is signed in or not
|
||||
Future<bool> signedIn() async {
|
||||
return false; // TODO : implement
|
||||
return await get() != null;
|
||||
}
|
||||
|
||||
/// Set new login tokens
|
||||
Future<void> set(LoginTokens tokens) async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString("login_tokens", tokens.toString());
|
||||
}
|
||||
|
||||
/// Get current [LoginTokens]. Returns null if none or in case of failure
|
||||
Future<LoginTokens> get() async {
|
||||
try {
|
||||
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
final string = prefs.getString("login_tokens");
|
||||
if(string == null) return null;
|
||||
return LoginTokens.fromJSON(jsonDecode(string));
|
||||
|
||||
} on Exception catch(e){
|
||||
print(e.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
41
lib/helpers/account_helper.dart
Normal file
41
lib/helpers/account_helper.dart
Normal file
@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
}
|
68
lib/helpers/api_helper.dart
Normal file
68
lib/helpers/api_helper.dart
Normal file
@ -0,0 +1,68 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:comunic/models/api_request.dart';
|
||||
import 'package:comunic/models/api_response.dart';
|
||||
import 'package:comunic/models/config.dart';
|
||||
|
||||
/// API Helper
|
||||
///
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
class APIHelper {
|
||||
final _httpClient = HttpClient();
|
||||
|
||||
/// Execute a [request] on the server and returns a [APIResponse]
|
||||
///
|
||||
/// This method should never throw but the response code of the [APIResponse]
|
||||
/// should be verified before accessing response content
|
||||
Future<APIResponse> exec(APIRequest request) async {
|
||||
try {
|
||||
//Add API tokens
|
||||
request.addString("serviceName", config().serviceName);
|
||||
request.addString("serviceToken", config().serviceToken);
|
||||
|
||||
//Add user tokens (if required)
|
||||
if (request.needLogin) throw "Can add user tokens right now !";
|
||||
|
||||
// Prepare request body
|
||||
String requestBody = "";
|
||||
request.args.forEach((key, value) => requestBody +=
|
||||
Uri.encodeQueryComponent(key) +
|
||||
"=" +
|
||||
Uri.encodeQueryComponent(value) +
|
||||
"&");
|
||||
|
||||
List<int> bodyBytes = utf8.encode(requestBody);
|
||||
|
||||
// Determine server URL
|
||||
final path = config().apiServerUri + request.uri;
|
||||
Uri url;
|
||||
if (!config().apiServerSecure)
|
||||
url = Uri.http(config().apiServerName, path);
|
||||
else
|
||||
url = Uri.https(config().apiServerName, path);
|
||||
|
||||
//Connect to server
|
||||
final connection = await _httpClient.postUrl(url);
|
||||
connection.headers.set("Content-Length", bodyBytes.length.toString());
|
||||
connection.headers
|
||||
.set("Content-Type", "application/x-www-form-urlencoded");
|
||||
connection.add(bodyBytes);
|
||||
|
||||
final response = await connection.close();
|
||||
|
||||
if (response.statusCode != HttpStatus.ok)
|
||||
return APIResponse(response.statusCode, null);
|
||||
|
||||
//Save & return response
|
||||
final responseBody = await response.transform(utf8.decoder).join();
|
||||
|
||||
return APIResponse(response.statusCode, responseBody);
|
||||
} on Exception catch (e) {
|
||||
print(e.toString());
|
||||
print("Could not execute a request!");
|
||||
return APIResponse(-1, null);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user