forked from LinusU/flutter_web_auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dart:async'; | ||
import 'dart:html'; | ||
import 'dart:js'; | ||
|
||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_web_plugins/flutter_web_plugins.dart'; | ||
|
||
class FlutterWebAuthPlugin { | ||
static void registerWith(Registrar registrar) { | ||
final MethodChannel channel = MethodChannel( | ||
'flutter_web_auth', const StandardMethodCodec(), registrar.messenger); | ||
final FlutterWebAuthPlugin instance = FlutterWebAuthPlugin(); | ||
channel.setMethodCallHandler(instance.handleMethodCall); | ||
} | ||
|
||
Future<dynamic> handleMethodCall(MethodCall call) async { | ||
switch (call.method) { | ||
case 'authenticate': | ||
final String url = call.arguments['url']; | ||
return _authenticate(url); | ||
default: | ||
throw PlatformException( | ||
code: 'Unimplemented', | ||
details: "The flutter_web_auth plugin for web doesn't implement " | ||
"the method '${call.method}'"); | ||
} | ||
} | ||
|
||
static Future<String> _authenticate(String url) async { | ||
context.callMethod('open', [url]); | ||
await for (MessageEvent messageEvent in window.onMessage) { | ||
final message = messageEvent.data['flutter-web-auth']; | ||
if (messageEvent.origin == Uri.base.origin && message is String) { | ||
return message; | ||
} | ||
} | ||
throw new PlatformException( | ||
code: 'error', message: 'Iterable window.onMessage is empty'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<title>Authentication complete</title> | ||
<p>Authentication is complete. If this does not happen automatically, please | ||
close the window. | ||
<script> | ||
window.opener.postMessage({ | ||
'flutter-web-auth': window.location.href | ||
}, window.location.origin); | ||
window.close(); | ||
</script> |