Mohammad Yunus
Mohammad Yunus

Reputation: 214

how to check is user is log in in different view controllers in firebase

I am making an app where user can upload files on cloud and retrieve later, but I am stuck.

My question is how to check if user is logged in or not, if login page should be my view controller and every time a user opens the app they have to login or is there some way we can skip this procedure?

I tried making home page an initial view controller and checking in view didload if there is any user or not using auth.auth().currentuser.uid but I don't feel good about using it please any help would be appreciated.

I am new to firebase

Upvotes: 0

Views: 580

Answers (3)

a.palo
a.palo

Reputation: 288

I think the below code might help you.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        configureFirebase()
        configureApplicationScreen() 
        return true
}

// To determine which screen will be displayed when application is launched
func configureApplicationScreen() {

        guard let rootNav = window?.rootViewController as? UINavigationController else {
            return
        }

         // Might need to change the code inside if let depending your requirement
        if let _ = Auth.auth().currentUser, UserDefaults.isUserLoggedIn {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let sideMenuVC = storyboard.instantiateViewController(withIdentifier: "SideMenuController")
            rootNav.navigationBar.isHidden = true
            rootNav.addChild(sideMenuVC)
        }
    }

extension UserDefaults {

    static var isUserLoggedIn: Bool {
        get {
            return UserDefaults.standard.bool(forKey: "IsUserLoggedIn")
        }
        set {
            UserDefaults.standard.set(newValue, forKey: "IsUserLoggedIn")
            UserDefaults.standard.synchronize()
        }

    }

}

Once user is logged in you need to save the status in your UserDefault like "UserDefaults.isUserLoggedIn = true" and you need to check it when the application is launched in the AppDelegate class as stated above.

Upvotes: 0

Picker
Picker

Reputation: 786

if Auth.auth().currentuser.uid != nil {
   //success code 
}

in AppDelegate in didFinishLaunchingWithOptions

if Auth.auth().currentUser?.uid != nil { 
self.window?.rootViewController = UINavigationController.init(rootViewController: viewController1()) 
} else {
self.window?.rootViewController = UINavigationController.init(rootViewController: viewController2())
}

Upvotes: 2

Daniel Ghrenassia
Daniel Ghrenassia

Reputation: 51

to check if user is logged in with firebase, you need to implement this in the didFinishLaunchingWithOptions in the appDelegate

if Auth.auth().currentUser != nil {
        // the user is logged in, you can now redirect to another view controller
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "YourStoryboardVCName") as! YourViewController
        self.window?.rootViewController = vc
    }

Upvotes: 0

Related Questions