Skip to content
Snippets Groups Projects

Resolve "Rework auth flow."

Merged Igor Markin requested to merge 5-rework-auth-cancelation-flow into master
Files
4
+ 23
125
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';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';
import '../../config.dart';
import '../app.dart';
class Home extends StatefulWidget {
Home({@required this.mode, @required this.invokerURL, @required this.setMode, @required this.host});
final String mode;
final String invokerURL;
Home({
@required this.mode,
@required this.host,
@required this.setHost,
@required this.authorizeClick,
@required this.openDashboardClick,
@required this.buttonsHidden,
});
final AppMode mode;
final String host;
final void Function(String) setMode;
final bool buttonsHidden;
final void Function(String) setHost;
final void Function() openDashboardClick;
final void Function() authorizeClick;
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
String _host = Config.appFlavor == Flavor.DEVELOPMENT ? Config.HOSTS[0] : Config.DEFAULT_APP_HOST;
bool _hidden = true;
@override
initState() {
super.initState();
if (widget.host != null) {
setState(() {
_host = widget.host;
});
}
showMode(widget.mode);
FlutterAppAuthWrapper.eventStream().listen((data) {
var token = json.decode(data.toString())["access_token"];
_showAlert(token);
widget.setMode("");
}, onError: (error) {
log("Err $error");
widget.setMode("");
});
Timer(
Duration(seconds: 3),
() {
setState(() {
_hidden = false;
});
}
);
}
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&host=$_host");
} 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.host != oldWidget.host && widget.host != null) {
setState(() {
_host = widget.host;
});
}
if (widget.mode != oldWidget.mode) {
showMode(widget.mode);
}
super.didUpdateWidget(oldWidget);
}
showMode(mode) {
if (mode == "app") {
openVereign();
} else if (mode == "oauth") {
startOAuth();
}
}
openVereign() {
FlutterWebBrowser.openWebPage(url: _host, androidToolbarColor: Color(0xFFd51d32));
}
startOAuth() {
var params = Config.getOAuthParams(host: _host);
FlutterAppAuthWrapper.startAuth(
AuthConfig(
clientId: params["clientId"],
clientSecret: params["clientSecret"],
redirectUrl: params["redirectUrl"],
state: "login",
prompt: "consent",
endpoint: AuthEndpoint(
auth: params["authEndpoint"], token: params["tokenEndpoint"]),
scopes: [
"user_account_status",
"user_territory",
"user_profile"
],
),
);
}
@override
Widget build(BuildContext context) {
if (_hidden) {
if (widget.buttonsHidden) {
return Scaffold();
}
var children = <Widget>[
_urlButton(context, "Open Dashboard", openVereign),
_urlButton(context, "Authorize with Vereign", startOAuth)
_urlButton(context, "Open Dashboard", widget.openDashboardClick),
];
if (widget.mode == AppMode.Authorization) {
children.add(_urlButton(context, "Authorize with Vereign", widget.authorizeClick));
}
if (Config.appFlavor == Flavor.DEVELOPMENT) {
children.add(
wrapInContainer(
DropdownButton<String>(
value: _host,
value: widget.host,
onChanged: (String newValue) async {
setState(() {
_host = newValue;
});
widget.setHost(newValue);
final prefs = await SharedPreferences.getInstance();
prefs.setString("host", newValue);
},
Loading