Newer
Older
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';
Home({@required this.mode, @required this.invokerURL, @required this.setMode});
final void Function(String) setMode;
@override
_HomeState createState() => _HomeState();
}
String clientId = "222222";
String clientSecret = "22222222";
String redirectURL = "app://com.vereign.app/oauth2";
String authEndpoint = "https://gospodinbodurov.dev.vereign.com/api/oauth2/authorize";
String tokenEndpoint = "https://gospodinbodurov.dev.vereign.com/api/oauth2/token";
final _links = ['vereign://app.vereign.com', 'vereign://app.vereign.com/oauth2', 'testredirect://app.vereign.com', 'https://igormarkin.dev.vereign.com', 'https://gospodinbodurov.dev.vereign.com'];
@override
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("Open ${widget.invokerURL}");
try {
await launch("${widget.invokerURL}?token=$token");
} catch (e) {
log("Error launching url ${widget.invokerURL}");
}
}
@override
void didUpdateWidget(Home oldWidget) {
// this method IS called when parent widget passes new "props"
// unlike React, this method IS called _before_ the build
// unlike React, this method ISN'T called after setState()
if (widget.mode != oldWidget.mode) {
showMode(widget.mode);
}
super.didUpdateWidget(oldWidget);
}
showMode(mode) {
FlutterWebBrowser.openWebPage(url: 'https://gospodinbodurov.dev.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(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
// 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, 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(title),
onPressed: listener