简体   繁体   中英

Ios Development: How often should an app check user is still logged in?

Im nearly finished developing my first Swift application in Xcode. I'm using firebase. My question is, should I be checking the user is still logged in when segueing to every view controller?

I currently check in the viewdidload() method on only two of the tab view controllers. Do I have to do this on every page?

I'm using the below code to check user is logged in an segue to the main screen if they're not:

FIRAuth.auth()!.addStateDidChangeListener() { auth, user in
        // 2
        if user != nil {
            // 3
             self.performSegue(withIdentifier: "fromListToHome", sender: self)
        }
    }
}

Its always smart to keep a global instance of a User class where you store all the details, in this case static let shared_Instance = user_Global()

class user_Global {

var refresh_Delegate : user_Data_Refresh?

static let shared_Instance = user_Global()

var is_Authenticated : Bool = false{

    didSet{

        refresh_Delegate?.refresh_User_State()

    }
  }

}

Conform those viewControllers that are dependent on users Auth state to user_Data_Refresh protocol, and check the user_Global.shared_Instance.is_Authenticated bool value

// FIREBASE User Refresh protocol.....


protocol user_Data_Refresh : class {

  func refresh_User_State()

}

Function to check auth state...

func isUserSignedIn(completion_Block : @escaping (_ user_State : Bool?) -> Void){

    Auth.auth().addStateDidChangeListener({ (auth, user) in


        if user != nil{

            print("The user is Authenticated")
            user_Global.shared_Instance.is_Authenticated = true
            completion_Block(true)
            return

        }else{

            print("The user is not Authenticated")
            user_Global.shared_Instance.is_Authenticated = false
            completion_Block(false)
            return

        }

    })
}

Call this function in your appDelegate to get this thread running in your network link...

Basically you check at the start of the app

However when the user makes a call and it fails (reason: Unauthorized) then you handle this by logging him out.

Hope this helps!

Firebase Authentication checks the validity of the user:

  1. When you call signIn... or reauthenticate... .
  2. When the app is restarted and the user was previously signed in.
  3. Every hour after one of these, to see if the status has changed.

Whether you need to check in every view controller, depends on the actions on that view controller. But if what you display depends on whether the user is signed in, then you indeed need to attach a listener in that view controller. This is easiest if you put the code in a class that you then share between those view controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM