Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active September 9, 2020 20:04
Show Gist options
  • Save vialyx/63946b573a5511d05cd2580fc699c1e0 to your computer and use it in GitHub Desktop.
Save vialyx/63946b573a5511d05cd2580fc699c1e0 to your computer and use it in GitHub Desktop.
class Model: AuthHandlerType {
var session: NSObject? = nil
var contextProvider: AuthContextProvider?
func auth(_ completion: @escaping ((String?, Error?) -> Void)) {
guard let authUrl = Constants.authUrl else {
completion(nil, nil)
return
}
var urlComponents = URLComponents(url: authUrl, resolvingAgainstBaseURL: false)
urlComponents?.queryItems = [
URLQueryItem(name: "response_type", value: Constants.responseType),
URLQueryItem(name: "client_id", value: Constants.clientId),
URLQueryItem(name: "redirect_url", value: Constants.redirectUrl),
URLQueryItem(name: "scope", value: Constants.scope.joined(separator: " ")),
URLQueryItem(name: "expires_in", value: String(Constants.expires))
]
guard let url = urlComponents?.url else {
completion(nil, nil)
return
}
auth(url: url, callbackScheme: Constants.redirectScheme) {
url, error in
if error != nil {
completion(nil, error)
} else if let `url` = url {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
let item = components.queryItems?.first(where: { $0.name == "code" }),
let code = item.value else {
completion(nil, nil)
return
}
completion(code, nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment