Skip to content
Snippets Groups Projects
home.dart 4.07 KiB
Newer Older
  • Learn to ignore specific revisions
  • igorwork's avatar
    igorwork committed
    import 'package:flutter/material.dart';
    
    import 'package:flutter_web_browser/flutter_web_browser.dart';
    
    igorwork's avatar
    igorwork committed
    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, @required this.invokerURL, @required this.setMode});
    
      final String mode;
    
      final String invokerURL;
    
      final void Function(String) setMode;
    
    
      @override
      _HomeState createState() => _HomeState();
    }
    
    
    igorwork's avatar
    igorwork committed
    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";
    
    
    class _HomeState extends State<Home> {
    
    igorwork's avatar
    igorwork committed
      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();
    
                    widget.setMode("");
    
    igorwork's avatar
    igorwork committed
    
                    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) {
    
    igorwork's avatar
    igorwork committed
        log(mode);
    
        if (mode == "app") {
    
          openVereign();
    
        } else if (mode == "oauth") {
    
          startOAuth();
    
      openVereign() {
    
        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"
            ],
          ),
        );
      }
    
    
    
    igorwork's avatar
    igorwork committed
      @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),
                      ]
    
    igorwork's avatar
    igorwork committed
                    ))));
      }
    
      Widget _urlButton(BuildContext context, String url, String title, listener) {
    
    igorwork's avatar
    igorwork committed
        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
    
    igorwork's avatar
    igorwork committed
            ));
      }
    }