                     return TransactionStore.shared.clear()
                 }
                 _ = TransactionStore.shared.resume(callbackURL, options: [:])
-                }
-            self.authSession = authSession
-            authSession.start()
+            })
         }
         #else
-        let authSession = SFAuthenticationSession(url: authorizeURL, callbackURLScheme: self.redirectURL.absoluteString) { [unowned self] in
+        authSession = .init(SFAuthenticationSession(url: authorizeURL, callbackURLScheme: self.redirectURL.absoluteString) { [unowned self] in
             guard $1 == nil, let callbackURL = $0 else {
                 let authError = $1 ?? WebAuthError.unknownError
                 if case SFAuthenticationError.canceledLogin = authError {

                return TransactionStore.shared.clear()
            }
            _ = TransactionStore.shared.resume(callbackURL, options: [:])
        }
        self.authSession = authSession
        authSession.start()
        #endif
    }
}
#endif

#if swift(>=3.2)
@available(iOS 11.0, *)
class SafariAuthenticationSession: AuthSession {
    private enum AuthenticationSession {
        @available(iOS 12.0, *)
        case authenticationServices(ASWebAuthenticationSession)
        case safariServices(SFAuthenticationSession)

        @available(iOS 12.0, *)
        init(_ session: ASWebAuthenticationSession) {
            self = .authenticationServices(session)
        }

        init(_ session: SFAuthenticationSession) {
            self = .safariServices(session)
        }

        func start() -> Bool {
            switch self {
            case .authenticationServices(let session):
                return session.start()
            case .safariServices(let session):
                return session.start()
            }
        }

        func cancel() {
            switch self {
            case .authenticationServices(let session):
                return session.cancel()
            case .safariServices(let session):
                return session.cancel()
            }
        }
    }

    private var authSession: AuthenticationSession?

    init(authorizeURL: URL, redirectURL: URL, state: String? = nil, handler: OAuth2Grant, finish: @escaping FinishSession, logger: Logger?) {
        super.init(redirectURL: redirectURL, state: state, handler: handler, finish: finish, logger: logger)
        #if canImport(AuthenticationServices)
        if #available(iOS 12.0, *) {
            authSession = .init(ASWebAuthenticationSession(url: authorizeURL, callbackURLScheme: self.redirectURL.absoluteString) { [unowned self] in
                guard $1 == nil, let callbackURL = $0 else {
                    let authError = $1 ?? WebAuthError.unknownError
                    if case ASWebAuthenticationSessionError.canceledLogin = authError {
