delete ab cache and show login button on http 401

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-08-10 17:16:53 +08:00
parent bc0a5bf6e1
commit eb8231af14
2 changed files with 32 additions and 11 deletions

View File

@ -55,13 +55,24 @@ class AbModel {
abError.value = ""; abError.value = "";
} }
final api = "${await bind.mainGetApiServer()}/api/ab"; final api = "${await bind.mainGetApiServer()}/api/ab";
int? statusCode;
try { try {
var authHeaders = getHttpHeaders(); var authHeaders = getHttpHeaders();
authHeaders['Content-Type'] = "application/json"; authHeaders['Content-Type'] = "application/json";
authHeaders['Accept-Encoding'] = "gzip"; authHeaders['Accept-Encoding'] = "gzip";
final resp = await http.get(Uri.parse(api), headers: authHeaders); final resp = await http.get(Uri.parse(api), headers: authHeaders);
statusCode = resp.statusCode;
if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") { if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") {
Map<String, dynamic> json = jsonDecode(utf8.decode(resp.bodyBytes)); Map<String, dynamic> json;
try {
json = jsonDecode(utf8.decode(resp.bodyBytes));
} catch (e) {
if (resp.statusCode != 200) {
BotToast.showText(
contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
}
rethrow;
}
if (json.containsKey('error')) { if (json.containsKey('error')) {
abError.value = json['error']; abError.value = json['error'];
} else if (json.containsKey('data')) { } else if (json.containsKey('data')) {
@ -81,28 +92,33 @@ class AbModel {
peers.add(Peer.fromJson(peer)); peers.add(Peer.fromJson(peer));
} }
} }
save(); // save on success
} }
} }
} }
} catch (err) { } catch (err) {
reset();
abError.value = err.toString(); abError.value = err.toString();
} finally { } finally {
abLoading.value = false; abLoading.value = false;
initialized = true; initialized = true;
sync_all_from_recent = true; sync_all_from_recent = true;
_timerCounter = 0; _timerCounter = 0;
save(); if (abError.isNotEmpty) {
if (statusCode == 401) {
gFFI.userModel.reset(clearAbCache: true);
} else {
reset(clearCache: false);
}
}
} }
} }
Future<void> reset() async { Future<void> reset({bool clearCache = false}) async {
abError.value = '';
await bind.mainSetLocalOption(key: "selected-tags", value: ''); await bind.mainSetLocalOption(key: "selected-tags", value: '');
tags.clear(); tags.clear();
peers.clear(); peers.clear();
initialized = false; initialized = false;
await bind.mainClearAb(); if (clearCache) await bind.mainClearAb();
} }
void addId(String id, String alias, List<dynamic> tags) { void addId(String id, String alias, List<dynamic> tags) {
@ -181,12 +197,12 @@ class AbModel {
try { try {
await http.Client().send(request); await http.Client().send(request);
// await pullAb(quiet: true); // await pullAb(quiet: true);
save(); // save on success
} catch (e) { } catch (e) {
BotToast.showText(contentColor: Colors.red, text: e.toString()); BotToast.showText(contentColor: Colors.red, text: e.toString());
} finally { } finally {
sync_all_from_recent = true; sync_all_from_recent = true;
_timerCounter = 0; _timerCounter = 0;
save();
} }
} }

View File

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/hbbs/hbbs.dart'; import 'package:flutter_hbb/common/hbbs/hbbs.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@ -44,7 +45,7 @@ class UserModel {
refreshingUser = false; refreshingUser = false;
final status = response.statusCode; final status = response.statusCode;
if (status == 401 || status == 400) { if (status == 401 || status == 400) {
reset(); reset(clearAbCache: status == 401);
return; return;
} }
final data = json.decode(utf8.decode(response.bodyBytes)); final data = json.decode(utf8.decode(response.bodyBytes));
@ -83,10 +84,10 @@ class UserModel {
} }
} }
Future<void> reset() async { Future<void> reset({bool clearAbCache = false}) async {
await bind.mainSetLocalOption(key: 'access_token', value: ''); await bind.mainSetLocalOption(key: 'access_token', value: '');
await bind.mainSetLocalOption(key: 'user_info', value: ''); await bind.mainSetLocalOption(key: 'user_info', value: '');
await gFFI.abModel.reset(); await gFFI.abModel.reset(clearCache: clearAbCache);
await gFFI.groupModel.reset(); await gFFI.groupModel.reset();
userName.value = ''; userName.value = '';
} }
@ -118,7 +119,7 @@ class UserModel {
} catch (e) { } catch (e) {
debugPrint("request /api/logout failed: err=$e"); debugPrint("request /api/logout failed: err=$e");
} finally { } finally {
await reset(); await reset(clearAbCache: true);
gFFI.dialogManager.dismissByTag(tag); gFFI.dialogManager.dismissByTag(tag);
} }
} }
@ -135,6 +136,10 @@ class UserModel {
body = jsonDecode(utf8.decode(resp.bodyBytes)); body = jsonDecode(utf8.decode(resp.bodyBytes));
} catch (e) { } catch (e) {
debugPrint("login: jsonDecode resp body failed: ${e.toString()}"); debugPrint("login: jsonDecode resp body failed: ${e.toString()}");
if (resp.statusCode != 200) {
BotToast.showText(
contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
}
rethrow; rethrow;
} }
if (resp.statusCode != 200) { if (resp.statusCode != 200) {