fix user login state

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-06-21 11:32:50 +08:00
parent 12b6faffa2
commit bf1cf29cb9
3 changed files with 45 additions and 11 deletions

View File

@ -54,6 +54,9 @@ class _AddressBookState extends State<AddressBook> {
if (gFFI.abModel.abError.isNotEmpty) { if (gFFI.abModel.abError.isNotEmpty) {
return _buildShowError(gFFI.abModel.abError.value); return _buildShowError(gFFI.abModel.abError.value);
} }
if (gFFI.userModel.fromServer.isFalse) {
return Offstage();
}
return isDesktop return isDesktop
? _buildAddressBookDesktop() ? _buildAddressBookDesktop()
: _buildAddressBookMobile(); : _buildAddressBookMobile();

View File

@ -96,7 +96,7 @@ class ConfigOP {
class WidgetOP extends StatefulWidget { class WidgetOP extends StatefulWidget {
final ConfigOP config; final ConfigOP config;
final RxString curOP; final RxString curOP;
final Function(String) cbLogin; final Function(Map<String, dynamic>) cbLogin;
const WidgetOP({ const WidgetOP({
Key? key, Key? key,
required this.config, required this.config,
@ -153,9 +153,8 @@ class _WidgetOPState extends State<WidgetOP> {
} }
if (authBody != null) { if (authBody != null) {
_updateTimer?.cancel(); _updateTimer?.cancel();
final String username = authBody['user']['name'];
widget.curOP.value = ''; widget.curOP.value = '';
widget.cbLogin(username); widget.cbLogin(authBody as Map<String, dynamic>);
} }
setState(() { setState(() {
@ -255,7 +254,7 @@ class _WidgetOPState extends State<WidgetOP> {
class LoginWidgetOP extends StatelessWidget { class LoginWidgetOP extends StatelessWidget {
final List<ConfigOP> ops; final List<ConfigOP> ops;
final RxString curOP; final RxString curOP;
final Function(String) cbLogin; final Function(Map<String, dynamic>) cbLogin;
LoginWidgetOP({ LoginWidgetOP({
Key? key, Key? key,
@ -368,7 +367,8 @@ const kAuthReqTypeOidc = 'oidc/';
/// common login dialog for desktop /// common login dialog for desktop
/// call this directly /// call this directly
Future<bool?> loginDialog() async { Future<bool?> loginDialog() async {
var username = TextEditingController(); var username =
TextEditingController(text: UserModel.getLocalUserInfo()?['name'] ?? '');
var password = TextEditingController(); var password = TextEditingController();
final userFocusNode = FocusNode()..requestFocus(); final userFocusNode = FocusNode()..requestFocus();
Timer(Duration(milliseconds: 100), () => userFocusNode..requestFocus()); Timer(Duration(milliseconds: 100), () => userFocusNode..requestFocus());
@ -480,8 +480,17 @@ Future<bool?> loginDialog() async {
.where((op) => oidcOptions.contains(op.op.toLowerCase())) .where((op) => oidcOptions.contains(op.op.toLowerCase()))
.toList(), .toList(),
curOP: curOP, curOP: curOP,
cbLogin: (String username) { cbLogin: (Map<String, dynamic> authBody) {
gFFI.userModel.userName.value = username; try {
final loginResp =
gFFI.userModel.getLoginResponseFromAuthBody(authBody);
if (loginResp.access_token != null) {
bind.mainSetLocalOption(
key: 'access_token', value: loginResp.access_token!);
}
} catch (e) {
debugPrint('Failed too parse oidc login body: "$authBody"');
}
close(true); close(true);
}, },
), ),
@ -567,6 +576,7 @@ Future<bool?> verificationCodeDialog(UserPayload? user) async {
if (resp.access_token != null) { if (resp.access_token != null) {
await bind.mainSetLocalOption( await bind.mainSetLocalOption(
key: 'access_token', value: resp.access_token!); key: 'access_token', value: resp.access_token!);
gFFI.userModel.refreshCurrentUser();
close(true); close(true);
return; return;
} }

View File

@ -14,6 +14,7 @@ import 'platform_model.dart';
class UserModel { class UserModel {
final RxString userName = ''.obs; final RxString userName = ''.obs;
final RxBool isAdmin = false.obs; final RxBool isAdmin = false.obs;
final RxBool fromServer = false.obs;
WeakReference<FFI> parent; WeakReference<FFI> parent;
UserModel(this.parent); UserModel(this.parent);
@ -48,7 +49,7 @@ class UserModel {
} }
final user = UserPayload.fromJson(data); final user = UserPayload.fromJson(data);
await _parseAndUpdateUser(user); _parseAndUpdateUser(user);
} catch (e) { } catch (e) {
print('Failed to refreshCurrentUser: $e'); print('Failed to refreshCurrentUser: $e');
} finally { } finally {
@ -56,6 +57,22 @@ class UserModel {
} }
} }
static Map<String, dynamic>? getLocalUserInfo() {
try {
return json.decode(bind.mainGetLocalOption(key: 'user_info'));
} catch (e) {
print('Failed to get local user info: $e');
}
return null;
}
_updateLocalUserInfo() {
final userInfo = getLocalUserInfo();
if (userInfo != null) {
userName.value = userInfo['name'];
}
}
Future<void> reset() async { Future<void> reset() async {
await bind.mainSetLocalOption(key: 'access_token', value: ''); await bind.mainSetLocalOption(key: 'access_token', value: '');
await gFFI.abModel.reset(); await gFFI.abModel.reset();
@ -64,9 +81,10 @@ class UserModel {
gFFI.peerTabModel.check_dynamic_tabs(); gFFI.peerTabModel.check_dynamic_tabs();
} }
Future<void> _parseAndUpdateUser(UserPayload user) async { _parseAndUpdateUser(UserPayload user) {
userName.value = user.name; userName.value = user.name;
isAdmin.value = user.isAdmin; isAdmin.value = user.isAdmin;
fromServer.value = true;
} }
Future<void> _updateOtherModels() async { Future<void> _updateOtherModels() async {
@ -110,11 +128,14 @@ class UserModel {
print("login: jsonDecode resp body failed: ${e.toString()}"); print("login: jsonDecode resp body failed: ${e.toString()}");
rethrow; rethrow;
} }
if (resp.statusCode != 200) { if (resp.statusCode != 200) {
throw RequestException(resp.statusCode, body['error'] ?? ''); throw RequestException(resp.statusCode, body['error'] ?? '');
} }
return getLoginResponseFromAuthBody(body);
}
LoginResponse getLoginResponseFromAuthBody(Map<String, dynamic> body) {
final LoginResponse loginResponse; final LoginResponse loginResponse;
try { try {
loginResponse = LoginResponse.fromJson(body); loginResponse = LoginResponse.fromJson(body);
@ -124,7 +145,7 @@ class UserModel {
} }
if (loginResponse.user != null) { if (loginResponse.user != null) {
await _parseAndUpdateUser(loginResponse.user!); _parseAndUpdateUser(loginResponse.user!);
} }
return loginResponse; return loginResponse;