-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialLoginService.swift
More file actions
213 lines (179 loc) · 6.8 KB
/
Copy pathSocialLoginService.swift
File metadata and controls
213 lines (179 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// SocialLoginService.swift
// Siksha
//
// Created by Codex on 7/5/26.
//
import AuthenticationServices
import Foundation
import GoogleSignIn
import KakaoSDKAuth
import KakaoSDKUser
import UIKit
protocol SocialLoginService: AnyObject {
func login(
provider: LoginProvider,
presentingViewController: UIViewController?
) async throws -> LoginCredential
@MainActor
func handleOpenURL(_ url: URL) -> Bool
}
enum SocialLoginServiceError: Error {
case missingPresentingViewController
case missingCredential
}
final class SocialLoginServiceImpl: SocialLoginService {
private var appleAuthorizationCoordinator: AppleAuthorizationCoordinator?
func login(
provider: LoginProvider,
presentingViewController: UIViewController?
) async throws -> LoginCredential {
switch provider {
case .kakao:
return try await loginWithKakao()
case .google:
return try await loginWithGoogle(presentingViewController: presentingViewController)
case .apple:
return try await loginWithApple(presentingViewController: presentingViewController)
}
}
@MainActor
func handleOpenURL(_ url: URL) -> Bool {
if AuthApi.isKakaoTalkLoginUrl(url) {
return AuthController.handleOpenUrl(url: url)
}
return GIDSignIn.sharedInstance.handle(url)
}
private func loginWithKakao() async throws -> LoginCredential {
let oauthToken = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<OAuthToken, Error>) in
let completion: (OAuthToken?, Error?) -> Void = { oauthToken, error in
if let error {
continuation.resume(throwing: error)
return
}
guard let oauthToken else {
continuation.resume(throwing: SocialLoginServiceError.missingCredential)
return
}
continuation.resume(returning: oauthToken)
}
if UserApi.isKakaoTalkLoginAvailable() {
UserApi.shared.loginWithKakaoTalk(completion: completion)
} else {
UserApi.shared.loginWithKakaoAccount(completion: completion)
}
}
return LoginCredential(provider: .kakao, token: oauthToken.accessToken)
}
private func loginWithGoogle(
presentingViewController: UIViewController?
) async throws -> LoginCredential {
guard let presentingViewController else {
throw SocialLoginServiceError.missingPresentingViewController
}
let signInResult = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<GIDSignInResult, Error>) in
GIDSignIn.sharedInstance.signIn(withPresenting: presentingViewController) { signInResult, error in
if let error {
continuation.resume(throwing: error)
return
}
guard let signInResult else {
continuation.resume(throwing: SocialLoginServiceError.missingCredential)
return
}
continuation.resume(returning: signInResult)
}
}
guard let token = signInResult.user.idToken?.tokenString else {
throw SocialLoginServiceError.missingCredential
}
return LoginCredential(provider: .google, token: token)
}
private func loginWithApple(
presentingViewController: UIViewController?
) async throws -> LoginCredential {
let coordinator = AppleAuthorizationCoordinator(
presentingViewController: presentingViewController
)
appleAuthorizationCoordinator = coordinator
defer {
appleAuthorizationCoordinator = nil
}
return try await coordinator.perform()
}
}
private final class AppleAuthorizationCoordinator: NSObject {
private weak var presentingViewController: UIViewController?
private var continuation: CheckedContinuation<LoginCredential, Error>?
init(presentingViewController: UIViewController?) {
self.presentingViewController = presentingViewController
}
func perform() async throws -> LoginCredential {
try await withCheckedThrowingContinuation { continuation in
self.continuation = continuation
let provider = ASAuthorizationAppleIDProvider()
let request = provider.createRequest()
request.requestedScopes = [.fullName, .email]
let authorizationController = ASAuthorizationController(
authorizationRequests: [request]
)
authorizationController.delegate = self
authorizationController.presentationContextProvider = self
authorizationController.performRequests()
}
}
private func complete(with result: Result<LoginCredential, Error>) {
guard let continuation else {
return
}
self.continuation = nil
switch result {
case .success(let credential):
continuation.resume(returning: credential)
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
extension AppleAuthorizationCoordinator: ASAuthorizationControllerDelegate {
func authorizationController(
controller: ASAuthorizationController,
didCompleteWithAuthorization authorization: ASAuthorization
) {
guard let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential,
let tokenData = appleIDCredential.identityToken,
let token = String(data: tokenData, encoding: .utf8) else {
complete(with: .failure(SocialLoginServiceError.missingCredential))
return
}
complete(
with: .success(
LoginCredential(
provider: .apple,
token: token,
appleUserIdentifier: appleIDCredential.user
)
)
)
}
func authorizationController(
controller: ASAuthorizationController,
didCompleteWithError error: Error
) {
complete(with: .failure(error))
}
}
extension AppleAuthorizationCoordinator: ASAuthorizationControllerPresentationContextProviding {
func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
if let window = presentingViewController?.view.window {
return window
}
if let keyWindow = UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.flatMap(\.windows)
.first(where: \.isKeyWindow) {
return keyWindow
}
return ASPresentationAnchor()
}
}