Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for WASM compilation #516

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion auth0_flutter/example/lib/example_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class _ExampleAppState extends State<ExampleApp> {
Auth0Web(dotenv.env['AUTH0_DOMAIN']!, dotenv.env['AUTH0_CLIENT_ID']!);
webAuth =
auth0.webAuthentication(scheme: dotenv.env['AUTH0_CUSTOM_SCHEME']);

if (kIsWeb) {
auth0Web.onLoad().then((final credentials) => setState(() {
_output = credentials?.idToken ?? '';
Expand Down
4 changes: 2 additions & 2 deletions auth0_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ description: Demonstrates how to use the auth0_flutter plugin.
publish_to: "none" # Remove this line if you wish to publish to pub.dev

environment:
sdk: ">=2.17.0 <3.0.0"
flutter: ">=3.0.0"
sdk: ">=3.5.0 <4.0.0"
flutter: ">=3.24.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand Down
2 changes: 1 addition & 1 deletion auth0_flutter/example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
var serviceWorkerVersion = null;
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
<script src="flutter_bootstrap.js" async></script>
<script src="https://cdn.auth0.com/js/auth0-spa-js/2.0/auth0-spa-js.production.js" defer></script>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion auth0_flutter/lib/src/web.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'web/auth0_flutter_plugin_stub.dart'
if (dart.library.html) 'web/auth0_flutter_plugin_real.dart';
if (dart.library.js_interop) 'web/auth0_flutter_plugin_real.dart';
17 changes: 6 additions & 11 deletions auth0_flutter/lib/src/web/auth0_flutter_plugin_real.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:async';
import 'dart:html';
import 'dart:js_interop';

import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:web/web.dart';

import 'auth0_flutter_web_platform_proxy.dart';
import 'extensions/client_options_extensions.dart';
Expand Down Expand Up @@ -38,7 +39,7 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
try {
return await clientProxy!.handleRedirectCallback();
} catch (e) {
throw WebExceptionExtension.fromJsObject(e);
throw WebExceptionExtension.fromJsObject(JSObject.fromInteropObject(e));
}
}

Expand All @@ -48,7 +49,6 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
@override
Future<void> loginWithRedirect(final LoginOptions? options) {
final client = _ensureClient();

final authParams = JsInteropUtils.stripNulls(JsInteropUtils.addCustomParams(
interop.AuthorizationParams(
audience: options?.audience,
Expand All @@ -63,7 +63,6 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {

final loginOptions =
interop.RedirectLoginOptions(authorizationParams: authParams);

return client.loginWithRedirect(loginOptions);
}

Expand All @@ -83,7 +82,7 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
options?.parameters ?? {}));

final popupConfig = JsInteropUtils.stripNulls(interop.PopupConfigOptions(
popup: options?.popupWindow,
popup: options?.popupWindow as JSAny?,
timeoutInSeconds: options?.timeoutInSeconds));

try {
Expand All @@ -98,7 +97,7 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
scope: authParams.scope, audience: authParams.audience)),
detailedResponse: true)));
} catch (e) {
throw WebExceptionExtension.fromJsObject(e);
throw WebExceptionExtension.fromJsObject(JSObject.fromInteropObject(e));
}
}

Expand All @@ -115,17 +114,14 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
final clientProxy = _ensureClient();
final tokenOptions = options?.toGetTokenSilentlyOptions() ??
interop.GetTokenSilentlyOptions();

// Force this, as we always want the full detail back so that we can
// return a full Credentials instance.
tokenOptions.detailedResponse = true;

try {
final result = await clientProxy.getTokenSilently(tokenOptions);

return CredentialsExtension.fromWeb(result);
} catch (e) {
throw WebExceptionExtension.fromJsObject(e);
throw WebExceptionExtension.fromJsObject(JSObject.fromInteropObject(e));
}
}

Expand All @@ -136,7 +132,6 @@ class Auth0FlutterPlugin extends Auth0FlutterWebPlatform {
if (clientProxy == null) {
throw ArgumentError('Auth0Client has not been initialized');
}

return clientProxy!;
}
}
20 changes: 12 additions & 8 deletions auth0_flutter/lib/src/web/auth0_flutter_web_platform_proxy.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:js_util';
import 'dart:js_interop';
import 'js_interop.dart';

class Auth0FlutterWebClientProxy {
Expand All @@ -7,24 +7,28 @@ class Auth0FlutterWebClientProxy {
Auth0FlutterWebClientProxy({required this.client});

Future<void> loginWithRedirect(final RedirectLoginOptions options) =>
promiseToFuture(client.loginWithRedirect(options));
JSPromiseToFuture(client.loginWithRedirect(options)).toDart;

Future<void> loginWithPopup(
[final PopupLoginOptions? options,
final PopupConfigOptions? config]) =>
promiseToFuture(client.loginWithPopup(options, config));
JSPromiseToFuture(client.loginWithPopup(options, config)).toDart;

Future<void> checkSession() => promiseToFuture(client.checkSession());
Future<void> checkSession() =>
JSPromiseToFuture(client.checkSession()).toDart;

Future<WebCredentials> getTokenSilently(
[final GetTokenSilentlyOptions? options]) =>
promiseToFuture(client.getTokenSilently(options));
JSPromiseToFuture(client.getTokenSilently(options)).toDart;

Future<void> handleRedirectCallback() =>
promiseToFuture(client.handleRedirectCallback());
JSPromiseToFuture(client.handleRedirectCallback()).toDart;

Future<bool> isAuthenticated() => promiseToFuture(client.isAuthenticated());
Future<bool> isAuthenticated() async {
final jsBool = await JSPromiseToFuture(client.isAuthenticated()).toDart;
return jsBool.toDart;
}

Future<void> logout(final LogoutOptions? options) =>
promiseToFuture(client.logout(options));
JSPromiseToFuture(client.logout(options)).toDart;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:js_interop';

import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart';

import '../js_interop.dart';
Expand All @@ -7,11 +9,10 @@ import 'user_profile_extension.dart';

extension CredentialsExtension on Credentials {
static Credentials fromWeb(final WebCredentials webCredentials) {
final expiresIn = webCredentials.expires_in as int;
final expiresIn = webCredentials.expires_in.toDartInt;
final expiresAt = DateTime.now().add(Duration(seconds: expiresIn));
final claims = JWT.decode(webCredentials.id_token);
final user = UserProfileExtension.fromClaims(claims);

return Credentials(
idToken: webCredentials.id_token,
accessToken: webCredentials.access_token,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:js_interop';

import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart';

import '../js_interop.dart';
Expand All @@ -12,6 +14,6 @@ extension CredentialsOptionsExtension on CredentialsOptions {
scope: scopes?.join(' '), audience: audience),
parameters)),
cacheMode: cacheMode.toString(),
timeoutInSeconds: timeoutInSeconds,
timeoutInSeconds: timeoutInSeconds?.toJS,
detailedResponse: detailedResponse);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import 'dart:js_util';
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:auth0_flutter_platform_interface/auth0_flutter_platform_interface.dart';

@JS('Object.keys')
external JSArray<JSString> keys(final JSObject o);

extension WebExceptionExtension on WebException {
static WebException fromJsObject(final Object jsException) {
final error = getProperty<String>(jsException, 'error');
final description = getProperty<String>(jsException, 'error_description');
final Map<String, dynamic> details = {};
static WebException fromJsObject(final JSObject jsException) {
final error = jsException.getProperty<JSString>('error'.toJS);
final description =
jsException.getProperty<JSString>('error_description'.toJS);
final Map<String, JSAny?> details = {};

objectKeys(jsException).forEach((final key) {
if (key == 'error' || key == 'error_description') return;
details[key as String] = getProperty<dynamic>(jsException, key);
keys(jsException).toDart.forEach((final JSString key) {
if (key.toDart == 'error' || key.toDart == 'error_description') return;
details[key.toDart] = jsException.getProperty<JSAny?>(key);
});

switch (error) {
Expand All @@ -27,18 +32,18 @@ extension WebExceptionExtension on WebException {
case 'unsupported_grant_type':
case 'temporarily_unavailable':
return WebException.authenticationError(
error, description, {'state': details['state']});
error.toDart, description.toDart, {'state': details['state']});
case 'mfa_required':
return WebException.mfaError(
description, getProperty(jsException, 'mfaToken'));
return WebException.mfaError(description.toDart,
jsException.getProperty('mfaToken'.toJS) as String);
case 'timeout':
return WebException.timeout(description);
return WebException.timeout(description.toDart);
case 'cancelled':
return WebException.popupClosed(description);
return WebException.popupClosed(description.toDart);
case 'missing_refresh_token':
return WebException.missingRefreshToken(description);
return WebException.missingRefreshToken(description.toDart);
}

return WebException(error, description, details);
return WebException(error.toDart, description.toDart, details);
}
}
Loading
Loading