Skip to content

Commit f0f3210

Browse files
committed
Updated auth way.
1 parent 0f4e592 commit f0f3210

File tree

4 files changed

+152
-7
lines changed

4 files changed

+152
-7
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
3+
public enum FirebaseAuthProvider: String, CaseIterable {
4+
5+
case apple
6+
case google
7+
case email
8+
9+
public var id: String { rawValue }
10+
11+
static func getByBaseURL(_ url: String) -> FirebaseAuthProvider? {
12+
for provider in Self.allCases {
13+
if url == provider.baseURL {
14+
return provider
15+
}
16+
}
17+
print("FirebaseWrapper: Can't get provider by web url \(url)")
18+
return nil
19+
}
20+
21+
var baseURL: String {
22+
switch self {
23+
case .apple:
24+
return "apple.com"
25+
case .google:
26+
return "google.com"
27+
case .email:
28+
return ""
29+
}
30+
}
31+
}

Sources/FirebaseWrapperAuth/FirebaseWrapperAuth.swift

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ import SwiftBoost
88
public class FirebaseWrapperAuth {
99

1010
public static func configure(authDidChangedWork: (() -> Void)? = nil) {
11-
debug("FirebaseWrapper: Auth configure")
11+
// Logs
12+
debug("FirebaseWrapper: Auth configure.")
13+
debug("FirebaseWrapper: Current state isAuthed: " + (isAuthed ? "true" : "false"))
14+
if isAuthed {
15+
debug("FirebaseWrapper: userID: \(userID ?? .empty), email: \(userEmail ?? "nil")")
16+
}
17+
// Observer Clean
1218
if let observer = shared.observer {
1319
Auth.auth().removeStateDidChangeListener(observer)
1420
}
21+
// Configure Observer
1522
shared.observer = Auth.auth().addStateDidChangeListener { auth, user in
1623
let newState = isAuthed
1724
let cachedState = isAuthedStored
@@ -23,7 +30,14 @@ public class FirebaseWrapperAuth {
2330
}
2431

2532
public static func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
26-
return GIDSignIn.sharedInstance.handle(url)
33+
let handleEmailWay = handleSignInWithEmailURL(url) { error in
34+
// Process auth after handle email
35+
}
36+
if handleEmailWay {
37+
return true
38+
} else {
39+
return GIDSignIn.sharedInstance.handle(url)
40+
}
2741
}
2842

2943
// MARK: - Data
@@ -33,6 +47,17 @@ public class FirebaseWrapperAuth {
3347
public static var userName: String? { Auth.auth().currentUser?.displayName }
3448
public static var userEmail: String? { Auth.auth().currentUser?.email }
3549

50+
public static var providers: [FirebaseAuthProvider] {
51+
guard let providerData = Auth.auth().currentUser?.providerData else { return [] }
52+
var providers: [FirebaseAuthProvider] = []
53+
for providerMeta in providerData {
54+
if let provider = FirebaseAuthProvider.getByBaseURL(providerMeta.providerID) {
55+
providers.append(provider)
56+
}
57+
}
58+
return providers
59+
}
60+
3661
private static var isAuthedStored: Bool {
3762
get { UserDefaults.standard.bool(forKey: "firebase_wrapper_auth_is_authed_stored") }
3863
set { UserDefaults.standard.set(newValue, forKey: "firebase_wrapper_auth_is_authed_stored") }
@@ -47,8 +72,12 @@ public class FirebaseWrapperAuth {
4772
return
4873
}
4974
AppleAuthService.signIn(on: window) { data, appleError in
75+
if let appleError {
76+
debug("FirebaseWrapper: Sign in with Apple error: \(appleError.localizedDescription)")
77+
return
78+
}
5079
guard let data else {
51-
completion?(nil, appleError)
80+
completion?(nil, AuthError.cantMakeData)
5281
return
5382
}
5483
let credential = OAuthProvider.appleCredential(
@@ -64,9 +93,13 @@ public class FirebaseWrapperAuth {
6493

6594
public static func signInWithGoogle(on controller: UIViewController, completion: ((SignInWithAppleData?, Error?) -> Void)?) {
6695
debug("FirebaseWrapper: Auth start sign in with Google")
67-
GoogleAuthService.signInWithGoogle(on: controller) { data, googleError in
96+
GoogleAuthService.signIn(on: controller) { data, googleError in
97+
if let googleError {
98+
debug("FirebaseWrapper: Sign in with Google error: \(googleError.localizedDescription)")
99+
return
100+
}
68101
guard let data else {
69-
completion?(nil, googleError)
102+
completion?(nil, AuthError.cantMakeData)
70103
return
71104
}
72105
let credential = GoogleAuthProvider.credential(withIDToken: data.identityToken, accessToken: data.accessToken)
@@ -76,6 +109,31 @@ public class FirebaseWrapperAuth {
76109
}
77110
}
78111

112+
public static func signInWithEmail(email: String, handleURL: URL, completion: ((Error?) -> Void)?) {
113+
debug("FirebaseWrapper: Auth start sign in with Email")
114+
EmailAuthService.signIn(email: email, handleURL: handleURL) { emailError in
115+
if let emailError {
116+
debug("FirebaseWrapper: Sign in with Email error: \(emailError.localizedDescription)")
117+
}
118+
completion?(emailError)
119+
}
120+
}
121+
122+
static func handleSignInWithEmailURL(_ url: URL, completion: ((Error?) -> Void)?) -> Bool {
123+
guard Auth.auth().isSignIn(withEmailLink: url.absoluteString) else {
124+
completion?(AuthError.cantMakeData)
125+
return false
126+
}
127+
guard let processingEmail = EmailAuthService.processingEmail else {
128+
completion?(AuthError.cantMakeData)
129+
return false
130+
}
131+
Auth.auth().signIn(withEmail: processingEmail, link: url.absoluteString) { user, emailError in
132+
completion?(emailError)
133+
}
134+
return true
135+
}
136+
79137
public static func signOut(completion: @escaping (Error?)->Void) {
80138
debug("FirebaseWrapper: Auth start sign out")
81139
do {
@@ -89,11 +147,42 @@ public class FirebaseWrapperAuth {
89147
public static func delete(on controller: UIViewController, completion: @escaping (Error?)->Void) {
90148
debug("FirebaseWrapper: Auth start delete")
91149

150+
// Basic Delete Account
151+
let deleteAccountAction = {
152+
do {
153+
try await Auth.auth().currentUser?.delete()
154+
DispatchQueue.main.async {
155+
completion(nil)
156+
}
157+
} catch {
158+
DispatchQueue.main.async {
159+
completion(error)
160+
}
161+
}
162+
}
163+
164+
let providers = providers
165+
if providers.isEmpty {
166+
#warning("del account error make enum")
167+
completion(AuthError.cantMakeData)
168+
return
169+
}
170+
171+
let provider = providers.first!
172+
173+
// Check which provider for reauth https://firebase.google.com/docs/auth/ios/manage-users#delete_a_user
174+
// Check if sign in with apple for revoke token, maybe first do it and later check others
175+
// after auth deelte account
176+
// FIRAuthErrorCodeCredentialTooOld error when delete call reauth only when happen this error
177+
178+
/*
92179
guard let providers = Auth.auth().currentUser?.providerData, !providers.isEmpty else {
93180
completion(AuthError.cantMakeData)
94181
return
95182
}
96183

184+
185+
97186
// Basic Delete Account
98187
let deleteAccountAction = {
99188
do {
@@ -137,7 +226,7 @@ public class FirebaseWrapperAuth {
137226
// Basic Delete Account
138227
await deleteAccountAction()
139228
}
140-
}
229+
}*/
141230
}
142231

143232
public static func setDisplayName(_ name: String, completion: @escaping (Error?)->Void) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import UIKit
2+
import FirebaseCore
3+
import FirebaseAuth
4+
5+
class EmailAuthService {
6+
7+
static func signIn(email: String, handleURL: URL, completion: ((Error?) -> Void)?) {
8+
9+
let actionCodeSettings = ActionCodeSettings()
10+
actionCodeSettings.url = handleURL
11+
actionCodeSettings.handleCodeInApp = true
12+
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
13+
14+
Auth.auth().sendSignInLink(toEmail: email, actionCodeSettings: actionCodeSettings) { emailError in
15+
completion?(emailError)
16+
}
17+
}
18+
19+
// MARK: - Private
20+
21+
static var processingEmail: String? {
22+
get { UserDefaults.standard.string(forKey: "firebase_wrapper_sign_in_process_email") }
23+
set { UserDefaults.standard.setValue(newValue, forKey: "firebase_wrapper_sign_in_process_email") }
24+
}
25+
}

Sources/FirebaseWrapperAuth/Services/GoogleAuthService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import GoogleSignIn
44

55
class GoogleAuthService {
66

7-
public static func signInWithGoogle(on controller: UIViewController, completion: ((SignInWithGoogleData?, Error?) -> Void)?) {
7+
public static func signIn(on controller: UIViewController, completion: ((SignInWithGoogleData?, Error?) -> Void)?) {
88
guard let clientID = FirebaseApp.app()?.options.clientID else {
99
completion?(nil, AuthError.cantMakeData)
1010
return

0 commit comments

Comments
 (0)