Better client/login/authState handling
All checks were successful
Build Docker and Deploy / Build Docker Container (push) Successful in 2m58s
Build Docker and Deploy / Deploy to Server (push) Successful in 32s

This commit is contained in:
2024-06-02 17:09:57 +02:00
parent ec506a7f9e
commit ac299ec7ba
17 changed files with 496 additions and 401 deletions

View File

@@ -0,0 +1,157 @@
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/api/api_exception.dart';
import 'package:simplecloudnotifier/models/client.dart';
import 'package:simplecloudnotifier/models/user.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/token_source.dart';
class AppAuth extends ChangeNotifier implements TokenSource {
String? _clientID;
String? _userID;
String? _tokenAdmin;
String? _tokenSend;
User? _user;
Client? _client;
String? get userID => _userID;
String? get tokenAdmin => _tokenAdmin;
String? get tokenSend => _tokenSend;
static AppAuth? _singleton = AppAuth._internal();
factory AppAuth() {
return _singleton ?? (_singleton = AppAuth._internal());
}
AppAuth._internal() {
load();
}
bool isAuth() {
return _userID != null && _tokenAdmin != null;
}
void set(User user, Client client, String tokenAdmin, String tokenSend) {
_client = client;
_user = user;
_userID = user.userID;
_clientID = client.clientID;
_tokenAdmin = tokenAdmin;
_tokenSend = tokenSend;
notifyListeners();
}
void setClientAndClientID(Client client) {
_client = client;
_clientID = client.clientID;
notifyListeners();
}
void clear() {
_clientID = null;
_userID = null;
_tokenAdmin = null;
_tokenSend = null;
_client = null;
_user = null;
notifyListeners();
}
void load() {
final uid = Globals().sharedPrefs.getString('auth.userid');
final cid = Globals().sharedPrefs.getString('auth.clientid');
final toka = Globals().sharedPrefs.getString('auth.tokenadmin');
final toks = Globals().sharedPrefs.getString('auth.tokensend');
if (uid == null || toka == null || toks == null || cid == null) {
clear();
return;
}
_clientID = cid;
_userID = uid;
_tokenAdmin = toka;
_tokenSend = toks;
_client = null;
_user = null;
notifyListeners();
}
Future<void> save() async {
final prefs = await SharedPreferences.getInstance();
if (_clientID == null || _userID == null || _tokenAdmin == null || _tokenSend == null) {
await prefs.remove('auth.userid');
await prefs.remove('auth.tokenadmin');
await prefs.remove('auth.tokensend');
} else {
await prefs.setString('auth.userid', _userID!);
await prefs.setString('auth.clientid', _clientID!);
await prefs.setString('auth.tokenadmin', _tokenAdmin!);
await prefs.setString('auth.tokensend', _tokenSend!);
}
}
Future<User> loadUser({bool force = false}) async {
if (!force && _user != null && _user!.userID == _userID) {
return _user!;
}
if (_userID == null || _tokenAdmin == null) {
throw Exception('Not authenticated');
}
final user = await APIClient.getUser(this, _userID!);
_user = user;
notifyListeners();
await save();
return user;
}
Future<Client?> loadClient({bool force = false}) async {
if (!force && _client != null && _client!.clientID == _clientID) {
return _client!;
}
if (_clientID == null || _tokenAdmin == null) {
throw Exception('Not authenticated');
}
try {
final client = await APIClient.getClient(this, _clientID!);
_client = client;
notifyListeners();
await save();
return client;
} on APIException catch (_) {
_client = null;
notifyListeners();
return null;
} catch (exc) {
_client = null;
rethrow;
}
}
@override
String getToken() {
return _tokenAdmin!;
}
@override
String getUserID() {
return _userID!;
}
}

View File

@@ -0,0 +1,21 @@
abstract class TokenSource {
String getToken();
String getUserID();
}
class DirectTokenSource implements TokenSource {
final String _userID;
final String _token;
DirectTokenSource(this._userID, this._token);
@override
String getUserID() {
return _userID;
}
@override
String getToken() {
return _token;
}
}

View File

@@ -1,117 +0,0 @@
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/models/client.dart';
import 'package:simplecloudnotifier/models/key_token_auth.dart';
import 'package:simplecloudnotifier/models/user.dart';
import 'package:simplecloudnotifier/state/globals.dart';
class UserAccount extends ChangeNotifier {
User? _user;
User? get user => _user;
Client? _client;
Client? get client => _client;
KeyTokenAuth? _auth;
KeyTokenAuth? get auth => _auth;
static UserAccount? _singleton = UserAccount._internal();
factory UserAccount() {
return _singleton ?? (_singleton = UserAccount._internal());
}
UserAccount._internal() {
load();
}
void setToken(KeyTokenAuth auth) {
_auth = auth;
_user = null;
notifyListeners();
}
void clearToken() {
_auth = null;
_user = null;
notifyListeners();
}
void setUser(User user) {
_user = user;
notifyListeners();
}
void clearUser() {
_user = null;
notifyListeners();
}
void setClient(Client client) {
_client = client;
notifyListeners();
}
void clearClient() {
_client = null;
notifyListeners();
}
void set(User user, Client client, KeyTokenAuth auth) {
_client = client;
_user = user;
_auth = auth;
notifyListeners();
}
void clear() {
_client = null;
_user = null;
_auth = null;
notifyListeners();
}
void load() {
final uid = Globals().sharedPrefs.getString('auth.userid');
final toka = Globals().sharedPrefs.getString('auth.tokenadmin');
final toks = Globals().sharedPrefs.getString('auth.tokensend');
if (uid != null && toka != null && toks != null) {
setToken(KeyTokenAuth(userId: uid, tokenAdmin: toka, tokenSend: toks));
} else {
clearToken();
}
}
Future<void> save() async {
final prefs = await SharedPreferences.getInstance();
if (_auth == null) {
await prefs.remove('auth.userid');
await prefs.remove('auth.tokenadmin');
await prefs.remove('auth.tokensend');
} else {
await prefs.setString('auth.userid', _auth!.userId);
await prefs.setString('auth.tokenadmin', _auth!.tokenAdmin);
await prefs.setString('auth.tokensend', _auth!.tokenSend);
}
}
Future<User> loadUser(bool force) async {
if (!force && _user != null) {
return _user!;
}
if (_auth == null) {
throw Exception('Not authenticated');
}
final user = await APIClient.getUser(_auth!, _auth!.userId);
setUser(user);
await save();
return user;
}
}