Skip to content

Commit

Permalink
Merge pull request #214 from 0inn/feat/qa
Browse files Browse the repository at this point in the history
[Fix] #213 - 리프래시 path를 auth/refresh로 수정
  • Loading branch information
0inn authored Apr 26, 2023
2 parents b790f36 + 30cddee commit d789555
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension SignInRepository: SignInRepositoryInterface {
case .network(let statusCode) = error,
statusCode == 400
else {
return self.userService.reissuance()
return self.authService.reissuance()
}
// NOTE: (@준호) 플그 미등록 + 비활동 유저의 경우 임시로 accessToken 빈 스트링 부여
// 자동로그인 시 활용하기 위함
Expand Down
15 changes: 13 additions & 2 deletions SOPT-iOS/Projects/Modules/Network/Sources/API/AuthAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import Foundation

import Alamofire
import Moya
import Core

public enum AuthAPI {
case signIn(token: String)
case reissuance
}

extension AuthAPI: BaseAPI {
Expand All @@ -22,7 +24,10 @@ extension AuthAPI: BaseAPI {
// MARK: - Header
public var headers: [String: String]? {
switch self {
default: return HeaderType.json.value
case .signIn:
return HeaderType.json.value
case .reissuance:
return HeaderType.jsonWithToken.value
}
}

Expand All @@ -31,6 +36,8 @@ extension AuthAPI: BaseAPI {
switch self {
case .signIn:
return "playground"
case .reissuance:
return "refresh"
}
}

Expand All @@ -39,6 +46,8 @@ extension AuthAPI: BaseAPI {
switch self {
case .signIn:
return .post
case .reissuance:
return .patch
}
}

Expand All @@ -48,6 +57,8 @@ extension AuthAPI: BaseAPI {
switch self {
case .signIn(let token):
params["code"] = token
case .reissuance:
params["refreshToken"] = UserDefaultKeyList.Auth.appRefreshToken
}
return params
}
Expand All @@ -61,7 +72,7 @@ extension AuthAPI: BaseAPI {

public var task: Task {
switch self {
case .signIn:
case .signIn, .reissuance:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
}
}
Expand Down
11 changes: 2 additions & 9 deletions SOPT-iOS/Projects/Modules/Network/Sources/API/UserAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public enum UserAPI {
case editSentence(sentence: String)
case getNicknameAvailable(nickname: String)
case changeNickname(nickname: String)
case reissuance
case getUserMainInfo
case withdrawal
}
Expand All @@ -37,8 +36,6 @@ extension UserAPI: BaseAPI {
return "nickname"
case .getNicknameAvailable(let nickname):
return "nickname/\(nickname)"
case .reissuance:
return "refresh"
case .getUserMainInfo:
return "/main"
case .withdrawal:
Expand All @@ -51,7 +48,7 @@ extension UserAPI: BaseAPI {
switch self {
case .getNicknameAvailable, .getUserMainInfo, .fetchSoptampUser:
return .get
case .editSentence, .changeNickname, .reissuance:
case .editSentence, .changeNickname:
return .patch
case .withdrawal:
return .delete
Expand All @@ -66,8 +63,6 @@ extension UserAPI: BaseAPI {
params["nickname"] = nickname
case .editSentence(let sentence):
params["profileMessage"] = sentence
case .reissuance:
params["refreshToken"] = UserDefaultKeyList.Auth.appRefreshToken
default: break
}
return params
Expand All @@ -82,7 +77,7 @@ extension UserAPI: BaseAPI {

public var task: Task {
switch self {
case .changeNickname, .editSentence, .reissuance:
case .changeNickname, .editSentence:
return .requestParameters(parameters: bodyParameters ?? [:], encoding: parameterEncoding)
default:
return .requestPlain
Expand All @@ -91,8 +86,6 @@ extension UserAPI: BaseAPI {

public var validationType: ValidationType {
switch self {
case .reissuance:
return .none
default : return .successCodes
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Alamofire
public class AlamoInterceptor: RequestInterceptor {

public typealias AdapterResult = Swift.Result<URLRequest, Error>
private var userService = DefaultUserService()
private var authService = DefaultAuthService()

public func retry(_ request: Alamofire.Request, for session: Alamofire.Session, dueTo error: Swift.Error, completion: @escaping (RetryResult) -> Void) {
// token 재발급 API가 아니며 && 로그인 실패가 아니며 && 토큰이 만료된 경우(401)
Expand All @@ -28,7 +28,7 @@ public class AlamoInterceptor: RequestInterceptor {
return
}

userService.reissuance { reissuanceSuccessed in
authService.reissuance { reissuanceSuccessed in
if reissuanceSuccessed {
print("토큰 갱신 성공: ", request.request?.url)
completion(.retry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,45 @@ import Combine

import Alamofire
import Moya
import Core

public typealias DefaultAuthService = BaseService<AuthAPI>

public protocol AuthService {
func signIn(token: String) -> AnyPublisher<SignInEntity, Error>
func reissuance(completion: @escaping ((Bool) -> Void))
func reissuance() -> AnyPublisher<SignInEntity, Error>
}

extension DefaultAuthService: AuthService {
public func signIn(token: String) -> AnyPublisher<SignInEntity, Error> {
return requestObjectWithNetworkErrorInCombine(.signIn(token: token))
}

public func reissuance(completion: @escaping ((Bool) -> Void)) {
provider.request(.reissuance) { response in
switch response {
case .success(let value):
do {
let decoder = JSONDecoder()
let body = try decoder.decode(SignInEntity.self, from: value.data)
UserDefaultKeyList.Auth.appAccessToken = body.accessToken
UserDefaultKeyList.Auth.appRefreshToken = body.refreshToken
UserDefaultKeyList.Auth.playgroundToken = body.playgroundToken
UserDefaultKeyList.Auth.isActiveUser = body.status == .active
? true
: false
completion(true)
} catch {
completion(false)
}
case .failure:
completion(false)
}
}
}

public func reissuance() -> AnyPublisher<SignInEntity, Error> {
requestObjectInCombine(.reissuance)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public protocol UserService {
func changeNickname(nickname: String) -> AnyPublisher<Int, Error>
func getUserMainInfo() -> AnyPublisher<MainEntity, Error>
func withdraw() -> AnyPublisher<Int, Error>
func reissuance(completion: @escaping ((Bool) -> Void))
func reissuance() -> AnyPublisher<SignInEntity, Error>
}

extension DefaultUserService: UserService {
Expand All @@ -51,31 +49,4 @@ extension DefaultUserService: UserService {
public func withdraw() -> AnyPublisher<Int, Error> {
requestObjectInCombineNoResult(.withdrawal)
}

public func reissuance(completion: @escaping ((Bool) -> Void)) {
provider.request(.reissuance) { response in
switch response {
case .success(let value):
do {
let decoder = JSONDecoder()
let body = try decoder.decode(SignInEntity.self, from: value.data)
UserDefaultKeyList.Auth.appAccessToken = body.accessToken
UserDefaultKeyList.Auth.appRefreshToken = body.refreshToken
UserDefaultKeyList.Auth.playgroundToken = body.playgroundToken
UserDefaultKeyList.Auth.isActiveUser = body.status == .active
? true
: false
completion(true)
} catch {
completion(false)
}
case .failure:
completion(false)
}
}
}

public func reissuance() -> AnyPublisher<SignInEntity, Error> {
requestObjectInCombine(.reissuance)
}
}

0 comments on commit d789555

Please sign in to comment.