 
     public static func setup() {
         loadConfig()
-        if let p = profile, let a = account, let token = a.accessToken.accessToken {
-            CloudAPIClient.login(profile: p, token: token)
-            if isExpired {
-                refreshAccessToken(a)
-            }
-        } else {
-            profile = nil
-            clearAllAccount()
+        oauth = OAuth2Swift(
+            consumerKey:    clientId,
+            consumerSecret: clientSecret,
+            authorizeUrl:   shared.authUrl,
+            accessTokenUrl: shared.tokenUrl,
+            responseType:   "code"
+        )
+        if let c = credential {
+            oauth.client.credential.oauthToken          = c.oauthToken
+            oauth.client.credential.oauthTokenSecret    = c.oauthTokenSecret
+            oauth.client.credential.oauthTokenExpiresAt = c.oauthTokenExpiresAt
+            oauth.client.credential.oauthRefreshToken   = c.oauthRefreshToken
         }
+        if let p = profile, let c = credential {
+            CloudAPIClient.login(profile: p, token: c.oauthToken)
+        }
+        shared.manager.retrier = FeedlyOAuthRequestRetrier(oauth)
         CloudAPIClient.sharedPipe.0.observeResult({ result in
             guard let event = result.value else { return }
             switch event {
             case .Login(let profile):
                 self.profile = profile
             case .Logout:
-                if self.isExpired {
-                    self.refreshAccessToken(self.account!)
-                } else {
-                    self.clearAllAccount()
-                }
+                self.profile = nil
+                self.credential = nil
             }
         })
     }
 }
-
-open class FeedlyOAuth2ClientDelegate: NSObject, NXOAuth2ClientDelegate {
-    public override init() {
-    }
-    open func oauthClientNeedsAuthentication(_ client: NXOAuth2Client!) {}
-    open func oauthClientDidGetAccessToken(_ client: NXOAuth2Client!) {}
-    open func oauthClient(_ client: NXOAuth2Client!, didFailToGetAccessTokenWithError error: Error!) {
-    }
-    open func oauthClientDidLoseAccessToken(_ client: NXOAuth2Client!) {
-    }
-    open func oauthClientDidRefreshAccessToken(_ client: NXOAuth2Client!) {
-        CloudAPIClient.refreshAccount(NXOAuth2Account(accountWith: client.accessToken,
-            accountType: CloudAPIClient.accountType))
-    }
-}

    }

    fileprivate static func clearAllAccount() {
        guard let store    = NXOAuth2AccountStore.sharedStore() as? NXOAuth2AccountStore else { return }
        guard let accounts = store.accounts as? [NXOAuth2Account]                        else { return }
        for account in accounts {
            if account.accountType == "Feedly" {
                store.removeAccount(account)
            }
        }
        _account = nil
        profile = nil
    }
    
    fileprivate static func loadConfig() {


import SwiftyJSON
import FeedlyKit
import OAuthSwift
import Prephirences

open class FeedlyOAuthRequestRetrier: OAuthRequestRetrier {
    public override func refreshed(_ succeeded: Bool) {
        if succeeded {
            CloudAPIClient.credential = oauth.client.credential
            CloudAPIClient.setAccessToken(oauth.client.credential.oauthToken)
        } else {
            CloudAPIClient.credential = nil
            CloudAPIClient.logout()
        }
    }
}


public extension CloudAPIClient {
    fileprivate static let userDefaults = UserDefaults.standard
    fileprivate static var _profile:                    Profile?
    fileprivate static var _notificationDateComponents: DateComponents?
    fileprivate static var _lastChecked:                Date?
