添加token管理
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#import "AppleSignInManager.h"
|
||||
#import <AuthenticationServices/AuthenticationServices.h>
|
||||
#import <Masonry/Masonry.h>
|
||||
#import "KBAuthManager.h"
|
||||
|
||||
@interface LoginViewController ()
|
||||
// 容器视图(用于居中摆放内容)
|
||||
@@ -147,6 +148,12 @@
|
||||
if (code.length) info[@"authorizationCode"] = code;
|
||||
}
|
||||
|
||||
// 将示例中的 identityToken 暂存为访问令牌(实际项目应调用服务端换取业务 token)
|
||||
NSString *accessToken = info[@"identityToken"];
|
||||
NSString *uid = info[@"userIdentifier"]; // 不变
|
||||
if (accessToken.length > 0) {
|
||||
[[KBAuthManager shared] saveAccessToken:accessToken refreshToken:nil expiryDate:nil userIdentifier:uid];
|
||||
}
|
||||
if (selfStrong.onLoginSuccess) selfStrong.onLoginSuccess(info);
|
||||
}];
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#import "KBNetworkManager.h"
|
||||
#import "AFNetworking.h"
|
||||
#import "KBAuthManager.h"
|
||||
|
||||
NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
|
||||
|
||||
@@ -95,8 +96,10 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
|
||||
}
|
||||
|
||||
- (void)applyHeaders:(NSDictionary<NSString *,NSString *> *)headers toMutableRequest:(NSMutableURLRequest *)req contentType:(NSString *)contentType {
|
||||
// 合并默认头与局部头,局部覆盖
|
||||
// 合并默认头与局部头,并注入授权头(若可用)。局部覆盖优先。
|
||||
NSMutableDictionary *all = [self.defaultHeaders mutableCopy] ?: [NSMutableDictionary new];
|
||||
NSDictionary *auth = [[KBAuthManager shared] authorizationHeader];
|
||||
[auth enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }];
|
||||
if (contentType) all[@"Content-Type"] = contentType;
|
||||
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }];
|
||||
[all enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { [req setValue:obj forHTTPHeaderField:key]; }];
|
||||
|
||||
Reference in New Issue
Block a user