Skip to content

Commit

Permalink
add support 1.20 js sdk version
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergo007 committed Mar 30, 2023
1 parent 7fbf3a0 commit ae31c31
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 135 deletions.
31 changes: 31 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "example",
"cwd": "example",
"request": "launch",
"type": "dart",
"args": ["-d", "chrome", "--web-port", "8000"]
},
{
"name": "example (profile mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"args": ["--web-port", "8000"],
"flutterMode": "profile"
},
{
"name": "example (release mode)",
"cwd": "example",
"request": "launch",
"type": "dart",
"args": ["--web-port", "8000"],
"flutterMode": "release"
}
]
}
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,37 @@ A wrapper for Auth0 SPA SDK to use Auth0 in Flutter Web.

### 1. Add dependency

```yaml
dependencies:
auth0_flutter_web:
```
```yaml
dependencies:
auth0_flutter_web:
```
### 2. Load Auth0 SPA SDK in `index.html`

Make Auth0 SPA SDK available for dart by adding it to `index.html` in `web` folder (besides `ios` and `android`)

```HTML
<head>
<!-- other elements in head -->
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.13/auth0-spa-js.production.js"></script>
<!-- other elements in head -->
</head>
```
Make Auth0 SPA SDK available for dart by adding it to `index.html` in `web` folder (besides `ios` and `android`)

```HTML
<head>
<!-- other elements in head -->
<script src="https://cdn.auth0.com/js/auth0-spa-js/1.20/auth0-spa-js.production.js"></script>
<!-- other elements in head -->
</head>
```

### 3. Use `Auth0` in dart code

```dart
import 'package:auth0_flutter_web/auth0_flutter_web.dart';
```dart
import 'package:auth0_flutter_web/auth0_flutter_web.dart';
Auth0 auth0 = await createAuth0Client(
Auth0CreateOptions(
domain: '-- domain of the universal login page --',
client_id: '-- id of your app --',
)
);
Auth0 auth0 = await createAuth0Client(
Auth0CreateOptions(
domain: '-- domain of the universal login page --',
client_id: '-- id of your app --',
)
);
String token = await auth0.getTokenWithPopup();
```
String token = await auth0.getTokenWithPopup();
```

## Example

Expand Down
38 changes: 25 additions & 13 deletions auth0_flutter_web/lib/src/auth0_js_interop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ import 'options.dart';

@JS()
@anonymous
class _Promise<T>{} // just to denote a js promise object
class _Promise<T> {} // just to denote a js promise object

@JS('createAuth0Client')
external _Promise<_Auth0JS> Function(Auth0ClientOptions) get _createAuth0Client;

Future<Auth0> createAuth0Client(Auth0ClientOptions options) {
return promiseToFuture(_createAuth0Client(options))
.then((auth0js) => Auth0._(auth0js));
return promiseToFuture(_createAuth0Client(options)).then((auth0js) => Auth0._(auth0js));
}

@JS()
@anonymous // since it isn't exactly the auth0 object
abstract class _Auth0JS{
@JS() external _Promise<void> loginWithPopup(PopupLoginOptions options, PopupConfigOptions config);
@JS() external void logout(LogoutOptions options);
@JS() external _Promise<String> getTokenWithPopup(GetTokenWithPopupOptions options, PopupConfigOptions config);
@JS() external _Promise<String> getTokenSilently(GetTokenSilentlyOptions options);
@JS() external _Promise<Map<String, dynamic>> getUser(GetUserOptions options);
@JS() external _Promise<Map<String, dynamic>> getIdTokenClaims(GetIdTokenClaimsOptions options);
@JS() external _Promise<bool> isAuthenticated();
abstract class _Auth0JS {
@JS()
external _Promise<void> loginWithPopup(PopupLoginOptions options, PopupConfigOptions config);
@JS()
external _Promise<String?> loginWithRedirect(RedirectLoginOptions options);
@JS()
external void logout(LogoutOptions options);
@JS()
external _Promise<String> getTokenWithPopup(GetTokenWithPopupOptions options, PopupConfigOptions config);
@JS()
external _Promise<String> getTokenSilently(GetTokenSilentlyOptions options);
@JS()
external _Promise<Map<String, dynamic>> getUser(GetUserOptions options);
@JS()
external _Promise<Map<String, dynamic>> getIdTokenClaims(GetIdTokenClaimsOptions options);
@JS()
external _Promise<bool> isAuthenticated();
}

class Auth0 {
Expand All @@ -41,16 +49,20 @@ class Auth0 {
return promiseToFuture(_auth0js.loginWithPopup(options ?? jsify({}), config ?? jsify({})));
}

Future<String?> loginWithRedirect({RedirectLoginOptions? options = null}) {
return promiseToFuture(_auth0js.loginWithRedirect(options ?? jsify({})));
}

void logout({LogoutOptions? options = null}) => _auth0js.logout(options ?? jsify({}));

Future<String> getTokenWithPopup({GetTokenWithPopupOptions? options = null,PopupConfigOptions? config = null}) {
Future<String> getTokenWithPopup({GetTokenWithPopupOptions? options = null, PopupConfigOptions? config = null}) {
return promiseToFuture(_auth0js.getTokenWithPopup(options ?? jsify({}), config ?? jsify({})));
}

Future<String> getTokenSilently({GetTokenSilentlyOptions? options = null}) {
return promiseToFuture(_auth0js.getTokenSilently(options ?? jsify({})));
}

Future<Map<String, dynamic>?> getUser({GetUserOptions? options = null}) => promiseToFutureAsMap(_auth0js.getUser(options ?? jsify({})));
Future<Map<String, dynamic>?> getIdTokenClaims({GetIdTokenClaimsOptions? options = null}) => promiseToFutureAsMap(_auth0js.getIdTokenClaims(options ?? jsify({})));

Expand Down
58 changes: 46 additions & 12 deletions auth0_flutter_web/lib/src/options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class BaseLoginOptions {
external String get scope;
external String get audience;
external String? get connection;
external String? get organization;
external String? get invitation;
external String? get screen_hint;

external factory BaseLoginOptions({
String display,
Expand All @@ -28,7 +31,10 @@ class BaseLoginOptions {
String acr_values,
String scope,
String audience,
String? connection
String? connection,
String? organization,
String? invitation,
String? screen_hint,
});
}

Expand All @@ -37,9 +43,7 @@ class BaseLoginOptions {
class AdvancedOptions {
external String? get defaultScope;

external factory AdvancedOptions({
String? defaultScope
});
external factory AdvancedOptions({String? defaultScope});
}

@JS()
Expand All @@ -57,7 +61,9 @@ class Auth0ClientOptions extends BaseLoginOptions {
external bool get useCookiesForTransactions;
external AdvancedOptions get advancedOptions;
external num get sessionCheckExpiryDays;

external num get httpTimeoutInSeconds;
external bool get useFormData;

external factory Auth0ClientOptions({
String display,
String prompt,
Expand All @@ -80,13 +86,18 @@ class Auth0ClientOptions extends BaseLoginOptions {
bool legacySameSiteCookie,
bool useCookiesForTransactions,
AdvancedOptions advancedOptions,
num sessionCheckExpiryDays
num sessionCheckExpiryDays,
num httpTimeoutInSeconds,
bool useFormData,
String? organization,
String? invitation,
String? screen_hint,
});
}

@JS()
@anonymous
class PopupLoginOptions extends BaseLoginOptions{
class PopupLoginOptions extends BaseLoginOptions {
external factory PopupLoginOptions({
String display,
String prompt,
Expand All @@ -97,14 +108,17 @@ class PopupLoginOptions extends BaseLoginOptions{
String acr_values,
String scope,
String audience,
String? connection
String? connection,
String? organization,
String? invitation,
String? screen_hint,
});
}

@JS()
@anonymous
class GetTokenWithPopupOptions extends PopupLoginOptions {
external factory GetTokenWithPopupOptions({
class RedirectLoginOptions extends BaseLoginOptions {
external factory RedirectLoginOptions({
String display,
String prompt,
Object max_age,
Expand All @@ -114,10 +128,30 @@ class GetTokenWithPopupOptions extends PopupLoginOptions {
String acr_values,
String scope,
String audience,
String? connection
String redirect_url,
String? connection,
String? organization,
String? invitation,
String? screen_hint,
});
}

@JS()
@anonymous
class GetTokenWithPopupOptions extends PopupLoginOptions {
external factory GetTokenWithPopupOptions(
{String display,
String prompt,
Object max_age,
String ui_locales,
String id_token_hint,
String login_hint,
String acr_values,
String scope,
String audience,
String? connection});
}

@JS()
@anonymous
class GetTokenSilentlyOptions {
Expand Down Expand Up @@ -154,7 +188,7 @@ class LogoutOptions {

@JS()
@anonymous
class GetUserOptions{
class GetUserOptions {
external String get scope;
external String get audience;

Expand Down
13 changes: 11 additions & 2 deletions auth0_flutter_web/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ packages:
dependency: "direct main"
description:
name: js
url: "https://pub.dartlang.org"
sha256: d9bdfd70d828eeb352390f81b18d6a354ef2044aa28ef25682079797fa7cd174
url: "https://pub.dev"
source: hosted
version: "0.6.3"
meta:
dependency: "direct main"
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
sdks:
dart: ">=2.12.0-0 <3.0.0"
dart: ">=2.12.0 <3.0.0"
1 change: 0 additions & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
/build/

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols
Expand Down
Loading

0 comments on commit ae31c31

Please sign in to comment.