Skip to content

Commit

Permalink
Migrate to package:web
Browse files Browse the repository at this point in the history
  • Loading branch information
cuong0993 committed Aug 23, 2024
1 parent 1135e83 commit 7ba66ee
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 54 deletions.
69 changes: 31 additions & 38 deletions facebook_auth_web/lib/src/facebook_auth_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dart:async';
import 'dart:html';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';

import 'package:flutter/services.dart';
import 'package:flutter_facebook_auth_platform_interface/flutter_facebook_auth_platform_interface.dart';
import 'package:js/js.dart';
import 'dart:js' as js;
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'interop/facebook_auth_interop.dart' as fb;
import 'package:web/web.dart';

import 'interop/convert_interop.dart';
import 'interop/facebook_auth_interop.dart' as fb;

/// A web implementation of the FlutterFacebookAuth plugin.
class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
Expand All @@ -27,15 +29,11 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
if (!_initialized) return null;

Completer<LoginResult> completer = Completer();
fb.getLoginStatus(
allowInterop(
(jsResponse) {
this._handleResponse(jsResponse).then(
(result) => completer.complete(result),
);
},
),
);
fb.getLoginStatus((JSAny jsResponse) {
this._handleResponse(jsResponse).then(
(result) => completer.complete(result),
);
}.toJS);
final LoginResult result = await completer.future;
return result.accessToken;
}
Expand Down Expand Up @@ -73,15 +71,14 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
if (!_initialized) return {"error": "window.FB is undefined"};
Completer<Map<String, dynamic>> c = Completer();
fb.api(
"/me?fields=$fields",
allowInterop(
(_) => c.complete(
Map<String, dynamic>.from(
convert(_),
),
),
),
);
"/me?fields=$fields",
(JSAny _) {
c.complete(
Map<String, dynamic>.from(
convert(_),
),
);
}.toJS);
return c.future;
}

Expand All @@ -90,13 +87,11 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
Future<void> logOut() async {
if (!_initialized) return;
Completer<void> c = Completer();
fb.logout(allowInterop(
(_) {
if (!c.isCompleted) {
c.complete();
}
},
));
fb.logout((JSAny _) {
if (!c.isCompleted) {
c.complete();
}
}.toJS);
return c.future;
}

Expand All @@ -117,13 +112,11 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
String scope = permissions.join(",");
Completer<LoginResult> completer = Completer();
fb.login(
allowInterop(
(jsResponse) {
this._handleResponse(jsResponse).then(
(result) => completer.complete(result),
);
},
),
(JSAny jsResponse) {
this._handleResponse(jsResponse).then(
(result) => completer.complete(result),
);
}.toJS,
fb.LoginOptions(
scope: scope,
return_scopes: true,
Expand All @@ -145,7 +138,7 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
}) async {
this._appId = appId;

if (js.context['FB'] != null) {
if (globalContext.has("FB")) {
_initialized = true;
return;
}
Expand All @@ -166,7 +159,7 @@ class FlutterFacebookAuthPlugin extends FacebookAuthPlatform {
/// Injects a `script` with a `src` dynamically into the head of the current
/// document.
Future<void> _injectSrcScript() async {
final script = ScriptElement()
final script = HTMLScriptElement()
..type = 'text/javascript'
..src = 'https://connect.facebook.net/en_US/sdk.js'
..async = true
Expand Down
4 changes: 2 additions & 2 deletions facebook_auth_web/lib/src/interop/convert_interop.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
@JS()
library stringify;

import 'package:js/js.dart';
import 'dart:convert';
import 'dart:js_interop';

// Calls invoke JavaScript `JSON.stringify(obj)`.
@JS('JSON.stringify')
external String stringify(Object obj);
external String stringify(JSObject obj);

/// convert the a javascript object to a valid map
Map<String, dynamic> convert(dynamic object) {
Expand Down
18 changes: 9 additions & 9 deletions facebook_auth_web/lib/src/interop/facebook_auth_interop.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
@JS('FB')
library facebook_auth.js;

import 'package:js/js.dart';
import 'dart:js_interop';

typedef FbCallback = void Function(dynamic response);
typedef FbCallback = JSExportedDartFunction;

@JS('init')
external init(InitOptions options);
external void init(InitOptions options);

@JS('login')
external login(FbCallback fn, LoginOptions options);
external void login(FbCallback fn, LoginOptions options);

@JS('getLoginStatus')
external getLoginStatus(FbCallback fn);
external void getLoginStatus(FbCallback fn);

@JS('api')
external api(String request, FbCallback fn);
external void api(String request, FbCallback fn);

@JS('logout')
external logout(FbCallback fn);
external void logout(FbCallback fn);

@JS()
@anonymous
class InitOptions {
extension type InitOptions._(JSObject _) implements JSObject {
external factory InitOptions({
required String appId,
required String version,
Expand All @@ -37,7 +37,7 @@ class InitOptions {

@JS()
@anonymous
class LoginOptions {
extension type LoginOptions._(JSObject _) implements JSObject {
external factory LoginOptions({
required String scope,
// ignore: non_constant_identifier_names
Expand Down
7 changes: 3 additions & 4 deletions facebook_auth_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ homepage: https://github.com/darwin-morocho/flutter-facebook-auth/tree/master/fa


environment:
sdk: '>=3.0.0 <4.0.0'
flutter: ">=2.0.0"
sdk: '>=3.3.0 <4.0.0'
flutter: ">=3.19.2"

dependencies:
flutter:
sdk: flutter
flutter_web_plugins:
sdk: flutter
js: ^0.6.3
flutter_facebook_auth_platform_interface: ^6.0.0

web: ^0.5.1
# flutter_facebook_auth_platform_interface:
# path: ../facebook_auth_platform_interface

Expand Down
2 changes: 1 addition & 1 deletion facebook_auth_web/test/mock/mock_interop.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@JS()
library mock_facebook_auth;

import 'package:js/js.dart';
import 'dart:js_interop';

@JS()
@anonymous
Expand Down

0 comments on commit 7ba66ee

Please sign in to comment.