1

In my app, as soon as it opens I check to see if the user is already authenticated in the viewdidload of the initial view. If they are already authenticated, I perform a segue to the main view. I'm even printing the uid to the log at this time and it's printing correctly.

override func viewDidLoad() {
    super.viewDidLoad()


    if ref.authData != nil {
        let uid = ref.authData.uid
        print(uid)

I then do the same later in the app to get some of the user's info when they click on their profile settings. I write the exact same code to fetch their uid, but this time the uid is returning nil and is crashing with the error

"fatal error: unexpectedly found nil while unwrapping an Optional value"

Is this a firebase or simulator issue?

Edit: This issue has only occurred twice. Otherwise, the code itself works as intended, which makes me wonder whether it is a firebase or simulator issue.

FortuneFaded
  • 1,259
  • 4
  • 20
  • 28

3 Answers3

2

You want to use the observeAuthEventWithBlock method, which is a realtime authentication listener.

override func viewDidAppear() {
  let ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com")

  ref.observeAuthEventWithBlock({ authData in
    if authData != nil {
        // user authenticated
        print(authData)
        self.performSegueWithIdentifier("LoginToOtherView", sender: nil)
    } else {
        // No user is signed in
    }
  })
}
David East
  • 31,526
  • 6
  • 67
  • 82
  • When does ref.authData.uid go out of scope? i.e. Can we have a clarification as when to use ref.authData.uid vs when to use the rev.observeAuthEventWithBlock? – Jay Mar 31 '16 at 17:27
  • After you are authenticated you can always tap into `ref.authData` and it will contain the current user. This is true of any reference you create. – David East Mar 31 '16 at 17:28
  • My apologies for missing something here, but I too check for ref.authData != nil so I want to make sure I'm doing it correctly. In this instance the OP has verified the user is authenticated and later in the same session, that value is nil. Why would observeAuthEventWithBlock be used instead of ref.authData.uid since as you mentioned ref.authData is always valid? – Jay Mar 31 '16 at 17:53
1

About the exact error you are encountering I am not sure, but a Swift-yer way of doing your code (and avoiding your error) would be to call:

if let uid = ref.authData.uid {
    print(uid)
}

This code safely unwraps both authData and the UID.

sschale
  • 5,168
  • 3
  • 29
  • 36
  • Great, thank you, I'm new so I'm always looking for Swift-yer ways of doing things. If I do it in the way above, I can get rid of the if ref.authData != nil ? – FortuneFaded Mar 31 '16 at 03:36
  • 1
    Exactly - the `if let` portion attempts to unwrap (checking that it's not `nil`) each part of the expression. If it fails to, it stops and your program's execution continues without executing the part within `if`. – sschale Mar 31 '16 at 03:37
  • awesome, thanks! Sorry, I don't think I can upvote your answer though – FortuneFaded Mar 31 '16 at 04:10
1

I was having this problem and it took me hours. Then I realized that I'd just forgotten to do this:

ref = FIRDatabase.database().reference()

before

var ref: FIRDatabaseReference!