Konsy Konsy - 1 year ago 414
iOS Question

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.

self.storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let currentUser = FIRAuth.auth()?.currentUser!
if currentUser != nil
self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tabVC")
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.

Answer Source

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