Last active
April 16, 2020 20:27
-
-
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 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 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