Skip to content

Commit

Permalink
- add network service function to send a HTTP DELETE
Browse files Browse the repository at this point in the history
- clear facebook auth token after a successful login to allow users to login with multiple accounts
  • Loading branch information
simonmcl committed Nov 1, 2024
1 parent 9271a03 commit 1cee51f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
36 changes: 36 additions & 0 deletions Sources/KukaiCoreSwift/Services/NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,42 @@ public class NetworkService {
}.eraseToAnyPublisher()
}

/**
Send a HTTP DELETE to a given URL
*/
public func delete(url: URL, completion: @escaping ((Result<Bool, KukaiError>) -> Void)) {
var request = URLRequest(url: url)
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "DELETE"

urlSession.dataTask(with: request) { (data, response, error) in
if let err = error {
completion(Result.failure(KukaiError.internalApplicationError(error: err)))
} else {
completion(Result.success(true))
}
}.resume()
NetworkService.logRequestStart(loggingConfig: loggingConfig, fullURL: url)
}

/**
Send a HTTP DELETE to a given URL
*/
public func delete(url: URL) -> AnyPublisher<Bool, KukaiError> {
return Future<Bool, KukaiError> { [weak self] promise in
self?.delete(url: url, completion: { result in
guard let output = try? result.get() else {
let error = (try? result.getError()) ?? KukaiError.unknown()
promise(.failure(error))
return
}

promise(.success(output))
})
}.eraseToAnyPublisher()
}

func checkForRPCOperationErrors(parsedResponse: Any, withRequestURL: URL?, requestPayload: Data?, responsePayload: Data?, httpStatusCode: Int?) -> KukaiError? {
var operations: [OperationResponse] = []

Expand Down
17 changes: 12 additions & 5 deletions Sources/KukaiCoreSwift/Services/TorusAuthService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,22 @@ public class TorusAuthService: NSObject {
completion(Result.failure(KukaiError.internalApplicationError(error: TorusAuthError.missingVerifier)))
}

let accessToken = data.userInfo["accessToken"] as? String

// Twitter API doesn't give us the bloody "@" handle for some reason. Fetch that first and overwrite the username property with the handle, if found
if authType == .twitter {
twitterHandleLookup(id: userId ?? "") { [weak self] result in
switch result {
case .success(let actualUsername):
self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: actualUsername, userId: userId, profile: profile, completion: completion)
self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: actualUsername, userId: userId, profile: profile, accessToken: accessToken, completion: completion)

case .failure(_):
self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, completion: completion)
self?.createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, accessToken: accessToken, completion: completion)
}
}

} else {
createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, completion: completion)
createTorusWalletAndContinue(pk: pk, authType: authType, username: username, userId: userId, profile: profile, accessToken: accessToken, completion: completion)
}

} catch {
Expand All @@ -275,14 +276,20 @@ public class TorusAuthService: NSObject {
}
}

private func createTorusWalletAndContinue(pk: String?, authType: TorusAuthProvider, username: String?, userId: String?, profile: String?, completion: @escaping ((Result<TorusWallet, KukaiError>) -> Void)) {
private func createTorusWalletAndContinue(pk: String?, authType: TorusAuthProvider, username: String?, userId: String?, profile: String?, accessToken: String?, completion: @escaping ((Result<TorusWallet, KukaiError>) -> Void)) {
guard let privateKeyString = pk, let wallet = TorusWallet(authProvider: authType, username: username, userId: userId, profilePicture: profile, torusPrivateKey: privateKeyString) else {
Logger.torus.error("Error torus contained no, or invlaid private key")
completion(Result.failure(KukaiError.internalApplicationError(error: TorusAuthError.invalidTorusResponse)))
return
}

completion(Result.success(wallet))
if authType == .facebook, let token = accessToken, let url = URL(string: "https://graph.facebook.com/me/permissions?access_token=\(token)") {
networkService.delete(url: url) { result in
completion(Result.success(wallet))
}
} else {
completion(Result.success(wallet))
}
}


Expand Down

0 comments on commit 1cee51f

Please sign in to comment.