Skip to content

Commit 836172e

Browse files
authored
Merge pull request #53 from YAPP-Github/BOOK-48-feature/#27
feat: 애플 로그인 환경 세팅
2 parents 73aa4f5 + d719803 commit 836172e

17 files changed

Lines changed: 138 additions & 87 deletions

src/Projects/BKData/Project.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let project = Project.project(
1717
.domain(),
1818
.external(dependency: .KakaoSDKCommon),
1919
.external(dependency: .KakaoSDKAuth),
20-
.external(dependency: .KakaoSDKUser),
20+
.external(dependency: .KakaoSDKUser)
2121
]
2222
),
2323
Target.target(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright © 2025 Booket. All rights reserved
2+
3+
import UIKit
4+
5+
public extension UIApplication {
6+
var keyWindowInActiveScene: UIWindow? {
7+
connectedScenes
8+
.compactMap { $0 as? UIWindowScene }
9+
.first { $0.activationState == .foregroundActive }?
10+
.windows
11+
.first(where: \.isKeyWindow)
12+
}
13+
}

src/Projects/BKData/Sources/Interface/KakaoUserAPIProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public protocol KakaoUserAPIProtocol {
2525

2626
/// 카카오 계정 로그인
2727
func loginWithKakaoAccount(
28-
prompts : [KakaoAuthPrompt]?,
28+
prompts: [KakaoAuthPrompt]?,
2929
channelPublicIds: [String]?,
3030
serviceTerms: [String]?,
3131
nonce: String?,
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright © 2025 Booket. All rights reserved
22

33
public protocol KeyValueStorage {
4-
func save<T: Encodable>(_ data: T, for account: String) throws
5-
func load<T: Decodable>(for account: String) throws -> T
6-
func delete(for account: String) throws
4+
func save<T: Encodable>(_ data: T, for account: String) throws
5+
func load<T: Decodable>(for account: String) throws -> T
6+
func delete(for account: String) throws
77
}

src/Projects/BKData/Sources/RepositoryImpl/DefaultAuthRepository.swift renamed to src/Projects/BKData/Sources/Repository/DefaultAuthRepository.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import BKDomain
44
import Combine
55
import Foundation
66

7-
//public final class DefaultAuthRepository: AuthRepository
7+
// public final class DefaultAuthRepository: AuthRepository
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright © 2025 Booket. All rights reserved
2+
3+
import AuthenticationServices
4+
import BKDomain
5+
import Combine
6+
import UIKit
7+
8+
final class AppleLoginDelegateProxy: NSObject {
9+
private var currentSubject: PassthroughSubject<String, AuthError>?
10+
private var currentController: ASAuthorizationController?
11+
}
12+
13+
extension AppleLoginDelegateProxy {
14+
func startAuthorization() -> AnyPublisher<String, AuthError> {
15+
cancelPreviousAuthorization()
16+
17+
let subject = PassthroughSubject<String, AuthError>()
18+
self.currentSubject = subject
19+
20+
let controller = makeAuthorizationController()
21+
self.currentController = controller
22+
23+
controller.performRequests()
24+
25+
return subject
26+
.receive(on: DispatchQueue.main)
27+
.handleEvents(receiveCompletion: { [weak self] _ in
28+
self?.cleanup()
29+
}, receiveCancel: { [weak self] in
30+
self?.cleanup()
31+
})
32+
.eraseToAnyPublisher()
33+
}
34+
}
35+
36+
private extension AppleLoginDelegateProxy {
37+
func cancelPreviousAuthorization() {
38+
currentSubject?.send(completion: .failure(.sdkError(message: "이전 인증 요청 정리")))
39+
cleanup()
40+
}
41+
42+
func makeAuthorizationController() -> ASAuthorizationController {
43+
let request = ASAuthorizationAppleIDProvider().createRequest()
44+
request.requestedScopes = [.email, .fullName]
45+
46+
let controller = ASAuthorizationController(authorizationRequests: [request])
47+
controller.delegate = self
48+
controller.presentationContextProvider = self
49+
return controller
50+
}
51+
52+
func cleanup() {
53+
currentSubject = nil
54+
currentController = nil
55+
}
56+
}
57+
58+
extension AppleLoginDelegateProxy: ASAuthorizationControllerDelegate {
59+
func authorizationController(
60+
controller: ASAuthorizationController,
61+
didCompleteWithAuthorization authorization: ASAuthorization
62+
) {
63+
guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential,
64+
let tokenData = credential.identityToken,
65+
let token = String(data: tokenData, encoding: .utf8)
66+
else {
67+
currentSubject?.send(completion: .failure(.missingToken))
68+
return
69+
}
70+
71+
currentSubject?.send(token)
72+
currentSubject?.send(completion: .finished)
73+
}
74+
75+
func authorizationController(
76+
controller: ASAuthorizationController,
77+
didCompleteWithError error: Error
78+
) {
79+
currentSubject?.send(completion: .failure(.sdkError(message: error.localizedDescription)))
80+
}
81+
}
82+
83+
extension AppleLoginDelegateProxy: ASAuthorizationControllerPresentationContextProviding {
84+
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
85+
return UIApplication.shared.keyWindowInActiveScene ?? UIWindow()
86+
}
87+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright © 2025 Booket. All rights reserved
2+
3+
import AuthenticationServices
4+
import BKDomain
5+
import Combine
6+
import Foundation
7+
8+
public final class AppleLoginService: NSObject, SocialLoginService {
9+
public let provider: AuthProvider = .apple
10+
private let delegateProxy: AppleLoginDelegateProxy
11+
12+
public override init() {
13+
self.delegateProxy = AppleLoginDelegateProxy()
14+
super.init()
15+
}
16+
17+
public func login() -> AnyPublisher<String, AuthError> {
18+
delegateProxy.startAuthorization()
19+
}
20+
}

src/Projects/BKData/Sources/ServiceImpl/KakaoLoginService.swift renamed to src/Projects/BKData/Sources/Service/KakaoLoginService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import KakaoSDKUser
1010
/// 카카오 SDK 로그인 과정을 추상화한 서비스 구현
1111
/// 앱 설치 여부에 따라 앱 로그인 또는 계정 로그인을 수행하고,
1212
/// 발급된 accessToken만 방출합니다.
13-
public final class KakaoLoginService: SocialLoginService {
13+
public final class KakaoLoginService: NSObject, SocialLoginService {
1414
/// 이 서비스가 담당할 소셜 공급자
1515
public let provider: AuthProvider
1616

src/Projects/BKData/Sources/ServiceImpl/AppleLoginService.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Projects/BKDomain/Sources/Impl/DefaultSignInUseCase.swift

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)