Skip to content

Commit 0537108

Browse files
refactor: expose string utils on auth service
1 parent 3214349 commit 0537108

4 files changed

Lines changed: 27 additions & 42 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public final class AuthService {
6161
public let auth: Auth
6262
private var listenerManager: AuthListenerManager?
6363
private let googleProvider: GoogleProviderProtocol?
64+
public let string: StringUtils
6465

6566
public init(configuration: AuthConfiguration = AuthConfiguration(), auth: Auth = Auth.auth(),
6667
googleProvider: GoogleProviderProtocol? = nil) {
6768
self.auth = auth
6869
self.configuration = configuration
6970
self.googleProvider = googleProvider
71+
string = StringUtils(bundle: configuration.customStringsBundle ?? Bundle.module)
7072
listenerManager = AuthListenerManager(auth: auth, authEnvironment: self)
7173
}
7274

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Utils/StringUtils.swift

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,56 +14,42 @@ let kEmailsDoNotMatchError = "EmailsDoNotMatchError"
1414
let kUnknownError = "UnknownError"
1515

1616
public class StringUtils {
17-
public static func localizedString(forKey key: String,
18-
configuration: AuthConfiguration) -> String {
19-
if let customStringsBundle = configuration.customStringsBundle {
20-
let localizedString = customStringsBundle.localizedString(
21-
forKey: key,
22-
value: kKeyNotFound,
23-
table: nil
24-
)
25-
26-
if localizedString != key {
27-
return localizedString
28-
}
29-
}
17+
let bundle: Bundle
18+
init(bundle: Bundle) {
19+
self.bundle = bundle
20+
}
3021

31-
return Bundle.module.localizedString(forKey: key, value: nil, table: nil)
22+
public func localizedString(forKey key: String) -> String {
23+
return bundle.localizedString(forKey: key, value: nil, table: nil)
3224
}
3325

34-
public static func localizedErrorMessage(for error: Error,
35-
configuration: AuthConfiguration) -> String {
26+
public func localizedErrorMessage(for error: Error) -> String {
3627
let authError = error as NSError
3728
let errorCode = AuthErrorCode(rawValue: authError.code)
3829
switch errorCode {
3930
case .emailAlreadyInUse:
40-
return StringUtils.localizedString(
41-
forKey: kEmailAlreadyInUseError,
42-
configuration: configuration
31+
return localizedString(
32+
forKey: kEmailAlreadyInUseError
4333
)
4434
case .invalidEmail:
45-
return StringUtils.localizedString(forKey: kInvalidEmailError, configuration: configuration)
35+
return localizedString(forKey: kInvalidEmailError)
4636
case .weakPassword:
47-
return StringUtils.localizedString(forKey: kWeakPasswordError, configuration: configuration)
37+
return localizedString(forKey: kWeakPasswordError)
4838
case .tooManyRequests:
49-
return StringUtils.localizedString(
50-
forKey: kSignUpTooManyTimesError,
51-
configuration: configuration
39+
return localizedString(
40+
forKey: kSignUpTooManyTimesError
5241
)
5342
case .wrongPassword:
54-
return StringUtils.localizedString(
55-
forKey: kWrongPasswordError,
56-
configuration: configuration
43+
return localizedString(
44+
forKey: kWrongPasswordError
5745
)
5846
case .userNotFound:
59-
return StringUtils.localizedString(
60-
forKey: kUsersNotFoundError,
61-
configuration: configuration
47+
return localizedString(
48+
forKey: kUsersNotFoundError
6249
)
6350
case .userDisabled:
64-
return StringUtils.localizedString(
65-
forKey: kAccountDisabledError,
66-
configuration: configuration
51+
return localizedString(
52+
forKey: kAccountDisabledError
6753
)
6854
default:
6955
return error.localizedDescription

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ public struct EmailAuthView {
3737
do {
3838
try await authService.signIn(withEmail: email, password: password)
3939
} catch {
40-
errorMessage = StringUtils.localizedErrorMessage(
41-
for: error,
42-
configuration: authService.configuration
40+
errorMessage = authService.string.localizedErrorMessage(
41+
for: error
4342
)
4443
}
4544
}
@@ -48,9 +47,8 @@ public struct EmailAuthView {
4847
do {
4948
try await authService.createUser(withEmail: email, password: password)
5049
} catch {
51-
errorMessage = StringUtils.localizedErrorMessage(
52-
for: error,
53-
configuration: authService.configuration
50+
errorMessage = authService.string.localizedErrorMessage(
51+
for: error
5452
)
5553
}
5654
}

FirebaseSwiftUI/FirebaseGoogleSwiftUI/Sources/Views/GoogleButtonView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ public struct GoogleButtonView {
1212
do {
1313
try await authService.signInWithGoogle()
1414
} catch {
15-
errorMessage = StringUtils.localizedErrorMessage(
16-
for: error,
17-
configuration: authService.configuration
15+
errorMessage = authService.string.localizedErrorMessage(
16+
for: error
1817
)
1918
}
2019
}

0 commit comments

Comments
 (0)