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) {
return _buildShowError(gFFI.abModel.abError.value);
}
if (gFFI.userModel.fromServer.isFalse) {
return Offstage();
}
return isDesktop
? _buildAddressBookDesktop()
: _buildAddressBookMobile();

View File

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

View File

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