flutter_facebook_auth 0.1.1

flutter_facebook_auth #

Flutter plugin to make easy the facebook authentication in your flutter app. iOS and Android is supported.

install on Android #

Go to https://developers.facebook.com/docs/facebook-login/android/?locale=en and read the next documentation.

  • Edit Your Resources and Manifest
  • Associate Your Package Name and Default Class with Your App
  • Provide the Development and Release Key Hashes for Your App

install on iOS #

Configure Info.plist

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>fb{your-app-id}</string>
    </array>
  </dict>
</array>
<key>FacebookAppID</key>
<string>{your-app-id}</string>
<key>FacebookDisplayName</key>
<string>{your-app-name}</string>
<key>LSApplicationQueriesSchemes</key>
<array>
  <string>fbapi</string>
  <string>fb-messenger-share-api</string>
  <string>fbauth2</string>
  <string>fbshareextension</string>
</array>

METHODS #

fisrt create a new instance of FacebookAuth. NOTE: all methods are asynchronous.

  • .login({List<String> permissions = const ['email', 'public_profile'] }) : request login with a list of permissions.

    The public_profile permission allows you read the next fileds id, first_name, last_name, middle_name, name, name_format, picture, short_name

    For more info go to https://developers.facebook.com/docs/facebook-login/permissions/

    return one instance of LoginResult class:

      { 
        status: 200,
        accessToken: { 
          expires: 1573493493209, 
          declinedPermissions: [], 
          permissions: [public_profile, email], 
          userId: 3003332493073668, 
          token: EAAVDwBAE5lndhpg17DHFZABzh6QKiAZC42Qljcub9gib52L5CPEXvhk2ZBEa7LlOuyastmmkZBfwP7dKW6Xi4tvrTw8DToO2M2kMcau6CXsYtyys7WZAWV3XaMPnhuVauo5ghtGpnhJvZAtMKqlsgbV5GklPAYZD
        }
      }
    

    if status is 200 the login was successfull.

  • .logOut() : close the current facebook session.

  • .isLogged() : check if the user has an active facebook session. The response will be null if the user is not logged. return one instance of AccessToken class:

      { 
          expires: 1573493493209, 
          declinedPermissions: [], 
          permissions: [public_profile, email], 
          userId: 3003332493073668, 
          token: EAATaHWA7VDwBAE5lndhpg17DHFZABzh6QKiAZC42Qljcub9gib52L5CPEXvhk2ZBEa7LlOuytmmkZBfwP7dKW6Xi4XCO2M2kMcau6CXsYtyys7WZAWV3XaMPnhuVauo5ghtGpnhJvZAtMKqlsgbV5GklPAYZD
        }
    

    NOTE: declinedPermissions and permissions are null on iOS, please use the method permissionsStatus(String token)

  • .getUserData({String fields = "name,email,picture"}) : get the user info only if the user is logged.

    Expected response:

      {
      email = "dsmr.apps@gmail.com";
      id = 3003332493073668;
      name = "Darwin Morocho";
      picture =     {
          data =         {
              height = 50;
              "is_silhouette" = 0;
              url = "https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=3003332493073668&height=50&width=50&ext=1570917120&hash=AeQMSBD5s4QdgLoh";
              width = 50;
          };
      };
      }
    

EXAMPLE #

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _fb = FacebookAuth();
  dynamic _userData;
  String _token;

  @override
  void initState() {
    super.initState();
    _checkIfIsLogged();
  }

  _printCredentials(AccessToken accessToken) {
    _token = accessToken.token;
    print("userId: ${accessToken.userId}");
    print("token: $_token");
    print("expires: ${accessToken.expires}");
    print("permissions: ${accessToken.permissions.toString()}");
  }

  _checkIfIsLogged() async {
    final accessToken = await _fb.isLogged();
    if (accessToken != null) {
      _printCredentials(accessToken);
      // now you can call to  _fb.getUserData();
      final userData = await _fb.getUserData();
      // final userData = await _fb.getUserData(fields:"email,birthday");
      setState(() {
        _userData = userData;
      });
    }
  }

  _login() async {
    final result = await _fb.login();
    // final result = await _fb.login(permissions:['email','user_birthday']);
    print("login result ${result.toString()}");
    if (result.status == 200) {
      _printCredentials(result.accessToken);
      // get the user data
      final userData = await _fb.getUserData();
      // final userData = await _fb.getUserData(fields:"email,birthday");
      setState(() {
        _userData = userData;
      });
    } else if (result.status == 403) {
      print("login cancelled");
    } else {
      print("login failed");
    }
  }

  _logOut() async {
    await _fb.logOut();
    _token = null;
    setState(() {
      _userData = null;
    });
  }

  _checkPermissions() async {
    final dynamic response = await _fb.permissionsStatus(_token);
    print("permissions: ${response.toString()}");
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Facebook Auth Example'),
        ),
        body: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text(_userData != null ? _userData.toString() : "NO LOGGED"),
            _userData != null
                ? CupertinoButton(
                    child: Text("Check permissions"),
                    onPressed: _checkPermissions,
                    color: Colors.greenAccent,
                  )
                : Container(),
            SizedBox(height: 20),
            CupertinoButton(
                color: Colors.blue,
                child: Text(
                  _userData != null ? "LOGOUT" : "LOGIN",
                  style: TextStyle(color: Colors.white),
                ),
                onPressed: _userData != null ? _logOut : _login),
          ],
        ),
      ),
    );
  }
}

Using with firebase_auth #

Just create a credential like

 // this line do auth in firebase with your facebook credential.
 AuthCredential credential =  FacebookAuthProvider.getCredential(accessToken: _token);
            

final FirebaseUser user = (await _auth.signInWithCredential(credential)).user;

0.0.1 #

  • iOS version

example/README.md

flutter_facebook_auth_example #

Demonstrates how to use the flutter_facebook_auth plugin.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_facebook_auth: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
13
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Sep 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/src/access_token.dart.

Run flutter format to format lib/src/access_token.dart.

Format lib/src/login_result.dart.

Run flutter format to format lib/src/login_result.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
http ^0.12.0+2 0.12.0+2
Transitive dependencies
async 2.3.0
charcode 1.1.2
collection 1.14.11 1.14.12
http_parser 3.1.3
meta 1.1.7
path 1.6.4
pedantic 1.8.0+1
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test