Skip to content

Commit 1e28979

Browse files
committed
Added sign in with apple SUI button.
1 parent dc85d79 commit 1e28979

File tree

3 files changed

+66
-21
lines changed

3 files changed

+66
-21
lines changed

Sources/FirewrapAuth/FirewrapAuth.swift

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Firewrap
33
import Firebase
44
import FirebaseAuth
55
import SwiftBoost
6+
import GoogleSignIn
67
import UIKit
78

89
public class FirewrapAuth {
@@ -35,6 +36,7 @@ public class FirewrapAuth {
3536

3637

3738
public static func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
39+
3840
let handleEmailWay = handleSignInWithEmailURL(url) { error in
3941
shared.completionSignInViaEmail?(error)
4042
}
@@ -43,7 +45,7 @@ public class FirewrapAuth {
4345
return true
4446
} else {
4547
#if os(iOS) || os(macOS)
46-
return false//GIDSignIn.sharedInstance.handle(url)
48+
return GIDSignIn.sharedInstance.handle(url)
4749
#else
4850
return false
4951
#endif
@@ -95,19 +97,24 @@ public class FirewrapAuth {
9597
completion?(nil, .failed)
9698
return
9799
}
98-
let credential = OAuthProvider.appleCredential(
99-
withIDToken: data.identityToken,
100-
rawNonce: nil,
101-
fullName: data.name
102-
)
103-
Auth.auth().signIn(with: credential) { (authResult, firebaseError) in
104-
if let firebaseError {
105-
printConsole("Sign in with Apple complete with Firebase error: \(firebaseError.localizedDescription)")
106-
completion?(data, .failed)
107-
} else {
108-
printConsole("Sign in with Apple complete")
109-
completion?(data, nil)
110-
}
100+
101+
signInWithApple(with: data, completion: completion)
102+
}
103+
}
104+
105+
static func signInWithApple(with data: SignInWithAppleData, completion: ((SignInWithAppleData?, FirewrapAuthSignInError?) -> Void)?) {
106+
let credential = OAuthProvider.appleCredential(
107+
withIDToken: data.identityToken,
108+
rawNonce: nil,
109+
fullName: data.name
110+
)
111+
Auth.auth().signIn(with: credential) { (authResult, firebaseError) in
112+
if let firebaseError {
113+
printConsole("Sign in with Apple complete with Firebase error: \(firebaseError.localizedDescription)")
114+
completion?(data, .failed)
115+
} else {
116+
printConsole("Sign in with Apple complete successfully")
117+
completion?(data, nil)
111118
}
112119
}
113120
}
@@ -149,7 +156,7 @@ public class FirewrapAuth {
149156
printConsole("Sign in with Email complete with Firebase error: \(emailError.localizedDescription)")
150157
completion?(.failed)
151158
} else {
152-
printConsole("Sign in with Email complete")
159+
printConsole("Sign in with Email success complete")
153160
shared.completionSignInViaEmail = completion
154161
completion?(.mustConfirmViaEmail)
155162
}

Sources/FirewrapAuth/Services/AppleAuthService.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ class AppleAuthService: NSObject, ASAuthorizationControllerDelegate, ASAuthoriza
1515
authorizationController.performRequests()
1616
}
1717

18-
// MARK: - ASAuthorizationControllerDelegate
19-
20-
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
18+
static func parse(_ authorization: ASAuthorization, completion: ((SignInWithAppleData?, FirewrapAuthSignInError?) -> Void)) {
2119
guard
2220
let appleCredential = authorization.credential as? ASAuthorizationAppleIDCredential,
2321
let identityToken = appleCredential.identityToken,
2422
let identityTokenString = String(data: identityToken, encoding: .utf8),
2523
let authorizationCode = appleCredential.authorizationCode,
2624
let authorizationCodeString = String(data: authorizationCode, encoding: .utf8)
2725
else {
28-
// todo parse
29-
completion?(nil, FirewrapAuthSignInError.failed)
26+
completion(nil, FirewrapAuthSignInError.failed)
3027
return
3128
}
3229

@@ -36,7 +33,15 @@ class AppleAuthService: NSObject, ASAuthorizationControllerDelegate, ASAuthoriza
3633
name: appleCredential.fullName,
3734
email: appleCredential.email
3835
)
39-
completion?(data, nil)
36+
completion(data, nil)
37+
}
38+
39+
// MARK: - ASAuthorizationControllerDelegate
40+
41+
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
42+
Self.parse(authorization) { data, error in
43+
self.completion?(data, error)
44+
}
4045
}
4146

4247
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#if canImport(SwiftUI)
2+
import SwiftUI
3+
import AuthenticationServices
4+
5+
@available(iOS 14.0, *)
6+
public struct WrapperSignInWithAppleButton: View {
7+
8+
private let completion: ((SignInWithAppleData?, FirewrapAuthSignInError?) -> Void)?
9+
10+
public init(completion: ((SignInWithAppleData?, FirewrapAuthSignInError?) -> Void)? = nil) {
11+
self.completion = completion
12+
}
13+
14+
public var body: some View {
15+
SignInWithAppleButton { request in
16+
17+
} onCompletion: { result in
18+
switch result {
19+
case .success(let authorisation):
20+
AppleAuthService.parse(authorisation) { data, error in
21+
if let data {
22+
FirewrapAuth.signInWithApple(with: data, completion: completion)
23+
} else {
24+
self.completion?(nil, error ?? .failed)
25+
}
26+
}
27+
case .failure(_):
28+
self.completion?(nil, .failed)
29+
}
30+
}
31+
}
32+
}
33+
#endif

0 commit comments

Comments
 (0)