Update AuthSessionStore.swift
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user