Skip to content

flutter-news-app-full-source-code/auth-inmemory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

auth_inmemory

coverage: percentage style: very good analysis License: PolyForm Free Trial

An in-memory implementation of the AuthClient interface. This package provides a mock authentication client that operates entirely on in-memory data, making it suitable for demonstration purposes, local development, and testing without requiring a live backend.

Getting Started

Add the following to your pubspec.yaml dependencies:

dependencies:
  auth_inmemory:
    git:
      url: https://github.com/flutter-news-app-full-source-code/auth-inmemory

Features

This package implements the AuthClient interface, providing the following in-memory simulated authentication methods:

  • authStateChanges: A stream that emits the current authenticated User or null on state changes.
  • getCurrentUser: Retrieves the currently authenticated User.
  • requestSignInCode: Simulates sending a sign-in code to an email. This method supports an optional isDashboardLogin flag. When true, it simulates a privileged flow where only [email protected] is allowed to request a code; otherwise, it throws an UnauthorizedException.
  • verifySignInCode: Simulates verifying a sign-in code and authenticating a user. This method also supports an optional isDashboardLogin flag. When true, it simulates a privileged flow where only [email protected] can successfully verify a code (throwing a NotFoundException for other emails) and the authenticated user is assigned the UserRoles.admin role.
  • signInAnonymously: Simulates signing in a user anonymously.
  • signOut: Simulates signing out the current user.
  • currentToken: A custom getter to retrieve the simulated authentication token.

Usage

Here's how you can use AuthInmemory in your application for demo or testing environments:

import 'package:auth_inmemory/auth_inmemory.dart';
import 'package:core/core.dart'; // For User and AuthSuccessResponse

void main() async {
  final authClient = AuthInmemory();

  // Listen to authentication state changes
  authClient.authStateChanges.listen((user) {
    if (user != null) {
      print('User authenticated: ${user.email ?? 'Anonymous'}');
    } else {
      print('User signed out.');
    }
  });

  // Simulate anonymous sign-in
  try {
    final anonymousAuthResponse = await authClient.signInAnonymously();
    print('Signed in anonymously. User ID: ${anonymousAuthResponse.user.id}');
    print('Current Token: ${authClient.currentToken}');
  } catch (e) {
    print('Anonymous sign-in failed: $e');
  }

  // Simulate email sign-in flow
  const testEmail = '[email protected]';
  try {
    await authClient.requestSignInCode(testEmail);
    print('Sign-in code requested for $testEmail');

    // In a real app, the user would input the code received via email
    const code = '123456'; // Hardcoded code for in-memory demo

    final verifiedAuthResponse =
        await authClient.verifySignInCode(testEmail, code);
    print('Verified sign-in for ${verifiedAuthResponse.user.email}');
    print('Current Token: ${authClient.currentToken}');
  } on InvalidInputException catch (e) {
    print('Invalid input: ${e.message}');
  } on AuthenticationException catch (e) {
    print('Authentication failed: ${e.message}');
  } catch (e) {
    print('Sign-in failed: $e');
  }

  // Get current user
  final currentUser = await authClient.getCurrentUser();
  print('Current user (after operations): ${currentUser?.email}');

  // Simulate sign-out
  try {
    await authClient.signOut();
    print('User signed out successfully.');
  } catch (e) {
    print('Sign-out failed: $e');
  }
}

🔑 Licensing

This package is source-available and licensed under the PolyForm Free Trial 1.0.0. Please review the terms before use.

For commercial licensing options that grant the right to build and distribute unlimited applications, please visit the main Flutter News App - Full Source Code Toolkit organization.

About

In-memory implementation of the AuthClient interface.

Resources

License

Stars

Watchers

Forks

Languages