Commit 2897214c authored by Jose Blaya's avatar Jose Blaya
Browse files

Merge branch '172-add-logout-function-to-invalidate-the-access-token' into 'release/2.2.3'

Resolve "Add logout function to invalidate the access token"

See merge request ios/client-library-apple!228
parents 55f63382 d39aa994
......@@ -36,6 +36,12 @@ protocol WebServices: class {
func update(credentials: Credentials, email: String, _ callback: SuccessLibraryCallback?)
/**
Invalidates the access token.
- Parameter callback: Returns an `Bool` if the token was expired.
*/
func logout(_ callback: LibraryCallback<Bool>?)
#if os(iOS)
func signup(with request: Signup, _ callback: LibraryCallback<Credentials>?)
......
......@@ -281,9 +281,20 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
guard isLoggedIn else {
preconditionFailure()
}
cleanDatabase()
Macros.postNotification(.PIAAccountDidLogout)
callback?(nil)
webServices.logout { [weak self] (result, error) in
guard let success = result else {
callback?(nil)
return
}
if success {
self?.cleanDatabase()
Macros.postNotification(.PIAAccountDidLogout)
}
callback?(nil)
}
}
func invitesInformation(_ callback: LibraryCallback<InvitesInformation>?) {
......
......@@ -36,6 +36,8 @@ enum ClientEndpoint: String, Endpoint {
case account = "v2/account"
case updateAccount = "account"
case logout = "v2/expire_token"
case payment
......
......@@ -98,6 +98,21 @@ class PIAWebServices: WebServices, ConfigurationAccess {
})
}
func logout(_ callback: LibraryCallback<Bool>?) {
let endpoint = ClientEndpoint.logout
let status = [200]
req(nil, .post, endpoint, useAuthToken: true, nil, status, JSONRequestExecutor() { (json, status, error) in
if let error = error {
callback?(false, error)
return
}
callback?(true, nil)
})
}
#if os(iOS)
func signup(with request: Signup, _ callback: ((Credentials?, Error?) -> Void)?) {
let endpoint = ClientEndpoint.signup
......
......@@ -53,6 +53,10 @@ class MockWebServices: WebServices {
callback?(nil)
}
func logout(_ callback: LibraryCallback<Bool>?) {
callback?(true, nil)
}
func signup(with request: Signup, _ callback: ((Credentials?, Error?) -> Void)?) {
let result = credentials?()
let error: ClientError? = (result == nil) ? .unsupported : nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment