Skip to content
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

[IDLE-74] 로그인시 토큰 로컬 저장및 모든 요청에 적용 #18

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import Entity

public class DefaultAuthInputValidationRepository: AuthInputValidationRepository {

let networkService = CenterRegisterService()
let networkService = AuthService()

public init() { }

public func requestPhoneNumberAuthentication(phoneNumber: String) -> RxSwift.Single<BoolResult> {

networkService.request(api: .startPhoneNumberAuth(phoneNumber: phoneNumber))
networkService.requestWithoutToken(api: .startPhoneNumberAuth(phoneNumber: phoneNumber))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

Expand All @@ -29,14 +29,14 @@ public class DefaultAuthInputValidationRepository: AuthInputValidationRepository
case 204:
return .success(true)
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
return .failure(self.decodeError(of: InputValidationError.self, data: response.data))
}
}
}

public func authenticateAuthNumber(phoneNumber: String, authNumber: String) -> RxSwift.Single<BoolResult> {

networkService.request(api: .checkAuthNumber(phoneNumber: phoneNumber, authNumber: authNumber))
networkService.requestWithoutToken(api: .checkAuthNumber(phoneNumber: phoneNumber, authNumber: authNumber))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

Expand All @@ -46,14 +46,14 @@ public class DefaultAuthInputValidationRepository: AuthInputValidationRepository
case 204:
return .success(true)
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
return .failure(self.decodeError(of: InputValidationError.self, data: response.data))
}
}
}

public func requestBusinessNumberAuthentication(businessNumber: String) -> RxSwift.Single<BusinessNumberAuthResult> {

networkService.request(api: .authenticateBusinessNumber(businessNumber: businessNumber))
networkService.requestWithoutToken(api: .authenticateBusinessNumber(businessNumber: businessNumber))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

Expand All @@ -64,13 +64,13 @@ public class DefaultAuthInputValidationRepository: AuthInputValidationRepository
let dto: BusinessInfoDTO = self.decodeData(data: response.data)
return .success(dto.toEntity())
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
return .failure(self.decodeError(of: InputValidationError.self, data: response.data))
}
}
}

public func requestCheckingIdDuplication(id: String) -> RxSwift.Single<Entity.BoolResult> {
networkService.request(api: .checkIdDuplication(id: id))
networkService.requestWithoutToken(api: .checkIdDuplication(id: id))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

Expand All @@ -82,7 +82,7 @@ public class DefaultAuthInputValidationRepository: AuthInputValidationRepository
case 400:
return .success(false)
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
return .failure(self.decodeError(of: InputValidationError.self, data: response.data))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Entity

public class DefaultAuthRepository: AuthRepository {

let networkService = CenterRegisterService()
let networkService = AuthService()

public init() { }

Expand All @@ -29,7 +29,7 @@ public class DefaultAuthRepository: AuthRepository {

let data = (try? JSONEncoder().encode(dto)) ?? Data()

return networkService.request(api: .registerCenterAccount(data: data))
return networkService.requestWithoutToken(api: .registerCenterAccount(data: data))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

Expand All @@ -39,25 +39,43 @@ public class DefaultAuthRepository: AuthRepository {
case 201:
return .success(true)
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
return .failure(self.decodeError(of: AuthError.self, data: response.data))
}
}
}

public func requestCenterLogin(id: String, password: String) -> RxSwift.Single<Entity.BoolResult> {

return networkService.request(api: .centerLogin(id: id, password: password))
return networkService.requestWithoutToken(api: .centerLogin(id: id, password: password))
.catch { [weak self] in .error(self?.filterNetworkConnection($0) ?? $0) }
.map { [weak self] response in

guard let self = self else { return BoolResult.failure(.unknownError) }

switch response.statusCode {
case 200:
return .success(true)
default:
return .failure(self.decodeError(of: CenterRegisterError.self, data: response.data))
if response.statusCode == 200 {

if let dict = try JSONSerialization.jsonObject(with: response.data) as? [String: String],
let accessToken = dict["accessToken"],
let refreshToken = dict["refreshToken"] {

// 토큰처리
do {
try self.networkService.keyValueStore.saveAuthToken(
accessToken: accessToken,
refreshToken: refreshToken
)
#if DEBUG
print("\(#function) ✅ 토큰 저장성공")
#endif

return .success(true)
} catch {

return .failure(.localSaveError)
}
}
}
return .failure(self.decodeError(of: AuthError.self, data: response.data))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation
import Entity
import RepositoryInterface
import Moya
import NetworkDataSource

extension RepositoryBase {

Expand All @@ -34,6 +35,14 @@ extension RepositoryBase {

func filterNetworkConnection(_ error: Error) -> URLError? {

// 데이터소스 에러
if let dataSourceError = error as? DataSourceError {
#if DEBUG
print("데이터 소스 에러: \(error.localizedDescription)")
#endif
return nil
}

guard let moyaError = error as? MoyaError, case .underlying(let err, _) = moyaError, let afError = err.asAFError, afError.isSessionTaskError else {
return nil
}
Expand Down
32 changes: 27 additions & 5 deletions project/Projects/Data/ConcretesTests/ConcretesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,39 @@ final class ConcretesTests: XCTestCase {

func testToken() {

// TODO: 토큰 API구현이후 테스트 코드 작성 예정
// TODO: 로컬 맵핑 테스트

// let expectation = expectation(description: "Test function")
//
// let testStore = TestKeyValueStore()
// let repo = AuthService(
// keyValueStore: TestKeyValueStore()
// )
//
// let testService = DefaultTestService(keyValueStore: testStore)
// let disposeBag = DisposeBag()
//
// let single = testService.testRequest()
// let data = CenterRegistrationDTO(
// identifier: "test1234",
// password: "testpassword1234",
// phoneNumber: "010-4444-5555",
// managerName: "최준영",
// centerBusinessRegistrationNumber: "000-00-00000"
// )
//
// waitForExpectations(timeout: 10, handler: nil)
//
// repo
// .request(api: .centerLogin(
// id: "test1234",
// password: "testpassword1234")
// )
// .subscribe { response in
//
// print(response)
//
// expectation.fulfill()
// }
// .disposed(by: disposeBag)
//
// waitForExpectations(timeout: 60, handler: nil)
}

func testAuth() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public enum AuthAPI {
case checkIdDuplication(id: String)
case registerCenterAccount(data: Data)
case centerLogin(id: String, password: String)
case reissueToken(refreshToken: String)
}

extension AuthAPI: BaseAPI {
Expand All @@ -41,6 +42,8 @@ extension AuthAPI: BaseAPI {
return .post
case .centerLogin:
return .post
case .reissueToken:
return .post
}
}

Expand All @@ -58,6 +61,8 @@ extension AuthAPI: BaseAPI {
"center/join"
case .centerLogin:
"center/login"
case .reissueToken:
"center/refresh"
}
}

Expand All @@ -72,6 +77,8 @@ extension AuthAPI: BaseAPI {
case .centerLogin(let id, let password):
params["identifier"] = id
params["password"] = password
case .reissueToken(let refreshToken):
params["refreshToken"] = refreshToken
default:
break
}
Expand All @@ -95,6 +102,8 @@ extension AuthAPI: BaseAPI {
return .requestData(data)
case .centerLogin:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
case .reissueToken:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
default:
return .requestPlain
}
Expand Down
11 changes: 11 additions & 0 deletions project/Projects/Data/NetworkDataSource/API/BaseAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ public extension BaseAPI {

return ["Content-Type": "application/json"]
}

var validationType: ValidationType {
.customCodes(
[
200,
201,
204,
400,
]
)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//
// CenterRegisterService.swift
// AuthService.swift
// NetworkDataSource
//
// Created by choijunios on 7/8/24.
//

import Foundation

public class CenterRegisterService: BaseNetworkService<AuthAPI> {
public class AuthService: BaseNetworkService<AuthAPI> {

public init() { }

public override init(keyValueStore: KeyValueStore) {
super.init(keyValueStore: keyValueStore)
}
}
Loading