1+ import WebKit
12import WordPressAuthenticator
3+ import WordPressKit
24
35extension ViewController : WordPressAuthenticatorDelegate {
46
@@ -68,11 +70,7 @@ extension ViewController: WordPressAuthenticatorDelegate {
6870
6971 func sync( credentials: AuthenticatorCredentials , onCompletion: @escaping ( ) -> Void ) {
7072 dismiss ( animated: true ) { [ weak self] in
71- self ? . presentAlert (
72- title: " Authentication Successful " ,
73- message: " Next step will be syncing credentials " ,
74- onDismiss: { }
75- )
73+ self ? . sync ( credentials: credentials)
7674 }
7775 }
7876
@@ -90,3 +88,46 @@ extension ViewController: WordPressAuthenticatorDelegate {
9088 print ( error)
9189 }
9290}
91+
92+ extension ViewController {
93+
94+ // This is just so we can avoid nesting within a dismiss block and the weak self dance.
95+ //
96+ // See WordPress iOS
97+ //
98+ // - WordPressAuthenticationManager sync(credentials:, onCompletion:)
99+ // - WordPressAuthenticationManager syncWPCom(authToken:, isJetpackLogin:, onCompletion:)
100+ // - AccountService createOrUpdateAccountWithAuthToken:success:failure:
101+ private func sync( credentials: AuthenticatorCredentials ) {
102+ switch ( credentials. wpcom, credentials. wporg) {
103+ case ( . none, . none) , ( . some, . some) :
104+ fatalError ( " Inconsistent state! " )
105+ case ( . none, . some) :
106+ fatalError ( " Not implemented yet " )
107+ case ( . some( let wpComCredentials) , . none) :
108+ let api = WordPressComRestApi (
109+ oAuthToken: wpComCredentials. authToken,
110+ // TODO: there should be a way to read the user agent from the library configs
111+ userAgent: WKWebView . userAgent
112+ )
113+ let remote = AccountServiceRemoteREST ( wordPressComRestApi: api)
114+
115+ remote. getAccountDetails (
116+ success: { [ weak self] remoteUser in
117+ guard let remoteUser else {
118+ fatalError ( " Received no RemoteUser – Likely an Objective-C types byproduct. " )
119+ }
120+
121+ self ? . presentAlert (
122+ title: " 🎉 " ,
123+ message: " Welcome \( remoteUser. displayName ?? " 'no display name' " ) " ,
124+ onDismiss: { }
125+ )
126+ } ,
127+ failure: { error in
128+ print ( error!. localizedDescription)
129+ }
130+ )
131+ }
132+ }
133+ }
0 commit comments