delete ab cache and show login button on http 401
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
parent
bc0a5bf6e1
commit
eb8231af14
@ -55,13 +55,24 @@ class AbModel {
|
||||
abError.value = "";
|
||||
}
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab";
|
||||
int? statusCode;
|
||||
try {
|
||||
var authHeaders = getHttpHeaders();
|
||||
authHeaders['Content-Type'] = "application/json";
|
||||
authHeaders['Accept-Encoding'] = "gzip";
|
||||
final resp = await http.get(Uri.parse(api), headers: authHeaders);
|
||||
statusCode = resp.statusCode;
|
||||
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')) {
|
||||
abError.value = json['error'];
|
||||
} else if (json.containsKey('data')) {
|
||||
@ -81,28 +92,33 @@ class AbModel {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
save(); // save on success
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
reset();
|
||||
abError.value = err.toString();
|
||||
} finally {
|
||||
abLoading.value = false;
|
||||
initialized = true;
|
||||
sync_all_from_recent = true;
|
||||
_timerCounter = 0;
|
||||
save();
|
||||
if (abError.isNotEmpty) {
|
||||
if (statusCode == 401) {
|
||||
gFFI.userModel.reset(clearAbCache: true);
|
||||
} else {
|
||||
reset(clearCache: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> reset() async {
|
||||
abError.value = '';
|
||||
Future<void> reset({bool clearCache = false}) async {
|
||||
await bind.mainSetLocalOption(key: "selected-tags", value: '');
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
initialized = false;
|
||||
await bind.mainClearAb();
|
||||
if (clearCache) await bind.mainClearAb();
|
||||
}
|
||||
|
||||
void addId(String id, String alias, List<dynamic> tags) {
|
||||
@ -181,12 +197,12 @@ class AbModel {
|
||||
try {
|
||||
await http.Client().send(request);
|
||||
// await pullAb(quiet: true);
|
||||
save(); // save on success
|
||||
} catch (e) {
|
||||
BotToast.showText(contentColor: Colors.red, text: e.toString());
|
||||
} finally {
|
||||
sync_all_from_recent = true;
|
||||
_timerCounter = 0;
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:bot_toast/bot_toast.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
|
||||
import 'package:get/get.dart';
|
||||
@ -44,7 +45,7 @@ class UserModel {
|
||||
refreshingUser = false;
|
||||
final status = response.statusCode;
|
||||
if (status == 401 || status == 400) {
|
||||
reset();
|
||||
reset(clearAbCache: status == 401);
|
||||
return;
|
||||
}
|
||||
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: 'user_info', value: '');
|
||||
await gFFI.abModel.reset();
|
||||
await gFFI.abModel.reset(clearCache: clearAbCache);
|
||||
await gFFI.groupModel.reset();
|
||||
userName.value = '';
|
||||
}
|
||||
@ -118,7 +119,7 @@ class UserModel {
|
||||
} catch (e) {
|
||||
debugPrint("request /api/logout failed: err=$e");
|
||||
} finally {
|
||||
await reset();
|
||||
await reset(clearAbCache: true);
|
||||
gFFI.dialogManager.dismissByTag(tag);
|
||||
}
|
||||
}
|
||||
@ -135,6 +136,10 @@ class UserModel {
|
||||
body = jsonDecode(utf8.decode(resp.bodyBytes));
|
||||
} catch (e) {
|
||||
debugPrint("login: jsonDecode resp body failed: ${e.toString()}");
|
||||
if (resp.statusCode != 200) {
|
||||
BotToast.showText(
|
||||
contentColor: Colors.red, text: 'HTTP ${resp.statusCode}');
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
if (resp.statusCode != 200) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user