PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 16, 2022

[FIXED] How can i save facebook user login session in flutter app?

 July 16, 2022     dart, facebook-login, flutter     No comments   

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

Shared Preference 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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing