From 60723a5118170e399f570f1d5b04930322082363 Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 14 Dec 2021 14:49:21 -0800 Subject: [PATCH] Update AuthSessionStore.swift --- nirvana-ios/Auth/AuthSessionStore.swift | 81 +++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/nirvana-ios/Auth/AuthSessionStore.swift b/nirvana-ios/Auth/AuthSessionStore.swift index a47178b..3a3beb1 100644 --- a/nirvana-ios/Auth/AuthSessionStore.swift +++ b/nirvana-ios/Auth/AuthSessionStore.swift @@ -9,6 +9,7 @@ import Foundation import Firebase import Combine import FirebaseAuth +import GoogleSignIn enum SessionState { case isAuthenticated @@ -22,24 +23,81 @@ protocol SessionStore { func signInOrCreateUser() func logOut() func unbind() + func setupAuthListen() } final class AuthSessionStore: ObservableObject, SessionStore { @Published var user : User? @Published var sessionState: SessionState = SessionState.isLoggedOut + private var GIDconfig:GIDConfiguration + private var handler: AuthStateDidChangeListenerHandle? init() { + // create google sign in configuration object + let clientID = FirebaseApp.app()?.options.clientID + self.GIDconfig = GIDConfiguration(clientID: clientID!) + self.setupAuthListen() } func signInOrCreateUser() { - + if GIDSignIn.sharedInstance.currentUser == nil { + guard + let viewController = UIApplication.shared.windows.first!.rootViewController + else { + return + } + + GIDSignIn.sharedInstance.signIn(with: self.GIDconfig, presenting: viewController) + {[self] user, err in + + if err != nil { + print(err!.localizedDescription) + return + } + + guard + let authentication = user?.authentication, + let idToken = authentication.idToken + else { + return + } + + let credential = GoogleAuthProvider.credential(withIDToken: idToken, + accessToken: authentication.accessToken) + + // sign in with firebase + self.firebaseAuth(withCredentials: credential) + } + } + } + + func firebaseAuth(withCredentials authCred: AuthCredential) { + Auth.auth().signIn(with: authCred) { result, error in + if error != nil { + print(error!.localizedDescription) + } + + guard let user = result?.user else { + return + } + + // User is signed in + print(user.displayName ?? "Success!") + print(user) + } } func logOut() { - try? Auth.auth().signOut() + let firebaseAuth = Auth.auth() + + do { + try firebaseAuth.signOut() + } catch let signOutError as NSError { + print("Error signing out: %@", signOutError) + } } func unbind () { @@ -47,12 +105,12 @@ final class AuthSessionStore: ObservableObject, SessionStore { Auth.auth().removeStateDidChangeListener(handle) } } -} - -private extension AuthSessionStore { + func setupAuthListen() { // monitor authentication changes using firebase self.handler = Auth.auth().addStateDidChangeListener { (auth, user) in + print("auth listener activated") + if let user = user { // if we have a user, create a new user model print("Got user: \(user)") @@ -65,3 +123,16 @@ private extension AuthSessionStore { } } } + +extension UIApplication { + func getRootViewController() -> UIViewController { + guard let screen = UIApplication.shared.connectedScenes.first as? UIWindowScene else { return .init() } + + guard let root = screen.windows.first?.rootViewController else { + return .init() + } + + return root + } + +}