-
Notifications
You must be signed in to change notification settings - Fork 191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unregister push on logout #1309
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Foundation | ||
|
||
actor UnregisterService { | ||
private let httpClient: HTTPClient | ||
private let projectId: String | ||
private let logger: ConsoleLogging | ||
private let environment: APNSEnvironment | ||
private let pushAuthenticator: PushAuthenticating | ||
private let clientIdStorage: ClientIdStoring | ||
private let pushHost: String | ||
|
||
init(httpClient: HTTPClient, | ||
projectId: String, | ||
clientIdStorage: ClientIdStoring, | ||
pushAuthenticator: PushAuthenticating, | ||
logger: ConsoleLogging, | ||
pushHost: String, | ||
environment: APNSEnvironment) { | ||
self.httpClient = httpClient | ||
self.clientIdStorage = clientIdStorage | ||
self.pushAuthenticator = pushAuthenticator | ||
self.projectId = projectId | ||
self.logger = logger | ||
self.pushHost = pushHost | ||
self.environment = environment | ||
} | ||
|
||
func unregister() async throws { | ||
let pushAuthToken = try pushAuthenticator.createAuthToken() | ||
let clientId = try clientIdStorage.getClientId() | ||
|
||
guard let url = URL(string: "https://\(pushHost)/\(projectId)/clients/\(clientId)") else { | ||
logger.error("Invalid URL") | ||
return | ||
} | ||
|
||
var request = URLRequest(url: url) | ||
request.httpMethod = "DELETE" | ||
request.addValue("\(pushAuthToken)", forHTTPHeaderField: "Authorization") | ||
|
||
do { | ||
let (_, response) = try await URLSession.shared.data(for: request) | ||
|
||
guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Instead of checking for a specific status code, can you check if it is any 2xx status code? We may want e.g. change to 204 (no content) and all 2xx codes mean success |
||
logger.error("Failed to unregister from Push Server") | ||
throw NSError(domain: "UnregisterService", code: 0, userInfo: [NSLocalizedDescriptionKey: "Failed to unregister"]) | ||
} | ||
|
||
logger.debug("Successfully unregistered from Push Server") | ||
} catch { | ||
logger.error("Push Server unregistration error: \(error.localizedDescription)") | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the unregister fails (e.g. no internet?) Will this flow be tried again or will the push registration still be present?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, I will make it throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chris13524 what code push api returns for already unregistered client on /unregister ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It gives the same response code regardless if the client was already unregistered or not: 200
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think there is an issue. If it throws, what does caller do with that? If simply display an error message, the question to me is when will it do the unregister again?
Looks to me that you are clearing the notify storage database (and other things) before you unregister the push client. So this would mean if the way the unregister takes place is based on the notify storage, then the unregister won't happen again. I'm assuming that logout should be an atomic operation to prevent the receiving of push notifications it should not.