I was wondering what was the best approach to take on implementing a function that will allow the user to keep him/her self logged in even after the application has been closed, by checking if the token allocated to user is still valid or not.
-
What would be the purpose of this? I don't quite understand. Do you want the user to NOT have to log in again when they restart the app?Ewald– Ewald2018-06-14 18:33:16 +00:00Commented Jun 14, 2018 at 18:33
-
Yes, to avoid to log in all the timeheyr– heyr2018-06-14 18:38:27 +00:00Commented Jun 14, 2018 at 18:38
-
Then you'd need to persist some kind of token on your mobile device. That depends on the API you are talking to. I take it you are using OAuth or not? Take a look at flutter.institute/firebase-signin for a great way of doing this.Ewald– Ewald2018-06-14 18:47:23 +00:00Commented Jun 14, 2018 at 18:47
1 Answer
Upon successful login you can store the token either in the shared preferences using the shared_preferences package or in a more secure area using the flutter_secure_storagepackage.
When the user closes the app, to token will remain in the storage.
Then, on starting the app, you can get the token from the storage and validate it in the server. If the token continues to be valid you can grant access to the app, all of this transparently to the user.
If you use auth plugins like google_sign_in or firebase_auth almost all of this will be automatically integrated.