Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase + Swift: Bypass User Login Screen

So i'm working on a new app and i'm using Firebase to manage all the backend stuff with user logins etc, i've created a login/signup screen for the users when they load the app. What i'm trying to do is load the HomeScreenVC if the user is already logged in and if the user isn't then they would be directed to the login/register vc.

I kind of have it working in the sense that it works if the user is already a currentUser but the issue i'm having is that if the user has never loaded the app then it crashes as it can't detect currentUser.

My code I have in the AppDelegate is:

var window: UIWindow?
var storyboard: UIStoryboard?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    FIRApp.configure()

    self.storyboard =  UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    let currentUser = FIRAuth.auth()?.currentUser!
    if currentUser != nil
    {
        self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tabVC")
    }
    else
    {
        self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginScreen")
    }
    return true
}

This is the error i get: here

Any help would be great! I'm using the latest version of Firebase and Swift 2.

like image 314
Konsy Avatar asked Jul 04 '16 19:07

Konsy


1 Answers

You are force unwrapping nil on that line, causing the crash. Remove the ! from FIRAuth.auth()?.currentUser!

like image 95
stakri Avatar answered Sep 29 '22 20:09

stakri