Skip to content

umituz/react-native-firebase-anonymous

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@umituz/react-native-firebase-anonymous

Firebase Anonymous Authentication utilities and helpers for React Native apps - Centralized guest user management.

Features

  • Centralized Auth Utilities: Single source of truth for anonymous/guest user checks
  • Firestore Query Helpers: Skip queries for guest users automatically
  • Type-Safe: Full TypeScript support
  • DDD Architecture: Domain-Driven Design structure
  • React Hooks: Ready-to-use React hooks for anonymous auth

Installation

npm install @umituz/react-native-firebase-anonymous

Peer Dependencies

  • @umituz/react-native-firebase >= 1.0.0
  • @umituz/react-native-firebase-auth >= 1.0.0
  • firebase >= 11.0.0
  • react >= 18.2.0
  • react-native >= 0.74.0

Usage

Check Authentication State

import { getFirebaseAuth } from '@umituz/react-native-firebase-auth';
import { checkAuthState, isGuest } from '@umituz/react-native-firebase-anonymous';

const auth = getFirebaseAuth();
const state = checkAuthState(auth);

if (state.isGuest) {
  // Handle guest user
  console.log('User is anonymous/guest');
}

Skip Firestore Queries for Guest Users

import { getFirebaseAuth } from '@umituz/react-native-firebase-auth';
import { getFirestore } from 'firebase/firestore';
import { shouldSkipFirestoreQuery } from '@umituz/react-native-firebase-anonymous';

const auth = getFirebaseAuth();
const db = getFirestore();

async function getCredits(userId: string) {
  // Check if query should be skipped
  const skipResult = shouldSkipFirestoreQuery(auth, {
    userId,
    skipForGuest: true, // Skip for guest users (default: true)
  });

  if (skipResult.shouldSkip) {
    // Return default value for guest users
    return { credits: 0 };
  }

  // Proceed with Firestore query
  const doc = await getDoc(doc(db, 'users', userId));
  return doc.data();
}

Use React Hook

import { getFirebaseAuth } from '@umituz/react-native-firebase-auth';
import { useAnonymousAuth } from '@umituz/react-native-firebase-anonymous';

function MyComponent() {
  const auth = getFirebaseAuth();
  const { isGuest, signInAnonymously, loading } = useAnonymousAuth(auth);

  if (loading) return <Loading />;

  if (!isGuest) {
    return <AuthenticatedContent />;
  }

  return (
    <View>
      <Text>Guest User</Text>
      <Button onPress={signInAnonymously}>Sign In Anonymously</Button>
    </View>
  );
}

API Reference

Utilities

checkAuthState(auth: Auth | null): AuthCheckResult

Check comprehensive authentication state.

shouldSkipFirestoreQuery(auth: Auth | null, options?: FirestoreQueryOptions): FirestoreQueryResult

Determine if Firestore query should be skipped based on auth state.

Options:

  • skipForGuest: Skip query if user is anonymous (default: true)
  • skipIfNotAuthenticated: Skip query if user is not authenticated (default: true)
  • verifyUserId: Verify userId matches current user (default: true)
  • userId: User ID to verify

Hooks

useAnonymousAuth(auth: Auth | null): UseAnonymousAuthResult

React hook for anonymous authentication state and operations.

Architecture

This package follows Domain-Driven Design (DDD) principles:

  • Domain Layer: Entities and business logic
  • Infrastructure Layer: Utilities and services
  • Presentation Layer: React hooks

License

MIT

Author

Ümit UZ umit@umituz.com

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published