login method
make a login request using the facebook SDK
permissions
permissions like "email","public_profile"
loginBehavior
(only Android) use this param to set the UI for the authentication,
like webview, native app, or a dialog.
Implementation
@override
Future<LoginResult> login({
List<String> permissions = const ['email', 'public_profile'],
LoginBehavior loginBehavior = LoginBehavior.dialogOnly,
LoginTracking loginTracking = LoginTracking.enabled,
String? nonce,
}) async {
assert(
_appId.isNotEmpty,
'On desktop before call login() you must call to desktopInitialize(...)',
);
final signInURL = Uri.parse(
'https://www.facebook.com/$_version/dialog/oauth',
);
const redirectURL = 'https://www.facebook.com/connect/login_success.html';
final signInUri = Uri(
scheme: signInURL.scheme,
host: signInURL.host,
path: signInURL.path,
queryParameters: {
'client_id': _appId,
'redirect_uri': redirectURL,
'response_type': 'token,granted_scopes',
'scope': permissions.join(','),
'state': _generateNonce(),
},
);
final callbackUrl = await PlatformChannel().signIn(
signInUri.toString(),
redirectURL,
);
if (callbackUrl != null) {
final fragment = Uri.parse(callbackUrl).fragment;
final arguments = Uri.splitQueryString(fragment);
String? token = arguments['long_lived_token'];
bool isLoginLiveToken = token != null;
late final DateTime expiresIn;
if (!isLoginLiveToken) {
token = arguments['access_token']!;
expiresIn = DateTime.now().add(
Duration(
seconds: int.parse(arguments['expires_in']!),
),
);
} else {
expiresIn = DateTime.now().add(
const Duration(days: 59),
);
}
final grantedScopes = arguments['granted_scopes']!.split(',');
final deniedScopes = arguments['denied_scopes']!.split(',');
final response = await _httpClient.get(
Uri.parse('https://graph.facebook.com/me?access_token=$token'),
);
if (response.statusCode == 200) {
final userData = jsonDecode(response.body);
final accessToken = ClassicToken(
declinedPermissions: deniedScopes,
grantedPermissions: grantedScopes,
userId: userData['id'],
expires: expiresIn,
tokenString: token,
applicationId: _appId,
);
await _secureStorage.delete(key: _facebookAccessTokenKey);
await _secureStorage.write(
key: _facebookAccessTokenKey,
value: jsonEncode(
accessToken.toJson(),
),
);
return LoginResult(
status: LoginStatus.success,
accessToken: accessToken,
);
}
return LoginResult(
status: LoginStatus.failed,
message: 'User info could not be get it',
);
}
return LoginResult(status: LoginStatus.cancelled);
}