Skip to content

Commit

Permalink
Adopt new authentication API from black candy
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Nov 8, 2023
1 parent 98d8129 commit 9f888bc
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 20 deletions.
4 changes: 3 additions & 1 deletion BlackCandy.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1320;
LastUpgradeCheck = 1320;
LastUpgradeCheck = 1500;
TargetAttributes = {
DD62F4C727C769CC005A5D0B = {
CreatedOnToolsVersion = 13.2.1;
Expand Down Expand Up @@ -905,6 +905,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -967,6 +968,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "3efbfba0e4e56c7187cc19137ee16b7c95346b79",
"version" : "1.1.0"
"revision" : "edd66cace818e1b1c6f1b3349bb1d8e00d6f8b01",
"version" : "1.0.0"
}
},
{
Expand All @@ -113,8 +113,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swiftui-navigation",
"state" : {
"revision" : "6eb293c49505d86e9e24232cb6af6be7fff93bd5",
"version" : "1.0.2"
"revision" : "f5bcdac5b6bb3f826916b14705f37a3937c2fd34",
"version" : "1.0.0"
}
},
{
Expand All @@ -131,8 +131,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "23cbf2294e350076ea4dbd7d5d047c1e76b03631",
"version" : "1.0.2"
"revision" : "302891700c7fa3b92ebde9fe7b42933f8349f3c7",
"version" : "1.0.0"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1340"
LastUpgradeVersion = "1500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
7 changes: 5 additions & 2 deletions BlackCandy/Clients/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Dependencies
import Alamofire

struct APIClient {
var authentication: (LoginState) async throws -> AuthenticationResponse
var login: (LoginState) async throws -> AuthenticationResponse
var logout: () async throws -> NoContentResponse
var getCurrentPlaylistSongs: () async throws -> [Song]
var toggleFavorite: (Song) async throws -> Int
var deleteCurrentPlaylistSongs: ([Song]) async throws -> NoContentResponse
Expand All @@ -15,7 +16,9 @@ struct APIClient {

extension APIClient: TestDependencyKey {
static let testValue = Self(
authentication: unimplemented("\(Self.self).authentication"),
login: unimplemented("\(Self.self).login"),

logout: unimplemented("\(Self.self).logout"),

getCurrentPlaylistSongs: unimplemented("\(Self.self).getCurrentPlaylistSongs"),

Expand Down
20 changes: 17 additions & 3 deletions BlackCandy/Clients/APIClient/LiveAPIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ extension APIClient: DependencyKey {
}

return Self(
authentication: { loginState in
login: { loginState in
let parameters: [String: Any] = [
"with_session": "true",
"user_session": [
"with_cookie": "true",
"session": [
"email": loginState.email,
"password": loginState.password
]
Expand Down Expand Up @@ -125,6 +125,20 @@ extension APIClient: DependencyKey {
}
},

logout: {
let request = AF.request(
requestURL("/authentication"),
method: .delete,
headers: headers
)
.validate()
.serializingDecodable(NoContentResponse.self)

return try await handleRequest(request) { request, _ in
try await request.value
}
},

getCurrentPlaylistSongs: {
let request = AF.request(
requestURL("/current_playlist/songs"),
Expand Down
17 changes: 17 additions & 0 deletions BlackCandy/Store/AppReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SwiftUI
import ComposableArchitecture

struct AppReducer: Reducer {
@Dependency(\.apiClient) var apiClient
@Dependency(\.userDefaultsClient) var userDefaultsClient
@Dependency(\.cookiesClient) var cookiesClient
@Dependency(\.keychainClient) var keychainClient
Expand Down Expand Up @@ -56,6 +57,7 @@ struct AppReducer: Reducer {
case dismissAlert
case restoreStates
case logout
case logoutResponse(TaskResult<APIClient.NoContentResponse>)
case updateTheme(State.Theme)
case player(PlayerReducer.Action)
case login(LoginReducer.Action)
Expand All @@ -72,6 +74,15 @@ struct AppReducer: Reducer {
return .none

case .logout:
return .run { send in
await send(
.logoutResponse(
TaskResult { try await apiClient.logout() }
)
)
}

case .logoutResponse(.success):
keychainClient.deleteAPIToken()
jsonDataClient.deleteCurrentUser()
windowClient.changeRootViewController(LoginViewController(store: AppStore.shared))
Expand All @@ -82,6 +93,12 @@ struct AppReducer: Reducer {
await cookiesClient.cleanCookies()
}

case let .logoutResponse(.failure(error)):
guard let error = error as? APIClient.APIError else { return .none }
state.alert = .init(title: .init(error.localizedString))

return .none

case let .updateTheme(theme):
state.currentTheme = theme
return .none
Expand Down
2 changes: 1 addition & 1 deletion BlackCandy/Store/LoginReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct LoginReducer: Reducer {
return .run { send in
await send(
.loginResponse(
TaskResult { try await apiClient.authentication(loginState) }
TaskResult { try await apiClient.login(loginState) }
)
)
}
Expand Down
2 changes: 1 addition & 1 deletion BlackCandyTests/Clients/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class APIClientTests: XCTestCase {
loginState.email = "admin@admin.com"
loginState.password = "foobar"

let authenticationResponse = try await apiClient.authentication(loginState)
let authenticationResponse = try await apiClient.login(loginState)
let responseCookie = authenticationResponse.cookies.first!

XCTAssertEqual(authenticationResponse.user.email, "admin@admin.com")
Expand Down
16 changes: 13 additions & 3 deletions BlackCandyTests/Store/AppReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,21 @@ final class AppReducerTests: XCTestCase {
var state = AppReducer.State()
state.currentUser = try users(id: 1)

let store = TestStore(initialState: state) {
AppReducer()
let logoutResponse = APIClient.NoContentResponse()

let store = withDependencies {
$0.apiClient.logout = {
logoutResponse
}
} operation: {
TestStore(initialState: state) {
AppReducer()
}
}

await store.send(.logout) {
await store.send(.logout)

await store.receive(.logoutResponse(.success(logoutResponse))) {
$0.currentUser = nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions BlackCandyTests/Store/LoginReducerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class LoginReducerTests: XCTestCase {
let loginResponse = APIClient.AuthenticationResponse(token: "test_token", user: user, cookies: [cookie])

let store = withDependencies {
$0.apiClient.authentication = { _ in
$0.apiClient.login = { _ in
loginResponse
}
} operation: {
Expand All @@ -131,7 +131,7 @@ final class LoginReducerTests: XCTestCase {
let responseError = APIClient.APIError.unknown

let store = withDependencies {
$0.apiClient.authentication = { _ in
$0.apiClient.login = { _ in
throw responseError
}
} operation: {
Expand Down

0 comments on commit 9f888bc

Please sign in to comment.