-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] 자동로그인 구현
- Loading branch information
Showing
8 changed files
with
256 additions
and
89 deletions.
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
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,78 @@ | ||
// | ||
// AuthInterceptor.swift | ||
// KkuMulKum | ||
// | ||
// Created by 이지훈 on 7/17/24. | ||
// | ||
|
||
import Foundation | ||
|
||
import Moya | ||
import Alamofire | ||
|
||
enum AuthError: Error { | ||
case tokenRefreshFailed | ||
} | ||
|
||
class AuthInterceptor: RequestInterceptor { | ||
let authService: AuthServiceType | ||
let provider: MoyaProvider<LoginTargetType> | ||
|
||
init(authService: AuthServiceType, provider: MoyaProvider<LoginTargetType>) { | ||
self.authService = authService | ||
self.provider = provider | ||
} | ||
|
||
func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) { | ||
guard let accessToken = authService.getAccessToken() else { | ||
completion(.success(urlRequest)) | ||
return | ||
} | ||
|
||
var urlRequest = urlRequest | ||
urlRequest.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization") | ||
completion(.success(urlRequest)) | ||
} | ||
|
||
func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) { | ||
guard let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 else { | ||
completion(.doNotRetry) | ||
return | ||
} | ||
|
||
guard let refreshToken = authService.getRefreshToken() else { | ||
authService.clearTokens() | ||
completion(.doNotRetry) | ||
return | ||
} | ||
|
||
provider.request(.refreshToken(refreshToken: refreshToken)) { [weak self] result in | ||
switch result { | ||
case .success(let response): | ||
do { | ||
let reissueResponse = try response.map(ResponseBodyDTO<ReissueModel>.self) | ||
if reissueResponse.success, let data = reissueResponse.data { | ||
let newAccessToken = data.accessToken | ||
let newRefreshToken = data.refreshToken | ||
self?.authService.saveAccessToken(newAccessToken) | ||
self?.authService.saveRefreshToken(newRefreshToken) | ||
print("Token refreshed successfully in interceptor") | ||
completion(.retry) | ||
} else { | ||
print("Token refresh failed in interceptor: \(reissueResponse.error?.message ?? "Unknown error")") | ||
self?.authService.clearTokens() | ||
completion(.doNotRetry) | ||
} | ||
} catch { | ||
print("Token refresh failed in interceptor: \(error)") | ||
self?.authService.clearTokens() | ||
completion(.doNotRetry) | ||
} | ||
case .failure(let error): | ||
print("Network error during token refresh in interceptor: \(error)") | ||
self?.authService.clearTokens() | ||
completion(.doNotRetry) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.