Line data Source code
1 : import 'dart:convert'; 2 : import 'package:flutter/services.dart'; 3 : import '../flutter_facebook_auth_platform_interface.dart'; 4 : import 'access_token.dart'; 5 : import 'dart:io' show Platform; 6 : import 'package:flutter/foundation.dart' show kIsWeb, visibleForTesting; 7 : import 'login_behavior.dart'; 8 : import 'facebook_auth_exception.dart'; 9 : 10 : /// class to make calls to the facebook login SDK 11 : class MethodCahnnelFacebookAuth extends FacebookAuthPlatform { 12 : @visibleForTesting 13 : MethodChannel channel = const MethodChannel('app.meedu/flutter_facebook_auth'); 14 : 15 : /// make a login request using the facebook SDK 16 : /// 17 : /// [permissions] permissions like ["email","public_profile"] 18 : /// 19 : /// [loginBehavior] (only Android) use this param to set the UI for the authentication, 20 : /// like webview, native app, or a dialog. 21 : @override 22 1 : Future<AccessToken> login({ 23 : List<String> permissions = const ['email', 'public_profile'], 24 : String loginBehavior = LoginBehavior.DIALOG_ONLY, 25 : }) async { 26 : try { 27 4 : final result = await channel.invokeMethod("login", { 28 : "permissions": permissions, 29 : "loginBehavior": loginBehavior, 30 : }); 31 2 : return AccessToken.fromJson(Map<String, dynamic>.from(result)); 32 1 : } on PlatformException catch (e) { 33 3 : throw FacebookAuthException(e.code, e.message); 34 : } 35 : } 36 : 37 : /// Express login logs people in with their Facebook account across devices and platform. 38 : /// If a person logs into your app on Android and then changes devices, 39 : /// express login logs them in with their Facebook account, instead of asking for them to select a login method. 40 : /// 41 : /// This avoid creating duplicate accounts or failing to log in at all. To support the changes in Android 11, 42 : /// first add the following code to the queries element in your /app/manifest/AndroidManifest.xml file. 43 : /// For more info go to https://developers.facebook.com/docs/facebook-login/android 44 : @override 45 1 : Future<AccessToken?> expressLogin() async { 46 2 : final tesing = Platform.environment.containsKey('FLUTTER_TEST'); 47 1 : if (Platform.isAndroid || tesing) { 48 : try { 49 3 : final result = await channel.invokeMethod("expressLogin"); 50 2 : return AccessToken.fromJson(Map<String, dynamic>.from(result)); 51 1 : } on PlatformException catch (e) { 52 3 : throw FacebookAuthException(e.code, e.message); 53 : } 54 : } 55 : return null; 56 : } 57 : 58 : /// retrive the user information using the GraphAPI 59 : /// 60 : /// [fields] string of fields like birthday,email,hometown 61 : @override 62 1 : Future<Map<String, dynamic>> getUserData({ 63 : String fields = "name,email,picture.width(200)", 64 : }) async { 65 : try { 66 4 : final result = await channel.invokeMethod("getUserData", { 67 : "fields": fields, 68 : }); 69 : if (kIsWeb) { 70 0 : return Map<String, dynamic>.from(result); 71 : } else { 72 2 : return Platform.isAndroid ? jsonDecode(result) : Map<String, dynamic>.from(result); //null or dynamic data 73 : } 74 1 : } on PlatformException catch (e) { 75 3 : throw FacebookAuthException(e.code, e.message); 76 : } 77 : } 78 : 79 : /// Sign Out from Facebook 80 : @override 81 1 : Future<void> logOut() async { 82 3 : await channel.invokeMethod("logOut"); 83 : } 84 : 85 : /// if the user is logged return one instance of AccessToken 86 : @override 87 1 : Future<AccessToken?> get accessToken async { 88 3 : final result = await channel.invokeMethod("getAccessToken"); 89 : if (result != null) { 90 2 : return AccessToken.fromJson(Map<String, dynamic>.from(result)); 91 : } 92 : return null; 93 : } 94 : }