A basic implementation of a canteen food ordering application built with SvelteKit and Firebase. This starter project demonstrates core features including a food menu, shopping cart, user authentication, and real-time order processing. It's designed as a foundation that you can extend with additional features and customizations.
Clone the repository
git clone [REPOSITORY_URL]
cd svelte-firebase-canteen
Install dependencies
npm install
Set up Firebase
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Allow read access to all users
match /menu/{document=**} {
allow read: if true;
// Only allow writes if authenticated as admin
allow write: if request.auth != null && get(/databases/$(database)/documents/admins/$(request.auth.uid)).exists();
}
// Order-related rules
match /orders/{orderId} {
allow read: if request.auth != null && (
request.auth.uid == resource.data.uid || // Owner can read
get(/databases/$(database)/documents/admins/$(request.auth.uid)).exists() // Admin can read
);
allow create: if request.auth != null && request.resource.data.uid == request.auth.uid;
}
// Admin users collection
match /admins/{userId} {
allow read, write: if request.auth != null && get(/databases/$(database)/documents/admins/$(request.auth.uid)).exists();
}
}
}
admins
and add admin user documents with the UID as the document IDConfigure Environment
Copy the .env.example
file to .env.local
and fill in your Firebase configuration:
cp .env.example .env.local
Fill in the .env.local
file with your Firebase configuration (you can find these in your Firebase Console > Project Settings > General > Your apps > Web app):
VITE_FIREBASE_API_KEY=your-api-key
VITE_FIREBASE_AUTH_DOMAIN=your-project.firebaseapp.com
VITE_FIREBASE_DATABASE_URL=https://your-project.firebaseio.com
VITE_FIREBASE_PROJECT_ID=your-project-id
VITE_FIREBASE_STORAGE_BUCKET=your-bucket.appspot.com
VITE_FIREBASE_MESSAGING_SENDER_ID=your-sender-id
VITE_FIREBASE_APP_ID=your-app-id
VITE_FIREBASE_MEASUREMENT_ID=G-XXXXXXXXXX
Run the Application
Open http://localhost:5173 in your browser.
To populate your database with sample menu items, you'll need to set up Firebase Admin credentials:
Set up Firebase Admin (one-time setup):
service-account.json
and place it in the project rootInstall required dependencies (if not already installed):
npm install firebase-admin
Run the setup script:
node add_sample_menu.js
This will create a menu
collection with these sample items:
npm install -g firebase-tools
Login to Firebase:
firebase login
Start the emulator:
firebase emulators:start --only auth,firestore
Build the application for production:
npm run build
Deploy to Firebase:
firebase deploy --only hosting
src/
โโโ lib/
โ โโโ components/ # Reusable Svelte components
โ โ โโโ AuthForm.svelte # Authentication form
โ โ โโโ Cart.svelte # Shopping cart component
โ โ โโโ Menu.svelte # Menu display component
โ โโโ firebase.js # Firebase configuration and initialization
โ โโโ stores.js # Svelte stores for state management
โโโ routes/
โ โโโ orders/ # Order-related pages
โ โ โโโ +page.svelte # Order history page
โ โโโ +layout.svelte # Main application layout
โ โโโ +page.svelte # Main page (menu and cart)
โโโ app.css # Global styles
git checkout -b feature/your-feature
)git commit -m 'Add some feature'
)git push origin feature/your-feature
)Important Security Notes:
.env
fileTo run the development server:
npm run dev
To build for production:
npm run build
To preview the production build:
npm run preview
Licensed under the MIT License.