Issue
I want to save users facebook login sessions in my app, so the user doesn't have to sign in every time the app reopened. I already did this with google but can not find anything about Facebook. Here is my authentication code.
Future<void> logInWithFacebook() async {
final result = await fbLogin.logIn(['email']);
switch (result.status) {
case FacebookLoginStatus.loggedIn:
final token = result.accessToken.token;
final graphResponse = await http.get(
'https://graph.facebook.com/v2.12/me?fields=name,picture,email&access_token=${token}');
final profile = JSON.jsonDecode(graphResponse.body);
print(profile);
await Navigator.push(context,MaterialPageRoute(builder: (context)=>HomePage()));
setState(() {
userProfile = profile;
isAuth = true;
});
break;
case FacebookLoginStatus.cancelledByUser:
setState(() => isAuth = false);
break;
case FacebookLoginStatus.error:
setState(() => isAuth = false);
break;
}
}
Solution
Save your token or your login state inside Shared Preference. Here is the link to the Flutter Package
SharedPreferences prefs = await SharedPreferences.getInstance();
I would suggest making this a global variable, to share its access with the whole application package. After that, you can do something like this
prefs.setBool("pref_logged_user",isLoggedIn);
Where pref_logged_user is the key which will be used to access the data
bool isLoggedIn = await prefs.getBool("pref_logged_user")??false;
?? is used to check whether the output is null. If null then return false;
Answered By - Shubham Jain Answer Checked By - David Goodson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.