     private static var oauthSwift: OAuth2Swift?
 
     static func callbackURL(for loginService: LoginService, at server: URL) -> URL? {
-        guard let service = loginService.service else { return nil }
-        return URL(string: "https://\(server.host ?? "")/_oauth/\(service)")
+        guard
+            let service = loginService.service,
+            let host = server.host
+        else {
+            return nil
+        }
+
+        return URL(string: "https://\(host)/_oauth/\(service)")
+    }
+
+    static func state() -> String? {
+        return "{\"loginStyle\":\"popup\",\"credentialToken\":\"\(String.random(40))\",\"isCordova\":true}".base64Encoded()
     }
 
     static func authorize(loginService: LoginService, at server: URL, viewController: UIViewController, success: @escaping (OAuthCredentials) -> Void, failure: @escaping () -> Void) {

        }
        oauthSwift.authorizeURLHandler = handler

        let state = "{\"loginStyle\":\"popup\",\"credentialToken\":\"\(String.random(40))\",\"isCordova\":true}".base64Encoded()
        oauthSwift.authorize(withCallbackURL: callbackURL, scope: loginService.scope ?? "",
                             state: state ?? "", success: { _,_,_  in }, failure: { _ in failure() })
    }
}

            responseType: "token"
        )

        guard let oauthSwift = oauthSwift else {
            failure()
            return
        }

        let handler = WebViewController()
        oauthSwift.removeCallbackNotificationObserver()
        handler.targetURL = URL(string: "\(host)\(authorizePath)")
        handler.viewDidLoad()
        viewController.navigationController?.pushViewController(handler, animated: true)
        handler.didNavigate = { url in
            guard let url = url else { return false }

            if url.host == callbackURL.host && url.path == callbackURL.path, let fragment = url.fragment {
                let fragmentJSON = JSON(parseJSON: NSString(string: fragment).removingPercentEncoding ?? "")
