Files
zh-chat-flutter/lib/services/auth_service.dart

47 lines
1.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
class AuthService extends GetxService {
static AuthService get to => Get.find<AuthService>();
final _box = GetStorage();
/// 供外部使用的,判断是否登录的状态
get isUserLogin => isLogin.value;
/// 登录状态记录,可监听的这样ever才能监听到
final RxBool isLogin = false.obs;
/// 登录的token供请求时调用载入内存是为了每次使用的时候不需要从磁盘获取
late String userToken = '';
/// 获取存储的token这个可以做到持久化存储
String get _userToken => _box.read('userToken') ?? '';
@override
void onInit() {
super.onInit();
if (_userToken.isNotEmpty) {
isLogin.value = true;
userToken = _userToken;
}
// ever(_isLogin, (_) {
// if (_ == true) {
// Get.offAllNamed(AppRoutes.app);
// } else {
// Get.offAllNamed(AuthRoutes.index);
// }
// });
}
Future<bool> login(String address) async {
_box.write('userToken', address);
userToken = address;
isLogin.value = true;
return true;
}
}