authwebview 1.0.0 copy "authwebview: ^1.0.0" to clipboard
authwebview: ^1.0.0 copied to clipboard

A Flutter package for OAuth authentication using WebView.

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'AuthWebView Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  AuthorizationTokenResponse? _tokenResponse;

  final List<OAuthProvider> _providers = [];

  Future<void> _login(OAuthProvider provider) async {
    final result = await AuthService.performOAuthFlow(
      context,
      provider,
      loadingWidget: const Center(child: CircularProgressIndicator()),
    );

    setState(() {
      _tokenResponse = result;
    });
  }

  Future<void> _logout(OAuthProvider provider, String idTokenHint) async {
    final success = await AuthService.logout(provider, idTokenHint);
    if (success) {
      setState(() {
        _tokenResponse = null;
      });
    } else {
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Logout failed')),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('AuthWebView Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            if (_tokenResponse == null)
              ..._providers.map(
                (provider) => ElevatedButton(
                  onPressed: () => _login(provider),
                  child: Text('Login with ${provider.name}'),
                ),
              )
            else
              Column(
                children: [
                  const Text('Logged in successfully!'),
                  const SizedBox(height: 10),
                  Text('Access Token: ${_tokenResponse!.accessToken}'),
                  const SizedBox(height: 20),
                  ElevatedButton(
                    onPressed: () => _logout(
                        _providers.first,
                        _tokenResponse!
                            .idToken!), // Using the first provider for logout
                    child: const Text('Logout'),
                  ),
                ],
              ),
          ],
        ),
      ),
    );
  }
}
1
likes
0
points
152
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for OAuth authentication using WebView.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

crypto, flutter, http, webview_flutter

More

Packages that depend on authwebview