@@ -5,6 +5,14 @@ import Combine
55public class Twift : NSObject , ObservableObject {
66 /// The type of authentication access for this Twift instance
77 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+ }
816
917 internal let decoder : JSONDecoder
1018 internal let encoder : JSONEncoder
@@ -17,6 +25,22 @@ public class Twift: NSObject, ObservableObject {
1725 self . encoder = Self . initializeEncoder ( )
1826 }
1927
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+
2044 /// 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.
2145 /// This function returns a decoder which can decode Twitter's date formats, as well as converting keys from snake_case to camelCase.
2246 static internal func initializeDecoder( ) -> JSONDecoder {
@@ -71,9 +95,10 @@ public class Twift: NSObject, ObservableObject {
7195 }
7296
7397 /// 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)
7499 /// - Parameter onlyIfExpired: Set to false to force the token to refresh even if it hasn't yet expired.
75100 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 {
77102 throw TwiftError . WrongAuthenticationType ( needs: . oauth2UserAuth)
78103 }
79104
@@ -106,6 +131,10 @@ public class Twift: NSObject, ObservableObject {
106131 var refreshedOAuthUser = try JSONDecoder ( ) . decode ( OAuth2User . self, from: data)
107132 refreshedOAuthUser. clientId = clientId
108133
109- self . authenticationType = . oauth2UserAuth( refreshedOAuthUser)
134+ if let refreshCompletion = refreshCompletion {
135+ refreshCompletion ( refreshedOAuthUser)
136+ }
137+
138+ self . authenticationType = . oauth2UserAuth( refreshedOAuthUser, onRefresh: refreshCompletion)
110139 }
111140}
0 commit comments