diff --git a/lib/app.dart b/lib/app.dart index 8e654318284add1d9d097f34014be7aa331f6cb8..7b3b5318f636a3eb7d5349e6131743d41e06105f 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -7,7 +7,6 @@ import 'dart:async'; import 'dart:developer'; import 'package:uni_links/uni_links.dart'; -import 'package:flutter_web_browser/flutter_web_browser.dart'; class App extends StatefulWidget { @override @@ -19,6 +18,9 @@ class _AppState extends State<App> { String _appMode = "app"; + // Url of the app which invoked OAuth + String _invokerURL; + @override initState() { super.initState(); @@ -50,11 +52,12 @@ class _AppState extends State<App> { }); } - updateAppMode(uri) { - log("Uri ${uri?.toString()}"); + updateAppMode(Uri uri) { + log("Uri ${uri}"); if (uri?.path == "/oauth2") { setState(() { _appMode = "oauth"; + _invokerURL = uri.queryParameters["callbackUrl"]; }); } else { setState(() { @@ -76,7 +79,8 @@ class _AppState extends State<App> { home: new Scaffold( appBar: new AppBar(), body: Home( - mode: _appMode + mode: _appMode, + invokerURL: _invokerURL ) ), ); diff --git a/lib/screens/home.dart b/lib/screens/home.dart index c1d8ffd4b9b3900028dcd40939ccdc1b5b33d8d2..fa55a3a3d87b6116a377cf1b79e525ed81642e74 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -1,11 +1,14 @@ import 'package:flutter/material.dart'; import 'package:flutter_web_browser/flutter_web_browser.dart'; import 'package:flutter_app_auth_wrapper/flutter_app_auth_wrapper.dart'; +import 'package:url_launcher/url_launcher.dart'; import 'dart:developer'; +import 'dart:convert'; class Home extends StatefulWidget { - Home({@required this.mode}); + Home({@required this.mode, @required this.invokerURL}); final String mode; + final String invokerURL; @override _HomeState createState() => _HomeState(); @@ -25,6 +28,38 @@ class _HomeState extends State<Home> { initState() { super.initState(); showMode(widget.mode); + + FlutterAppAuthWrapper.eventStream().listen((data) { + var token = json.decode(data.toString())["access_token"]; + _showAlert(token); + }); + } + + Future<void> _showAlert(token) { + return showDialog<void>( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text('Authorization success'), + actions: <Widget>[ + FlatButton( + child: Text('Go back'), + onPressed: () async { + Navigator.of(context).pop(); + + log('Try run ${widget.invokerURL}'); + if (await canLaunch(widget.invokerURL)) { + log('Run ${widget.invokerURL}'); + await launch(widget.invokerURL); + } else { + log('Could not launch ${widget.invokerURL}'); + } + }, + ), + ], + ); + }, + ); } @override @@ -41,27 +76,36 @@ class _HomeState extends State<Home> { showMode(mode) { log(mode); if (mode == "app") { - FlutterWebBrowser.openWebPage(url: 'https://app.vereign.com', androidToolbarColor: Colors.deepPurple); + openVereign(); } else if (mode == "oauth") { - FlutterAppAuthWrapper.startAuth( - AuthConfig( - clientId: clientId, - clientSecret: clientSecret, - redirectUrl: redirectURL, - state: "login", - prompt: "consent", - endpoint: AuthEndpoint( - auth: authEndpoint, token: tokenEndpoint), - scopes: [ - "user_account_status", - "user_territory", - "user_profile" - ], - ), - ); + startOAuth(); } } + openVereign() { + FlutterWebBrowser.openWebPage(url: 'https://app.vereign.com', androidToolbarColor: Colors.deepPurple); + } + + startOAuth() { + FlutterAppAuthWrapper.startAuth( + AuthConfig( + clientId: clientId, + clientSecret: clientSecret, + redirectUrl: redirectURL, + state: "login", + prompt: "consent", + endpoint: AuthEndpoint( + auth: authEndpoint, token: tokenEndpoint), + scopes: [ + "user_account_status", + "user_territory", + "user_profile" + ], + ), + ); + } + + @override Widget build(BuildContext context) { return Scaffold( @@ -70,20 +114,21 @@ class _HomeState extends State<Home> { child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.stretch, - children: _links.map((link) => _urlButton(context, link)).toList(), +// children: _links.map((link) => _urlButton(context, link)).toList(), + children: <Widget>[ + _urlButton(context, _links[0], "Open Vereign", openVereign), + _urlButton(context, _links[1], "Authorize with Vereign", startOAuth), + ] )))); } - Widget _urlButton(BuildContext context, String url) { + Widget _urlButton(BuildContext context, String url, String title, listener) { return Container( padding: EdgeInsets.all(20.0), child: FlatButton( color: Theme.of(context).primaryColor, padding: const EdgeInsets.symmetric(horizontal: 50.0, vertical: 15.0), - child: Text(url), - onPressed: () => _handleURLButtonPress(context, url), + child: Text(title), + onPressed: listener )); } - void _handleURLButtonPress(BuildContext context, String url) async { - FlutterWebBrowser.openWebPage(url: url, androidToolbarColor: Colors.deepPurple); - } } \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 4160f5fd05968644705337c7c8f70a56e83d3306..89bf07e8392067c27254eaf19223e3d86c2cfabc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -170,6 +170,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.2.0" + url_launcher: + dependency: "direct main" + description: + name: url_launcher + url: "https://pub.dartlang.org" + source: hosted + version: "5.1.0" vector_math: dependency: transitive description: @@ -179,4 +186,4 @@ packages: version: "2.0.8" sdks: dart: ">=2.2.2 <3.0.0" - flutter: ">=0.1.4 <2.0.0" + flutter: ">=1.5.0 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 40f0cacc18098af36ea40f0a89cf7817407250b2..6f7bcbd567c8950306fefad7077f5874d5e39baa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,6 +28,7 @@ dependencies: uni_links: 0.2.0 http: ^0.12.0 flutter_app_auth_wrapper: ^0.1.1+3 + url_launcher: 5.1.0 dev_dependencies: flutter_test: