mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 12:59:21 +00:00
Automatically get current user ID
This commit is contained in:
parent
acf2b0d8e9
commit
5eac36c9a2
@ -10,10 +10,6 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
/// @author Pierre HUBERT
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
class AccountCredentialsHelper {
|
class AccountCredentialsHelper {
|
||||||
/// Checkout whether current user is signed in or not
|
|
||||||
Future<bool> signedIn() async {
|
|
||||||
return await get() != null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set new login tokens
|
/// Set new login tokens
|
||||||
Future<void> set(LoginTokens tokens) async {
|
Future<void> set(LoginTokens tokens) async {
|
||||||
|
@ -3,6 +3,7 @@ import 'package:comunic/helpers/api_helper.dart';
|
|||||||
import 'package:comunic/models/api_request.dart';
|
import 'package:comunic/models/api_request.dart';
|
||||||
import 'package:comunic/models/authentication_details.dart';
|
import 'package:comunic/models/authentication_details.dart';
|
||||||
import 'package:comunic/models/login_tokens.dart';
|
import 'package:comunic/models/login_tokens.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
/// Account helper
|
/// Account helper
|
||||||
///
|
///
|
||||||
@ -16,6 +17,23 @@ enum AuthResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class AccountHelper {
|
class AccountHelper {
|
||||||
|
static const _USER_ID_PREFERENCE_NAME = "user_id";
|
||||||
|
|
||||||
|
// Current user ID
|
||||||
|
static int _currentUserID = -1;
|
||||||
|
|
||||||
|
/// Checkout whether current user is signed in or not
|
||||||
|
///
|
||||||
|
/// Warning : This method MUST BE CALLED AT LEAST ONCE AFTER APP START !!!
|
||||||
|
Future<bool> signedIn() async {
|
||||||
|
bool signedIn = await AccountCredentialsHelper().get() != null;
|
||||||
|
|
||||||
|
// Load current user ID for later use
|
||||||
|
if (signedIn && _currentUserID == -1) await _loadCurrentUserID();
|
||||||
|
|
||||||
|
return signedIn;
|
||||||
|
}
|
||||||
|
|
||||||
/// Sign in user
|
/// Sign in user
|
||||||
Future<AuthResult> signIn(AuthenticationDetails auth) async {
|
Future<AuthResult> signIn(AuthenticationDetails auth) async {
|
||||||
final request = APIRequest(uri: "account/login");
|
final request = APIRequest(uri: "account/login");
|
||||||
@ -31,17 +49,53 @@ class AccountHelper {
|
|||||||
return AuthResult.TOO_MANY_ATTEMPTS;
|
return AuthResult.TOO_MANY_ATTEMPTS;
|
||||||
else if (response.code != 200) return AuthResult.NETWORK_ERROR;
|
else if (response.code != 200) return AuthResult.NETWORK_ERROR;
|
||||||
|
|
||||||
//Save login tokens
|
// Save login tokens
|
||||||
final tokensObj = response.getObject()["tokens"];
|
final tokensObj = response.getObject()["tokens"];
|
||||||
await AccountCredentialsHelper()
|
await AccountCredentialsHelper()
|
||||||
.set(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
|
.set(LoginTokens(tokensObj["token1"], tokensObj["token2"]));
|
||||||
|
|
||||||
|
// Get current user ID
|
||||||
|
final userID = await _downloadCurrentUserID();
|
||||||
|
if (userID == null) {
|
||||||
|
await signOut(); // We can not stay signed in without current user ID
|
||||||
|
return AuthResult.NETWORK_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save current user ID
|
||||||
|
final preferences = await SharedPreferences.getInstance();
|
||||||
|
await preferences.setInt(_USER_ID_PREFERENCE_NAME, userID);
|
||||||
|
_currentUserID = userID;
|
||||||
|
|
||||||
return AuthResult.SUCCESS;
|
return AuthResult.SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sign out user
|
/// Sign out user
|
||||||
Future<void> signOut() async {
|
Future<void> signOut() async {
|
||||||
await AccountCredentialsHelper().set(null);
|
await AccountCredentialsHelper().set(null);
|
||||||
|
_currentUserID = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get current user ID from the server
|
||||||
|
Future<int> _downloadCurrentUserID() async {
|
||||||
|
final response = await APIRequest(
|
||||||
|
uri: "user/getCurrentUserID",
|
||||||
|
needLogin: true,
|
||||||
|
).exec();
|
||||||
|
|
||||||
|
if (response.code != 200) return null;
|
||||||
|
|
||||||
|
return response.getObject()["userID"];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the ID of the currently signed in user
|
||||||
|
Future<void> _loadCurrentUserID() async {
|
||||||
|
final preferences = await SharedPreferences.getInstance();
|
||||||
|
_currentUserID = preferences.getInt(_USER_ID_PREFERENCE_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the ID of the currently signed in user
|
||||||
|
static int getCurrentUserID() {
|
||||||
|
if (_currentUserID == -1) throw "Current user ID has not been loaded yet!";
|
||||||
|
return _currentUserID;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import 'package:comunic/helpers/account_credentials_helper.dart';
|
import 'package:comunic/helpers/account_helper.dart';
|
||||||
import 'package:comunic/models/config.dart';
|
import 'package:comunic/models/config.dart';
|
||||||
import 'package:comunic/ui/routes/home_route.dart';
|
import 'package:comunic/ui/routes/home_route.dart';
|
||||||
import 'package:comunic/ui/routes/login_route.dart';
|
import 'package:comunic/ui/routes/login_route.dart';
|
||||||
@ -45,7 +45,7 @@ class _ComunicApplicationHomeState extends State<ComunicApplicationHome> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
|
||||||
AccountCredentialsHelper().signedIn().then((v) {
|
AccountHelper().signedIn().then((v) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_signedIn = v;
|
_signedIn = v;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user