Skip to content

Instantly share code, notes, and snippets.

@mhadaily
Last active April 16, 2020 20:27
Show Gist options
  • Save mhadaily/356070b22db4940ae524205bec583967 to your computer and use it in GitHub Desktop.
Save mhadaily/356070b22db4940ae524205bec583967 to your computer and use it in GitHub Desktop.
Flutter OAuth Web View and parse code from Callback in Flutter widget
// THIS IS JUST AN EXAMPLE AND POSSIBLY ONE SOLUTUON
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class OauthWebCallBackFlutter extends StatefulWidget {
const OauthWebCallBackFlutter(this.appID);
final String appID;
@override
_OauthWebCallBackFlutterState createState() => _OauthWebCallBackFlutterState();
}
class _OauthWebCallBackFlutterState extends State<OauthWebCallBackFlutter> {
final FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
StreamSubscription<String> subscription;
@override
void initState() {
subscription = flutterWebviewPlugin.onUrlChanged.listen((String url) {
const String callbackURL = 'https://example.com/callback';
if (url.startsWith(callbackURL)) {
final String code = Uri.parse(url).queryParameters['code'].toString();
print('CODE $code');
// DO whatever you want
}
});
super.initState();
}
@override
void dispose() {
subscription?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(),
body: SafeArea(
child: WebviewScaffold(
url:
'https://somelink/authorize?client_id=secrect&redirect_uri=https://example.com/callback&response_type=code&scope=example',
clearCookies: true,
clearCache: true,
bottomNavigationBar: null,
geolocationEnabled: false,
allowFileURLs: false,
resizeToAvoidBottomInset: true,
withJavascript: true,
withLocalStorage: true,
displayZoomControls: false,
withZoom: true,
hidden: true,
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment