Skip to content

Commit 323eef6

Browse files
feat: delete user for facebook
1 parent 1ed8dcc commit 323eef6

File tree

5 files changed

+41
-20
lines changed

5 files changed

+41
-20
lines changed

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AccountService+Email.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ protocol EmailPasswordOperationReauthentication {
66
}
77

88
extension EmailPasswordOperationReauthentication {
9-
func reauthenticate() async throws -> AuthenticationToken {
9+
@MainActor func reauthenticate() async throws -> AuthenticationToken {
1010
guard let user = Auth.auth().currentUser else {
1111
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
1212
}
@@ -28,6 +28,7 @@ extension EmailPasswordOperationReauthentication {
2828
}
2929
}
3030

31+
@MainActor
3132
class EmailPasswordDeleteUserOperation: AuthenticatedOperation,
3233
EmailPasswordOperationReauthentication {
3334
let passwordPrompt: PasswordPromptCoordinator

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AccountService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum AuthenticationToken {
1616
case firebase(String)
1717
}
1818

19+
@MainActor
1920
public protocol AuthenticatedOperation {
2021
func callAsFunction(on user: User) async throws
2122
func reauthenticate() async throws -> AuthenticationToken

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public protocol GoogleProviderAuthUIProtocol: ExternalAuthProvider {
1212

1313
public protocol FacebookProviderAuthUIProtocol: ExternalAuthProvider {
1414
@MainActor func signInWithFacebook(isLimitedLogin: Bool) async throws -> AuthCredential
15+
@MainActor func deleteUser(user: User) async throws
1516
}
1617

1718
public protocol PhoneAuthProviderAuthUIProtocol: ExternalAuthProvider {
@@ -200,7 +201,8 @@ public final class AuthService {
200201
try await linkAccounts(credentials: credentials)
201202
} else {
202203
do {
203-
try await auth.signIn(with: credentials)
204+
let result = try await auth.signIn(with: credentials)
205+
signedInCredential = result.credential
204206
updateAuthenticationState()
205207
} catch {
206208
authenticationState = .unauthenticated
@@ -232,9 +234,13 @@ public final class AuthService {
232234
public extension AuthService {
233235
func deleteUser() async throws {
234236
do {
235-
if let user = auth.currentUser {
236-
let operation = EmailPasswordDeleteUserOperation(passwordPrompt: passwordPrompt)
237-
try await operation(on: user)
237+
if let user = auth.currentUser, let providerId = signedInCredential?.provider {
238+
if providerId == "password" {
239+
let operation = EmailPasswordDeleteUserOperation(passwordPrompt: passwordPrompt)
240+
try await operation(on: user)
241+
} else if providerId == "facebook.com" {
242+
try await safeFacebookProvider.deleteUser(user: user)
243+
}
238244
}
239245

240246
} catch {
@@ -276,7 +282,8 @@ public extension AuthService {
276282
authenticationState = .authenticating
277283

278284
do {
279-
try await auth.createUser(withEmail: email, password: password)
285+
let result = try await auth.createUser(withEmail: email, password: password)
286+
signedInCredential = result.credential
280287
updateAuthenticationState()
281288
} catch {
282289
authenticationState = .unauthenticated

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/AccountService+Facebook.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
import FirebaseAuthSwiftUI
1010
import Observation
1111

12-
protocol FacebookOperationReauthentication {}
12+
protocol FacebookOperationReauthentication {
13+
var facebookProvider: FacebookProviderAuthUI { get }
14+
}
1315

1416
extension FacebookOperationReauthentication {
15-
func reauthenticate() async throws -> AuthenticationToken {
17+
@MainActor func reauthenticate() async throws -> AuthenticationToken {
1618
guard let user = Auth.auth().currentUser else {
1719
throw AuthServiceError.reauthenticationRequired("No user currently signed-in")
1820
}
1921

2022
do {
21-
let credential = try await FacebookProviderAuthUI.shared
22-
.signInWithFacebook(isLimitedLogin: FacebookProviderAuthUI.shared.isLimitedLogin)
23+
let credential = try await facebookProvider
24+
.signInWithFacebook(isLimitedLogin: facebookProvider.isLimitedLogin)
2325
try await user.reauthenticate(with: credential)
2426

2527
return .firebase("")
@@ -29,8 +31,13 @@ extension FacebookOperationReauthentication {
2931
}
3032
}
3133

32-
class FacebookDeleteUserOperation: AuthenticatedOperation, FacebookOperationReauthentication {
33-
init() {}
34+
@MainActor
35+
class FacebookDeleteUserOperation: AuthenticatedOperation,
36+
@preconcurrency FacebookOperationReauthentication {
37+
let facebookProvider: FacebookProviderAuthUI
38+
init(facebookProvider: FacebookProviderAuthUI) {
39+
self.facebookProvider = facebookProvider
40+
}
3441

3542
func callAsFunction(on user: User) async throws {
3643
try await callAsFunction(on: user) {

FirebaseSwiftUI/FirebaseFacebookSwiftUI/Sources/Services/FacebookProviderAuthUI.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ public enum FacebookProviderError: Error {
1616
case authenticationToken(String)
1717
}
1818

19-
public class FacebookProviderAuthUI: FacebookProviderAuthUIProtocol, @unchecked Sendable {
19+
public class FacebookProviderAuthUI: FacebookProviderAuthUIProtocol {
2020
public let id: String = "facebook"
2121
let scopes: [String]
2222
let shortName = "Facebook"
2323
let providerId = "facebook.com"
2424
private let loginManager = LoginManager()
25-
private var rawNonce: String
26-
private var shaNonce: String
25+
private var rawNonce: String?
26+
private var shaNonce: String?
2727
// Needed for reauthentication
28-
@MainActor public var isLimitedLogin: Bool = true
28+
var isLimitedLogin: Bool = true
2929

3030
@MainActor private static var _shared: FacebookProviderAuthUI?
3131

@@ -42,24 +42,29 @@ public class FacebookProviderAuthUI: FacebookProviderAuthUIProtocol, @unchecked
4242

4343
private init(scopes: [String]? = nil) {
4444
self.scopes = scopes ?? kDefaultFacebookScopes
45-
rawNonce = CommonUtils.randomNonce()
46-
shaNonce = CommonUtils.sha256Hash(of: rawNonce)
4745
}
4846

4947
@MainActor public func authButton() -> AnyView {
5048
AnyView(SignInWithFacebookButton())
5149
}
5250

51+
public func deleteUser(user: User) async throws {
52+
let operation = FacebookDeleteUserOperation(facebookProvider: self)
53+
try await operation(on: user)
54+
}
55+
5356
@MainActor public func signInWithFacebook(isLimitedLogin: Bool) async throws -> AuthCredential {
5457
let loginType: LoginTracking = isLimitedLogin ? .limited : .enabled
5558
self.isLimitedLogin = isLimitedLogin
5659

5760
guard let configuration: LoginConfiguration = {
5861
if loginType == .limited {
62+
rawNonce = CommonUtils.randomNonce()
63+
shaNonce = CommonUtils.sha256Hash(of: rawNonce!)
5964
return LoginConfiguration(
6065
permissions: scopes,
6166
tracking: loginType,
62-
nonce: shaNonce
67+
nonce: shaNonce!
6368
)
6469
} else {
6570
return LoginConfiguration(
@@ -116,7 +121,7 @@ public class FacebookProviderAuthUI: FacebookProviderAuthUIProtocol, @unchecked
116121
if let idToken = AuthenticationToken.current {
117122
let credential = OAuthProvider.credential(withProviderID: providerId,
118123
idToken: idToken.tokenString,
119-
rawNonce: rawNonce)
124+
rawNonce: rawNonce!)
120125
return credential
121126
} else {
122127
throw FacebookProviderError

0 commit comments

Comments
 (0)