1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 04:49:21 +00:00

Upgrade login system

This commit is contained in:
Pierre HUBERT 2021-02-13 16:03:07 +01:00
parent 581059cb1d
commit e5ed4fadda
7 changed files with 32 additions and 63 deletions

View File

@ -3,7 +3,6 @@ import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/authentication_details.dart';
import 'package:comunic/models/login_tokens.dart';
import 'package:comunic/models/new_account.dart';
import 'package:shared_preferences/shared_preferences.dart';
@ -36,7 +35,7 @@ class AccountHelper {
/// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!!
Future<bool> signedIn() async {
bool signedIn =
(await PreferencesHelper.getInstance()).getLoginTokens() != null;
(await PreferencesHelper.getInstance()).getLoginToken() != null;
// Load current user ID for later use
if (signedIn && _currentUserID == -1) await _loadCurrentUserID();
@ -47,8 +46,8 @@ 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);
request.addString("mail", auth.email);
request.addString("password", auth.password);
final response = await APIHelper().exec(request);
@ -59,10 +58,9 @@ class AccountHelper {
return AuthResult.TOO_MANY_ATTEMPTS;
else if (response.code != 200) return AuthResult.NETWORK_ERROR;
// Save login tokens
final tokensObj = response.getObject()["tokens"];
// Save login token
await (await PreferencesHelper.getInstance())
.setLoginTokens(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
.setLoginToken(response.getObject()["token"]);
// Get current user ID
final userID = await _downloadCurrentUserID();
@ -81,10 +79,11 @@ class AccountHelper {
/// Sign out user
Future<void> signOut() async {
await (await PreferencesHelper.getInstance()).setLoginTokens(null);
await (await PreferencesHelper.getInstance()).setLoginToken(null);
_currentUserID = 0;
// Close current web socket
await APIRequest.withLogin("account/logout").exec();
WebSocketHelper.close();
}
@ -178,10 +177,7 @@ class AccountHelper {
/// Get current user ID from the server
Future<int> _downloadCurrentUserID() async {
final response = await APIRequest(
uri: "user/getCurrentUserID",
needLogin: true,
).exec();
final response = await APIRequest.withLogin("account/id").exec();
if (response.code != 200) return null;

View File

@ -19,15 +19,13 @@ class APIHelper {
Future<APIResponse> exec(APIRequest request, {bool multipart = false}) async {
try {
//Add API tokens
request.addString("serviceName", config().serviceName);
request.addString("serviceToken", config().serviceToken);
request.addString("client", config().clientName);
//Add user tokens (if required)
if (request.needLogin) {
final tokens = (await PreferencesHelper.getInstance()).getLoginTokens();
assert(tokens != null);
request.addString("userToken1", tokens.tokenOne);
request.addString("userToken2", tokens.tokenTwo);
final token = (await PreferencesHelper.getInstance()).getLoginToken();
assert(token != null);
request.addString("token", token);
}
// Determine server URL

View File

@ -1,7 +1,4 @@
import 'dart:convert';
import 'package:comunic/models/application_preferences.dart';
import 'package:comunic/models/login_tokens.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// Preferences helper
@ -11,14 +8,14 @@ import 'package:shared_preferences/shared_preferences.dart';
/// @author Pierre HUBERT
enum PreferencesKeyList {
LOGIN_TOKENS,
LOGIN_TOKEN,
ENABLE_DARK_THEME,
FORCE_MOBILE_MODE,
SHOW_PERFORMANCE_OVERLAY,
}
const _PreferenceKeysName = {
PreferencesKeyList.LOGIN_TOKENS: "login_tokens",
PreferencesKeyList.LOGIN_TOKEN: "login_token",
PreferencesKeyList.ENABLE_DARK_THEME: "dark_theme",
PreferencesKeyList.FORCE_MOBILE_MODE: "force_mobile_mode",
PreferencesKeyList.SHOW_PERFORMANCE_OVERLAY: "perfs_overlay",
@ -45,23 +42,28 @@ class PreferencesHelper {
}
/// Set new login tokens
Future<void> setLoginTokens(LoginTokens tokens) async {
await setString(PreferencesKeyList.LOGIN_TOKENS,
tokens == null ? "null" : tokens.toString());
Future<void> setLoginToken(String token) async {
if (token != null)
await setString(PreferencesKeyList.LOGIN_TOKEN, token);
else
await _sharedPreferences.remove(token);
}
/// Get current [LoginTokens]. Returns null if none or in case of failure
LoginTokens getLoginTokens() {
String getLoginToken() {
try {
final string = getString(PreferencesKeyList.LOGIN_TOKENS);
if (string == null || string == "null") return null;
return LoginTokens.fromJSON(jsonDecode(string));
final string = getString(PreferencesKeyList.LOGIN_TOKEN);
return string;
} on Exception catch (e) {
print(e.toString());
return null;
}
}
Future<bool> removeKey(PreferencesKeyList key) async {
return await _sharedPreferences.remove(_PreferenceKeysName[key]);
}
Future<bool> setString(PreferencesKeyList key, String value) async {
return await _sharedPreferences.setString(_PreferenceKeysName[key], value);
}

View File

@ -23,8 +23,7 @@ void main() {
apiServerName: "192.168.1.9:3000",
apiServerUri: "/",
apiServerSecure: false,
serviceName: "ComunicFlutter",
serviceToken: "G9sZCBmb3IgVWJ1bnR1CkNvbW1lbnRbbmVdPeCkieCkrOCkq",
clientName: "ComunicFlutter",
termsOfServicesURL: "http://devweb.local/comunic/current/about.php?cgu",
));

View File

@ -10,8 +10,7 @@ void main() {
apiServerName: "api.communiquons.org",
apiServerUri: "/",
apiServerSecure: true,
serviceName: "ComunicFlutter",
serviceToken: "9KfSwmB76U9UUwjXngDG7PeYccNfy",
clientName: "ComunicFlutter",
termsOfServicesURL: "https://about.communiquons.org/about/terms/",
));

View File

@ -9,22 +9,20 @@ class Config {
final String apiServerName;
final String apiServerUri;
final bool apiServerSecure;
final String serviceName;
final String serviceToken;
final String clientName;
final String termsOfServicesURL;
const Config({
@required this.apiServerName,
@required this.apiServerUri,
@required this.apiServerSecure,
@required this.serviceName,
@required this.serviceToken,
@required this.clientName,
@required this.termsOfServicesURL,
}) : assert(apiServerName != null),
assert(apiServerUri != null),
assert(apiServerSecure != null),
assert(serviceName != null),
assert(serviceToken != null),
assert(clientName != null),
assert(termsOfServicesURL != null);
/// Get and set static configuration

View File

@ -1,23 +0,0 @@
import 'dart:convert';
/// Login tokens model
///
/// @author Pierre HUBERT
class LoginTokens {
final String tokenOne;
final String tokenTwo;
const LoginTokens(this.tokenOne, this.tokenTwo)
: assert(tokenOne != null),
assert(tokenTwo != null);
LoginTokens.fromJSON(Map<String, dynamic> json)
: tokenOne = json["token_one"],
tokenTwo = json["token_two"];
@override
String toString() {
return jsonEncode({"token_one": tokenOne, "token_two": tokenTwo});
}
}