Skip to content

Commit 60ab2ff

Browse files
committed
♻️ Simplify the code to match the script
1 parent 7249548 commit 60ab2ff

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

fundamentals/apple/auth-google-sign-in/final/Favourites/Shared/Auth/AuthenticationViewModel.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,38 +146,44 @@ extension AuthenticationViewModel {
146146
}
147147
}
148148

149+
enum AuthenticationError: Error {
150+
case tokenError(message: String)
151+
}
152+
149153
extension AuthenticationViewModel {
150-
func signInWithGoogle() {
154+
func signInWithGoogle() async -> Bool {
151155
guard let clientID = FirebaseApp.app()?.options.clientID else {
152156
fatalError("No client ID found in Firebase configuration")
153157
}
154158
let config = GIDConfiguration(clientID: clientID)
155159
GIDSignIn.sharedInstance.configuration = config
156160

157-
guard let viewController = UIApplication.shared.rootViewController else {
161+
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
162+
let window = windowScene.windows.first,
163+
let rootViewController = window.rootViewController else {
158164
print("There is no root view controller!")
159-
return
165+
return false
160166
}
161167

162-
Task {
163168
do {
164-
let user = try await GIDSignIn.sharedInstance.signIn(withPresenting: viewController)
169+
let userAuthentication = try await GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController)
165170

166-
let authentication = user.user
167-
guard let idToken = authentication.idToken else { return }
168-
let accessToken = authentication.accessToken
171+
let user = userAuthentication.user
172+
guard let idToken = user.idToken else { throw AuthenticationError.tokenError(message: "ID token missing") }
173+
let accessToken = user.accessToken
169174

170175
let credential = GoogleAuthProvider.credential(withIDToken: idToken.tokenString,
171-
accessToken: accessToken.tokenString)
176+
accessToken: accessToken.tokenString)
172177

173178
let result = try await Auth.auth().signIn(with: credential)
174179
let firebaseUser = result.user
175180
print("User \(firebaseUser.uid) signed in with email \(firebaseUser.email ?? "unknown")")
181+
return true
176182
}
177183
catch {
178184
print(error.localizedDescription)
179185
self.errorMessage = error.localizedDescription
186+
return false
180187
}
181-
}
182188
}
183189
}

fundamentals/apple/auth-google-sign-in/final/Favourites/Shared/Auth/LoginView.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private enum FocusableField: Hashable {
2828

2929
struct LoginView: View {
3030
@EnvironmentObject var viewModel: AuthenticationViewModel
31+
@Environment(\.colorScheme) var colorScheme
3132
@Environment(\.dismiss) var dismiss
3233

3334
@FocusState private var focus: FocusableField?
@@ -41,7 +42,11 @@ struct LoginView: View {
4142
}
4243

4344
private func signInWithGoogle() {
44-
viewModel.signInWithGoogle()
45+
Task {
46+
if await viewModel.signInWithGoogle() == true {
47+
dismiss()
48+
}
49+
}
4550
}
4651

4752
var body: some View {
@@ -113,7 +118,7 @@ struct LoginView: View {
113118
VStack { Divider() }
114119
}
115120

116-
Button(action: { self.signInWithGoogle() }) {
121+
Button(action: signInWithGoogle) {
117122
Text("Sign in with Google")
118123
.padding(.vertical, 8)
119124
.frame(maxWidth: .infinity)
@@ -122,10 +127,9 @@ struct LoginView: View {
122127
.frame(width: 30, alignment: .center)
123128
}
124129
}
125-
.foregroundColor(.black)
130+
.foregroundColor(colorScheme == .dark ? .white : .black)
126131
.buttonStyle(.bordered)
127132

128-
129133
HStack {
130134
Text("Don't have an account yet?")
131135
Button(action: { viewModel.switchFlow() }) {

fundamentals/apple/auth-google-sign-in/starter/Favourites/Shared/Auth/AuthenticationViewModel.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ extension AuthenticationViewModel {
149149
// MARK: - Google Sign-In
150150

151151
extension AuthenticationViewModel {
152-
func signInWithGoogle() {
152+
func signInWithGoogle() async -> Bool {
153+
return false // replace this with the implementation
153154
}
154155
}

fundamentals/apple/auth-google-sign-in/starter/Favourites/Shared/Auth/LoginView.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private enum FocusableField: Hashable {
2828

2929
struct LoginView: View {
3030
@EnvironmentObject var viewModel: AuthenticationViewModel
31+
@Environment(\.colorScheme) var colorScheme
3132
@Environment(\.dismiss) var dismiss
3233

3334
@FocusState private var focus: FocusableField?
@@ -41,7 +42,11 @@ struct LoginView: View {
4142
}
4243

4344
private func signInWithGoogle() {
44-
viewModel.signInWithGoogle()
45+
Task {
46+
if await viewModel.signInWithGoogle() == true {
47+
dismiss()
48+
}
49+
}
4550
}
4651

4752
var body: some View {

fundamentals/apple/auth-google-sign-in/starter/Favourites/Shared/FavouritesApp.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class AppDelegate: NSObject, UIApplicationDelegate {
2626
func application(_ application: UIApplication,
2727
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
2828
FirebaseApp.configure()
29-
Auth.auth().useEmulator(withHost:"localhost", port:9099)
3029
return true
3130
}
3231
}

0 commit comments

Comments
 (0)