Skip to main content

orgbytes_auth_flutter 0.0.5 copy "orgbytes_auth_flutter: ^0.0.5" to clipboard
orgbytes_auth_flutter: ^0.0.5 copied to clipboard

A stateless Flutter authentication package for OrgBytes. Handles authentication only - you control token storage.

example/lib/main.dart

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // Initialize OrgbytesAuth once in main()
  await OrgbytesAuth.configure(
    apiKey: 'YOUR_API_KEY',
    baseUrl: 'https://account.orgbyte.org',
    redirectUri: 'orgbytes://auth/callback',
    productId: 'YOUR_PRODUCT_ID', // Optional: specify product ID
  );

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'OrgBytes Auth Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
        useMaterial3: true,
      ),
      // Wrap app with OrgbytesAuthScope to enable context access
      home: const OrgbytesAuthScope(child: HomeScreen()),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    // Access user from context - widget rebuilds when user changes
    final user = OrgbytesAuth.userOf(context);

    return Scaffold(
      appBar: AppBar(
        title: const Text('OrgBytes Auth'),
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: Center(
        child: user != null
            ? _buildAuthenticatedView(context, user)
            : _buildUnauthenticatedView(context),
      ),
    );
  }

  Widget _buildAuthenticatedView(BuildContext context, User user) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        if (user.picture != null)
          CircleAvatar(
            backgroundImage: NetworkImage(user.picture!),
            radius: 50,
          ),
        const SizedBox(height: 24),
        Text(
          'Welcome, ${user.name ?? "User"}!',
          style: Theme.of(context).textTheme.headlineSmall,
        ),
        const SizedBox(height: 8),
        Text(user.email, style: Theme.of(context).textTheme.bodyLarge),
        const SizedBox(height: 32),
        FilledButton.icon(
          onPressed: () => _logout(context),
          icon: const Icon(Icons.logout),
          label: const Text('Logout'),
        ),
      ],
    );
  }

  Widget _buildUnauthenticatedView(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        const Icon(Icons.lock_outline, size: 80),
        const SizedBox(height: 24),
        Text('Not logged in', style: Theme.of(context).textTheme.headlineSmall),
        const SizedBox(height: 32),
        FilledButton.icon(
          onPressed: () => _login(context),
          icon: const Icon(Icons.login),
          label: const Text('Sign in with Organization'),
        ),
        const SizedBox(height: 12),
        OutlinedButton.icon(
          onPressed: () => _register(context),
          icon: const Icon(Icons.person_add),
          label: const Text('Create Account'),
        ),
      ],
    );
  }

  Future<void> _login(BuildContext context) async {
    try {
      // Use singleton - no need to pass around authClient!
      final result = await OrgbytesAuth.instance.login();

      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Welcome, ${result.user.firstName ?? "User"}!'),
            backgroundColor: Colors.green,
          ),
        );
      }
    } catch (e) {
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Login failed: $e'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }

  Future<void> _register(BuildContext context) async {
    try {
      final result = await OrgbytesAuth.instance.register();

      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(
              'Account created! Welcome, ${result.user.firstName ?? "User"}!',
            ),
            backgroundColor: Colors.green,
          ),
        );
      }
    } catch (e) {
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text('Registration failed: $e'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }

  Future<void> _logout(BuildContext context) async {
    await OrgbytesAuth.instance.logout();
    // Note: If you stored the token yourself, clear it here too
    // await secureStorage.delete(key: 'auth_token');

    if (context.mounted) {
      ScaffoldMessenger.of(
        context,
      ).showSnackBar(const SnackBar(content: Text('Logged out')));
    }
  }
}
0
likes
145
points
45
downloads

Documentation

API reference

Publisher

verified publisherorgbyte.com

Weekly Downloads

A stateless Flutter authentication package for OrgBytes. Handles authentication only - you control token storage.

Homepage

License

MIT (license)

Dependencies

flutter, flutter_secure_storage, flutter_web_auth_2, http, url_launcher

More

Packages that depend on orgbytes_auth_flutter