mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-06 10:07:45 +08:00
feat: use cookie to store jwt token, better performance
This commit is contained in:
@@ -2,8 +2,6 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:quiver/strings.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:ui/providers/server_response.dart';
|
||||
|
||||
class APIs {
|
||||
@@ -26,6 +24,7 @@ class APIs {
|
||||
static final delDownloadClientUrl = "$_baseUrl/api/v1/downloader/del/";
|
||||
static final storageUrl = "$_baseUrl/api/v1/storage/";
|
||||
static final loginUrl = "$_baseUrl/api/login";
|
||||
static final logoutUrl = "$_baseUrl/api/v1/setting/logout";
|
||||
static final loginSettingUrl = "$_baseUrl/api/v1/setting/auth";
|
||||
static final activityUrl = "$_baseUrl/api/v1/activity/";
|
||||
static final activityMediaUrl = "$_baseUrl/api/v1/activity/media/";
|
||||
@@ -49,53 +48,20 @@ class APIs {
|
||||
return "http://127.0.0.1:8080";
|
||||
}
|
||||
|
||||
static Dio? gDio;
|
||||
static Map<String, String> authHeaders = {};
|
||||
|
||||
static Future<bool> isLoggedIn() async {
|
||||
return isNotBlank(await getToken());
|
||||
}
|
||||
|
||||
static Future<String> getToken() async {
|
||||
var token = authHeaders["Authorization"];
|
||||
if (isBlank(token)) {
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var t = prefs.getString("token");
|
||||
if (isNotBlank(t)) {
|
||||
authHeaders["Authorization"] = t!;
|
||||
token = t;
|
||||
}
|
||||
}
|
||||
return token ?? "";
|
||||
}
|
||||
|
||||
static Future<Dio> getDio() async {
|
||||
if (gDio != null) {
|
||||
return gDio!;
|
||||
}
|
||||
var token = await getToken();
|
||||
|
||||
static Dio getDio() {
|
||||
var dio = Dio();
|
||||
dio.interceptors.add(InterceptorsWrapper(
|
||||
onRequest: (options, handler) {
|
||||
options.headers['Authorization'] = token;
|
||||
return handler.next(options);
|
||||
},
|
||||
onError: (error, handler) {
|
||||
if (error.response?.statusCode != null &&
|
||||
error.response?.statusCode! == 403) {
|
||||
final context = navigatorKey.currentContext;
|
||||
if (context != null) {
|
||||
context.go('/login');
|
||||
gDio = null;
|
||||
}
|
||||
}
|
||||
return handler.next(error);
|
||||
},
|
||||
));
|
||||
if (isNotBlank(token)) {
|
||||
gDio = dio;
|
||||
}
|
||||
return dio;
|
||||
}
|
||||
|
||||
@@ -108,9 +74,19 @@ class APIs {
|
||||
if (sp.code != 0) {
|
||||
throw sp.message;
|
||||
}
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
var t = sp.data["token"];
|
||||
authHeaders["Authorization"] = "Bearer $t";
|
||||
prefs.setString("token", "Bearer $t");
|
||||
}
|
||||
|
||||
static Future<void> logout() async {
|
||||
var resp = await getDio().get(APIs.logoutUrl);
|
||||
|
||||
var sp = ServerResponse.fromJson(resp.data);
|
||||
|
||||
if (sp.code != 0) {
|
||||
throw sp.message;
|
||||
}
|
||||
final context = navigatorKey.currentContext;
|
||||
if (context != null) {
|
||||
context.go('/login');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user