@@ -39,9 +39,7 @@ final class SignInViewController: BaseViewController {
39
39
}
40
40
41
41
// MARK: - Properties
42
- private var currentNonce : String ?
43
42
private let viewModel : SignInViewModel
44
- private let credentialSub = PublishSubject < ( OAuthCredential , String ) > ( )
45
43
46
44
// MARK: - Initializers
47
45
init ( viewModel: SignInViewModel ) {
@@ -82,22 +80,23 @@ final class SignInViewController: BaseViewController {
82
80
make. centerX. equalToSuperview ( )
83
81
}
84
82
}
85
-
83
+
86
84
override func bind( ) {
85
+ let credential = appleSignInButton. rx. tap
86
+ . asObservable ( )
87
+ . flatMap {
88
+ ASAuthorizationAppleIDProvider ( ) . rx. login ( scope: [ . email] )
89
+ }
90
+ . withUnretained ( self )
91
+ . compactMap { owner, authorization in
92
+ return owner. generateOAuthCredential ( authorization: authorization)
93
+ }
94
+
87
95
let input = SignInViewModel . Input (
88
- signInButtonTap: appleSignInButton. rx. tap. asObservable ( ) ,
89
- credential: credentialSub. asObservable ( )
96
+ credential: credential
90
97
)
91
98
let output = viewModel. transform ( input: input)
92
99
93
-
94
- output. presentSignInWithApple
95
- . asObservable ( )
96
- . subscribe { _ in
97
- self . startSignInWithAppleFlow ( )
98
- }
99
- . disposed ( by: disposeBag)
100
-
101
100
privacyPolicyButton. rx. tap
102
101
. bind ( onNext: { [ weak self] _ in
103
102
self ? . openPrivacyPolicy ( )
@@ -106,62 +105,35 @@ final class SignInViewController: BaseViewController {
106
105
}
107
106
}
108
107
109
- // MARK: - ASAuthorizationControllerDelegate
110
- extension SignInViewController : ASAuthorizationControllerDelegate {
111
- func authorizationController( controller: ASAuthorizationController , didCompleteWithAuthorization authorization: ASAuthorization ) {
108
+ // MARK: - private Function
109
+ private extension SignInViewController {
110
+
111
+ private func generateOAuthCredential( authorization: ASAuthorization ) -> ( OAuthCredential , String ) ? {
112
112
if let appleIDCredential = authorization. credential as? ASAuthorizationAppleIDCredential {
113
- guard let nonce = currentNonce else {
114
- fatalError ( " Invalid state: A login callback was received, but no login request was sent. " )
115
- }
113
+ let nonce = generateRandomNonce ( ) . toSha256 ( )
114
+
116
115
guard let appleIDToken = appleIDCredential. identityToken else {
117
116
Logger . print ( " Unable to fetch identity token " )
118
- return
117
+ return nil
119
118
}
120
119
guard let idTokenString = String ( data: appleIDToken, encoding: . utf8) else {
121
120
Logger . print ( " Unable to serialize token string from data: \( appleIDToken. debugDescription) " )
122
- return
121
+ return nil
123
122
}
124
123
125
124
guard
126
125
let authorizationCode = appleIDCredential. authorizationCode,
127
126
let codeString = String ( data: authorizationCode, encoding: . utf8)
128
127
else {
129
128
Logger . print ( " Unable to serialize token string from authorizationCode " )
130
- return
129
+ return nil
131
130
}
132
131
133
132
let credential = OAuthProvider . credential ( withProviderID: " apple.com " , idToken: idTokenString, rawNonce: nonce)
134
- credentialSub. onNext ( ( credential, codeString) )
133
+
134
+ return ( credential, codeString)
135
135
}
136
- }
137
- }
138
-
139
- // MARK: - ASAuthorizationControllerPresentationContextProviding
140
- extension SignInViewController : ASAuthorizationControllerPresentationContextProviding {
141
- func presentationAnchor( for controller: ASAuthorizationController ) -> ASPresentationAnchor {
142
- return self . view. window ?? ASPresentationAnchor ( )
143
- }
144
- }
145
-
146
- // MARK: - private Function
147
- private extension SignInViewController {
148
- func startSignInWithAppleFlow( ) {
149
- let ( request, nonce) = createRequest ( )
150
- self . currentNonce = nonce
151
-
152
- let authorizationController = ASAuthorizationController ( authorizationRequests: [ request] )
153
- authorizationController. delegate = self
154
- authorizationController. presentationContextProvider = self
155
- authorizationController. performRequests ( )
156
- }
157
-
158
- func createRequest( ) -> ( ASAuthorizationAppleIDRequest , String ) {
159
- let nonce = self . generateRandomNonce ( )
160
- let request = ASAuthorizationAppleIDProvider ( ) . createRequest ( )
161
- request. requestedScopes = [ . fullName, . email]
162
- request. nonce = nonce. toSha256 ( )
163
-
164
- return ( request, nonce)
136
+ return nil
165
137
}
166
138
167
139
// Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce
0 commit comments