Files
keyboard/Shared/KBUserSessionManager.m

172 lines
5.3 KiB
Mathematica
Raw Normal View History

2025-12-02 18:29:04 +08:00
//
// KBUserSessionManager.m
// keyBoard
//
// Created by Mac on 2025/12/1.
//
#import "KBUserSessionManager.h"
#import "KBAuthManager.h"
#import "KBUser.h"
#import "KBConfig.h"
#import <MJExtension/MJExtension.h>
2025-12-03 14:30:02 +08:00
#import "KBLoginVC.h"
2025-12-02 18:29:04 +08:00
/// App Group key
static NSString * const kKBSessionUserStoreKey = @"KBSession.currentUser";
/// key
static NSString * const kKBSessionInstallFlagKey = @"KBSession.installInitialized";
@interface KBUserSessionManager ()
@property (nonatomic, strong) NSUserDefaults *defaults;
@property (atomic, strong, readwrite, nullable) KBUser *currentUser;
@property (nonatomic, assign) BOOL didBootstrap;
@end
@implementation KBUserSessionManager
+ (instancetype)shared {
static KBUserSessionManager *m;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
m = [KBUserSessionManager new];
});
return m;
}
- (instancetype)init {
if (self = [super init]) {
// App Group UserDefaultsApp
NSUserDefaults *ud = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
_defaults = ud ?: [NSUserDefaults standardUserDefaults];
// token signOut
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_onAuthChanged:)
name:KBAuthChangedNotification
object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Public
- (void)bootstrapIfNeeded {
if (self.didBootstrap) return;
self.didBootstrap = YES;
2025-12-03 14:30:02 +08:00
// 使 App standardUserDefaults
// Keychain token
NSUserDefaults *installDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasInitializedThisInstall = [installDefaults boolForKey:kKBSessionInstallFlagKey];
2025-12-02 18:29:04 +08:00
KBAuthManager *auth = [KBAuthManager shared]; // reloadFromKeychain
if (!hasInitializedThisInstall) {
//
2025-12-03 14:30:02 +08:00
[installDefaults setBool:YES forKey:kKBSessionInstallFlagKey];
[installDefaults synchronize];
2025-12-02 18:29:04 +08:00
// Keychain token
if (auth.current.accessToken.length > 0) {
[auth signOut];
}
//
[self.defaults removeObjectForKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
self.currentUser = nil;
} else {
//
[self p_loadUserFromStore];
}
}
- (BOOL)isLoggedIn {
return [[KBAuthManager shared] isLoggedIn];
}
- (NSString *)accessToken {
return [KBAuthManager shared].current.accessToken;
}
- (void)handleLoginSuccessWithUser:(KBUser *)user {
if (!user) return;
// Keychain KBAuthManager
NSString *token = user.token;
if (token.length > 0) {
[[KBAuthManager shared] saveAccessToken:token
2025-12-02 20:33:17 +08:00
refreshToken:nil
expiryDate:nil
userIdentifier:nil];
2025-12-02 18:29:04 +08:00
}
// App/使
self.currentUser = user;
[self p_saveUserToStore:user];
}
- (void)logout {
// Keychain
[[KBAuthManager shared] signOut];
//
self.currentUser = nil;
[self.defaults removeObjectForKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
}
#pragma mark - Private
- (void)p_loadUserFromStore {
id obj = [self.defaults objectForKey:kKBSessionUserStoreKey];
if (![obj isKindOfClass:[NSDictionary class]]) {
self.currentUser = nil;
return;
}
KBUser *user = [KBUser mj_objectWithKeyValues:(NSDictionary *)obj];
self.currentUser = user;
}
- (void)p_saveUserToStore:(KBUser *)user {
if (!user) {
[self.defaults removeObjectForKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
return;
}
NSDictionary *dict = [user mj_keyValues];
if (dict) {
[self.defaults setObject:dict forKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
}
}
- (void)_onAuthChanged:(NSNotification *)note {
2025-12-03 12:55:51 +08:00
// KBAuthManager +shared +shared
// 使
KBAuthManager *auth = nil;
if ([note.object isKindOfClass:[KBAuthManager class]]) {
auth = (KBAuthManager *)note.object;
} else {
auth = [KBAuthManager shared];
}
2025-12-02 18:29:04 +08:00
// token currentUser
2025-12-03 12:55:51 +08:00
if (![auth isLoggedIn]) {
2025-12-02 18:29:04 +08:00
self.currentUser = nil;
[self.defaults removeObjectForKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
}
}
2025-12-03 14:30:02 +08:00
- (void)goLoginVC{
KBLoginVC *vc = [[KBLoginVC alloc] init];
[KB_CURRENT_NAV pushViewController:vc animated:true];
}
2025-12-02 18:29:04 +08:00
@end