twitter_webview_auth 0.2.0 copy "twitter_webview_auth: ^0.2.0" to clipboard
twitter_webview_auth: ^0.2.0 copied to clipboard

Twitter oauth1 webview authentication implemention using webview_flutter

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:twitter_webview_auth/twitter_webview_auth.dart';

const _consumerKey = 'your_consumer_key';
const _consumerSecret = 'your_consumer_secret';
const _callbackUrl = 'your_callback_url';

void main() {
  runApp(const _ExampleApp());
}

class _ExampleApp extends StatelessWidget {
  const _ExampleApp();

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Twitter webview auth example',
      home: _LoginPage(),
    );
  }
}

class _LoginPage extends StatefulWidget {
  const _LoginPage();

  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<_LoginPage> {
  Future<void> _authenticate() async {
    final TwitterAuthResult result = await TwitterAuth(
      consumerKey: _consumerKey,
      consumerSecret: _consumerSecret,
      callbackUrl: _callbackUrl,
    ).authenticateWithTwitter(
      webviewNavigation: (Widget webview) => Navigator.of(context).push(
        MaterialPageRoute(
          builder: (_) => Scaffold(
            appBar: AppBar(title: const Text('Login')),
            body: webview,
          ),
        ),
      ),
    );

    result.when(
      success: (String token, String secret, String userId) {
        // user successfully authenticated
        Navigator.of(context).push(
          MaterialPageRoute(
            builder: (_) => _HomePage(userId: userId),
          ),
        );
      },
      failure: (dynamic e, StackTrace? st) {
        // authentication failed
      },
      cancelled: () {
        // authentication has been cancelled by the user
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: _authenticate,
          child: const Text('Login'),
        ),
      ),
    );
  }
}

class _HomePage extends StatelessWidget {
  const _HomePage({
    required this.userId,
  });

  final String userId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text('successfully authenticated as $userId'),
      ),
    );
  }
}
0
likes
130
points
52
downloads

Publisher

unverified uploader

Weekly Downloads

Twitter oauth1 webview authentication implemention using webview_flutter

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

Dependencies

flutter, freezed_annotation, oauth1, webview_flutter

More

Packages that depend on twitter_webview_auth