auth_state_manager 0.1.2 copy "auth_state_manager: ^0.1.2" to clipboard
auth_state_manager: ^0.1.2 copied to clipboard

outdated

This package helps you to monitor auth statuses in your app.

example/lib/main.dart

import 'dart:async';

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

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AuthStateManager.initializeAuthState();
  runApp(
    const MaterialApp(
      home: MyApp(),
    ),
  );
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final StreamSubscription _authStateSub;

  @override
  void initState() {
    super.initState();
    _authStateSub = AuthStateManager.instance.authStateAsStream.listen((state) {
      if (!mounted) return;
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(
          content: Text('isAuthenticated: $state'),
        ),
      );
    });
  }

  @override
  void dispose() {
    super.dispose();
    _authStateSub.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return const AuthStateListener(
      authenticated: MainScreen(),
      unAuthenticated: LoginScreen(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Auth State Manager'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'Authenticated',
              style: TextStyle(
                fontSize: 24,
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                AuthStateManager.instance.logOut();
              },
              child: const Text(
                'Sign Out',
                style: TextStyle(
                  fontSize: 16,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Auth State Manager'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Text(
              'UnAuthenticated',
              style: TextStyle(
                fontSize: 24,
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: () async {
                final bool isSuccessful =
                    await AuthStateManager.instance.setToken('MyToken');
                if (isSuccessful) {
                  AuthStateManager.instance.login();
                }
              },
              child: const Text(
                'Log in',
                style: TextStyle(
                  fontSize: 16,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
8
likes
35
points
50
downloads

Publisher

unverified uploader

Weekly Downloads

This package helps you to monitor auth statuses in your app.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, shared_preferences

More

Packages that depend on auth_state_manager