@@ -5,6 +5,14 @@ import Combine
5
5
public class Twift : NSObject , ObservableObject {
6
6
/// The type of authentication access for this Twift instance
7
7
public private( set) var authenticationType : AuthenticationType
8
+ public var oauthUser : OAuth2User ? {
9
+ switch authenticationType {
10
+ case . oauth2UserAuth( let user, _) :
11
+ return user
12
+ default :
13
+ return nil
14
+ }
15
+ }
8
16
9
17
internal let decoder : JSONDecoder
10
18
internal let encoder : JSONEncoder
@@ -17,6 +25,22 @@ public class Twift: NSObject, ObservableObject {
17
25
self . encoder = Self . initializeEncoder ( )
18
26
}
19
27
28
+ /// Initialises an instance with OAuth2 User authentication
29
+ /// - Parameters:
30
+ /// - oauth2User: The OAuth2 User object for authenticating requests on behalf of a user
31
+ /// - onTokenRefresh: A callback invoked when the access token is refreshed by Twift. Useful for storing updated credentials.
32
+ public convenience init ( oauth2User: OAuth2User ,
33
+ onTokenRefresh: @escaping ( OAuth2User ) -> Void = { _ in } ) {
34
+ self . init ( . oauth2UserAuth( oauth2User, onRefresh: onTokenRefresh) )
35
+ }
36
+
37
+ /// Initialises an instance with App-Only Bearer Token authentication
38
+ /// - Parameters:
39
+ /// - appOnlyBearerToken: The App-Only Bearer Token issued by Twitter for authenticating requests
40
+ public convenience init ( appOnlyBearerToken: String ) {
41
+ self . init ( . appOnly( bearerToken: appOnlyBearerToken) )
42
+ }
43
+
20
44
/// Swift's native implementation of ISO 8601 date decoding defaults to a format that doesn't include milliseconds, causing decoding errors because of Twitter's date format.
21
45
/// This function returns a decoder which can decode Twitter's date formats, as well as converting keys from snake_case to camelCase.
22
46
static internal func initializeDecoder( ) -> JSONDecoder {
@@ -71,9 +95,10 @@ public class Twift: NSObject, ObservableObject {
71
95
}
72
96
73
97
/// Refreshes the OAuth 2.0 token, optionally forcing a refresh even if the token is still valid
98
+ /// After a successful refresh, a user-defined callback is performed. (optional)
74
99
/// - Parameter onlyIfExpired: Set to false to force the token to refresh even if it hasn't yet expired.
75
100
public func refreshOAuth2AccessToken( onlyIfExpired: Bool = true ) async throws {
76
- guard case AuthenticationType . oauth2UserAuth( let oauthUser) = self . authenticationType else {
101
+ guard case AuthenticationType . oauth2UserAuth( let oauthUser, let refreshCompletion ) = self . authenticationType else {
77
102
throw TwiftError . WrongAuthenticationType ( needs: . oauth2UserAuth)
78
103
}
79
104
@@ -106,6 +131,10 @@ public class Twift: NSObject, ObservableObject {
106
131
var refreshedOAuthUser = try JSONDecoder ( ) . decode ( OAuth2User . self, from: data)
107
132
refreshedOAuthUser. clientId = clientId
108
133
109
- self . authenticationType = . oauth2UserAuth( refreshedOAuthUser)
134
+ if let refreshCompletion = refreshCompletion {
135
+ refreshCompletion ( refreshedOAuthUser)
136
+ }
137
+
138
+ self . authenticationType = . oauth2UserAuth( refreshedOAuthUser, onRefresh: refreshCompletion)
110
139
}
111
140
}
0 commit comments