Skip to main content

Overview

The Restaurant Reservation System uses Firebase for authentication, database, and email services. This guide covers the complete Firebase setup process.

Prerequisites

  • A Firebase account
  • Flutter SDK installed
  • FlutterFire CLI installed: dart pub global activate flutterfire_cli

Firebase Project Setup

1

Create Firebase Project

  1. Go to Firebase Console
  2. Click “Add project”
  3. Enter your project name (e.g., software-restaurante-59030)
  4. Disable Google Analytics (optional for this project)
  5. Click “Create project”
2

Register Your App

Web App

  1. In Firebase Console, click the Web icon (</>)
  2. Register app with a nickname
  3. Enable Firebase Hosting (recommended)
  4. Copy the configuration values

Android/iOS (Optional)

Run FlutterFire CLI to automatically configure:
flutterfire configure
3

Configure Firebase in Flutter

Create lib/firebase_options.dart with your Firebase credentials:
lib/firebase_options.dart
import 'package:firebase_core/firebase_core.dart' show FirebaseOptions;
import 'package:flutter/foundation.dart'
    show defaultTargetPlatform, kIsWeb, TargetPlatform;

class DefaultFirebaseOptions {
  static FirebaseOptions get currentPlatform {
    if (kIsWeb) {
      return web;
    }
    switch (defaultTargetPlatform) {
      case TargetPlatform.android:
        return android;
      case TargetPlatform.iOS:
        return ios;
      default:
        throw UnsupportedError('Platform not supported');
    }
  }

  static const FirebaseOptions web = FirebaseOptions(
    apiKey: 'YOUR_API_KEY',
    appId: 'YOUR_APP_ID',
    messagingSenderId: 'YOUR_SENDER_ID',
    projectId: 'your-project-id',
    authDomain: 'your-project-id.firebaseapp.com',
    storageBucket: 'your-project-id.firebasestorage.app',
    measurementId: 'YOUR_MEASUREMENT_ID',
  );

  static const FirebaseOptions android = FirebaseOptions(
    apiKey: 'YOUR_ANDROID_API_KEY',
    appId: 'YOUR_ANDROID_APP_ID',
    messagingSenderId: 'YOUR_SENDER_ID',
    projectId: 'your-project-id',
    storageBucket: 'your-project-id.firebasestorage.app',
  );

  static const FirebaseOptions ios = FirebaseOptions(
    apiKey: 'YOUR_IOS_API_KEY',
    appId: 'YOUR_IOS_APP_ID',
    messagingSenderId: 'YOUR_SENDER_ID',
    projectId: 'your-project-id',
    storageBucket: 'your-project-id.firebasestorage.app',
    iosBundleId: 'com.example.appRestaurante',
  );
}
Never commit real API keys to public repositories. Use environment variables for production.

Firebase Authentication Setup

1

Enable Authentication Methods

  1. In Firebase Console, go to AuthenticationSign-in method
  2. Enable Email/Password authentication
  3. Enable Google authentication
  4. Configure authorized domains for production
2

Configure Google Sign-In

Web Configuration

  1. Go to AuthenticationSign-in methodGoogle
  2. Add your domain to authorized domains
  3. Copy the Web Client ID for OAuth

Implementation

The app uses ServicioAutenticacion to handle authentication:
// Initialize authentication service
final auth = ServicioAutenticacion();

// Sign in with Google
final credential = await auth.iniciarSesionConGoogle();

// Sign in with Email/Password
final credential = await auth.iniciarSesionConEmail(
  email: '[email protected]',
  password: 'password123',
);
The service automatically forces account selection on web (prompt: select_account) to prevent auto-login issues.
3

Phone Authentication (Optional)

For SMS verification:
  1. Enable Phone authentication in Firebase Console
  2. Configure reCAPTCHA (required for web)
  3. Use the built-in phone verification methods:
await auth.enviarCodigoSMS(
  numeroTelefono: '+5491112345678',
  onCodeSent: (message) => print(message),
  onError: (error) => print(error),
  onAutoVerified: () => print('Auto-verified'),
);

await auth.verificarCodigoSMS(codigo: '123456');

Firestore Database Setup

1

Create Firestore Database

  1. Go to Firestore Database in Firebase Console
  2. Click Create database
  3. Start in Production mode (we’ll add rules next)
  4. Select a location close to your users
2

Configure Security Rules

Create firestore.rules with appropriate security:
firestore.rules
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Negocios (businesses) - read by all, write by owner
    match /negocios/{negocioId} {
      allow read: if true;
      allow write: if request.auth != null && request.auth.uid == resource.data.ownerId;
    }

    // Mesas (tables) - read by all, write by business owner
    match /mesas/{mesaId} {
      allow read: if true;
      allow write: if request.auth != null;
    }

    // Reservas (reservations) - read/write with authentication
    match /reservas/{reservaId} {
      allow read: if request.auth != null;
      allow create: if request.auth != null;
      allow update: if request.auth != null;
      allow delete: if request.auth != null;
    }

    // Horarios (schedules) - read by all, write by owner
    match /horarios/{horarioId} {
      allow read: if true;
      allow write: if request.auth != null;
    }

    // Historia (restaurant history) - read by all
    match /historia/{historiaId} {
      allow read: if true;
      allow write: if request.auth != null;
    }

    // Mail queue (for Trigger Email extension)
    match /mail/{mailId} {
      allow read, write: if false; // Only Firebase Extensions can write
    }
  }
}
Deploy rules:
firebase deploy --only firestore:rules
3

Initialize Firestore Collections

The app uses these Firestore collections:
CollectionPurposeKey Fields
negociosRestaurant businessesnombre, email, duracionPromedioMinutos
mesasRestaurant tablesnombre, capacidad, zona, negocioId
reservasCustomer reservationsmesaId, fechaHora, estado, contactoCliente
horariosOpening hoursnegocioId, dias, horaInicio, horaFin
historiaRestaurant storynegocioId, titulo, descripcion
mailEmail queue (auto-managed)to, message, delivery
Collections are created automatically when you add the first document. No manual setup needed!

Initialize Firebase in Your App

In lib/main.dart, initialize Firebase before running the app:
lib/main.dart
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
import 'service_locator.dart';

void main() async {
  // Ensure Flutter bindings are initialized
  WidgetsFlutterBinding.ensureInitialized();
  
  // Initialize Firebase
  try {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
  } catch (e) {
    // Handle duplicate initialization during hot restart
    if (e.toString().contains('already exists')) {
      // Ignore - already initialized
    } else {
      rethrow;
    }
  }
  
  // Initialize dependency injection
  setupServiceLocator();
  
  runApp(const MyApp());
}

Firebase Hosting Setup

1

Install Firebase CLI

npm install -g firebase-tools
2

Login and Initialize

firebase login
firebase init hosting
When prompted:
  • Select your existing Firebase project
  • Set public directory: build/web
  • Configure as single-page app: Yes
  • Set up automatic builds with GitHub: Optional
3

Configure firebase.json

firebase.json
{
  "firestore": {
    "rules": "firestore.rules"
  },
  "hosting": {
    "public": "build/web",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}
4

Build and Deploy

# Build Flutter web app
flutter build web

# Deploy to Firebase Hosting
firebase deploy --only hosting
Your app will be live at: https://your-project-id.web.app

Dependencies

Add these Firebase packages to pubspec.yaml:
pubspec.yaml
dependencies:
  # Firebase Core
  firebase_core: ^3.3.0
  
  # Firebase Authentication
  firebase_auth: ^5.1.0
  google_sign_in: ^6.2.1
  
  # Cloud Firestore
  cloud_firestore: ^5.6.12
Then run:
flutter pub get

Verification

Test your Firebase setup:
  1. Authentication: Try signing in with Google and Email/Password
  2. Firestore: Check that data appears in Firebase Console when creating reservations
  3. Hosting: Visit your deployed URL and verify the app loads
Always test security rules in Firebase Console’s Rules Playground before deploying to production.

Troubleshooting

”Firebase already initialized” error

This happens during hot restart. The code already handles this:
try {
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
} catch (e) {
  if (e.toString().contains('already exists')) {
    // Safe to ignore
  }
}

Google Sign-In not working on web

Ensure your domain is in Authorized domains in Firebase Console → Authentication → Settings.

Firestore permission denied

Check your security rules. For development, you can temporarily use:
allow read, write: if request.auth != null;

Next Steps

Build docs developers (and LLMs) love