Swift Firebase and Google login シングルトン作成

SceneDelegate ログインのview周りの処理を書く場所

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
        guard let windowScene = (scene as? UIWindowScene) else { return }

        let window = UIWindow(windowScene: windowScene)
        let vc: UIViewController

//firebaseにログインしていたら
        if let user = Auth.auth().currentUser {

//Google login をチェック
            GIDSignIn.sharedInstance()?.restorePreviousSignIn()
            guard  let email = user.email else { return }

//シングルトンにてログイン
            User.instantiate(with: user.uid, name: user.displayName ?? email, email: email)
 
//遷移
            vc = UIStoryboard(name: DiaryIndexController.className, bundle: nil).instantiateInitialViewController()!

        }
//ログイン画面へ
        else {
            vc = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()!
        }

//あたらしい Viewをつくる、古いのがあればremoveしてnew one install
        window.rootViewController = vc
        self.window = window


//入力キーwindowをつくる
        window.makeKeyAndVisible()

    }

User.instantiate

 import Foundation
import Firebase
import GoogleSignIn

final class User  {

//シングルトン
 private(set) static var loggedIn: User?

   private static var listener: ListenerRegistration? {
        willSet { listener?.remove() }
    }

class func instantiate(with id: String, name: String, email: String) {
        
        listener = Firestore.firestore().collection(USER_REF).document(id).addSnapshotListener { (snapshot, error) in
            if let error = error{
                debugPrint(error)
            }
           
            guard let document = snapshot?.data() else {
                self.createNewUserData(id: id, fullName: name, email: email)
                return
            }
            let isLoggedIn = loggedIn != nil

            if !isLoggedIn {
                loggedIn = User()
                loggedIn?.user_id = id
            }
            loggedIn?.name = document["name"] as? String ?? name //get google name
            loggedIn?.email = document["email"] as? String ?? email//
            loggedIn?.points = document["point"] as? Int ?? 0
            loggedIn?.photo = document["photo"] as? String ?? "" //get google photo

        }
    }

createNewUserData

   private class func createNewUserData(id: String, fullName: String?, email: String?) {
        let fullName = fullName ?? "name"
        let email = email ?? "e-mail"
        let db = Firestore.firestore().collection(USER_REF).document(id)
        db.setData([
            NAME : fullName,
            EMAIL: email,
            CREATED_AT: FieldValue.serverTimestamp(),
            UPDATED_AT: FieldValue.serverTimestamp()
        ])
        
        User.instantiate(with: id, name: fullName, email: email)
    }