feat(account): 同步资料保存后的登录态
This commit is contained in:
1 parent
44f6feb249
commit
ef7f4ce3b6
2 files changed
+79
No files matched your search
@@ -90,6 +90,20 @@ class AuthController extends ChangeNotifier {
|
|||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 资料保存成功后同步登录态摘要,保留当前令牌。
|
||||||
|
Future<void> applyProfile(AuthUser user) async {
|
||||||
|
final current = _session;
|
||||||
|
if (current == null) return;
|
||||||
|
await _apply(
|
||||||
|
AuthSession(
|
||||||
|
accessToken: current.accessToken,
|
||||||
|
refreshToken: current.refreshToken,
|
||||||
|
expiresIn: current.expiresIn,
|
||||||
|
user: user,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _apply(AuthSession session) async {
|
Future<void> _apply(AuthSession session) async {
|
||||||
_session = session;
|
_session = session;
|
||||||
await _store.save(session);
|
await _store.save(session);
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
// Author: fruit.com.cn
|
||||||
|
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
|
||||||
|
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||||
|
|
||||||
|
import 'package:fruitshorts/core/data/models/auth_session.dart';
|
||||||
|
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||||
|
import 'package:fruitshorts/core/data/remote/auth_api.dart';
|
||||||
|
import 'package:fruitshorts/core/data/services/auth_controller.dart';
|
||||||
|
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
||||||
|
|
||||||
|
import '../support/fake_http_adapter.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('should_update_profile_summary_and_keep_tokens_when_applied', () async {
|
||||||
|
SharedPreferencesAsyncPlatform.instance =
|
||||||
|
InMemorySharedPreferencesAsync.empty();
|
||||||
|
final adapter = FakeHttpAdapter(
|
||||||
|
(_) async => FakeHttpResponse(
|
||||||
|
jsonEncode({
|
||||||
|
'code': 0,
|
||||||
|
'message': 'ok',
|
||||||
|
'data': {
|
||||||
|
'accessToken': 'access-1',
|
||||||
|
'refreshToken': 'refresh-1',
|
||||||
|
'expiresIn': 7200,
|
||||||
|
'user': {
|
||||||
|
'uid': 'U-OLD',
|
||||||
|
'nickname': 'Old',
|
||||||
|
'avatarUrl': 'old.png',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
late AuthController auth;
|
||||||
|
final client = ApiClient(
|
||||||
|
baseUrl: 'http://127.0.0.1:8081',
|
||||||
|
adapter: adapter,
|
||||||
|
accessTokenReader: () => auth.accessToken,
|
||||||
|
tokenRefresher: () => auth.refresh(),
|
||||||
|
);
|
||||||
|
auth = AuthController(AuthApi(client), AuthSessionStore());
|
||||||
|
await auth.register(email: 'a@b.com', password: 'abcd1234');
|
||||||
|
|
||||||
|
await auth.applyProfile(
|
||||||
|
const AuthUser(
|
||||||
|
uid: 'U-K19',
|
||||||
|
nickname: 'Nadia',
|
||||||
|
avatarUrl: 'avatar-k19.png',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(auth.accessToken, 'access-1');
|
||||||
|
expect(auth.user?.uid, 'U-K19');
|
||||||
|
expect(auth.user?.nickname, 'Nadia');
|
||||||
|
expect(auth.user?.avatarUrl, 'avatar-k19.png');
|
||||||
|
final saved = await AuthSessionStore().read();
|
||||||
|
expect(saved?.refreshToken, 'refresh-1');
|
||||||
|
expect(saved?.user.nickname, 'Nadia');
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in new issue
Block a user