• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
  • Auth Overview
  • Global Configuration
    • Setting up your Firebase Project
    • Setting up Firebase Functions
  • Writing data to Firestore
  • Commands
  • Introduction
  • Production Checklist
  • Introduction
  • Overview
  • Stripe Configuration
  • Running Tests
  • Introduction
  • Setting up Firebase Auth
  • Fetching data from Firestore
  • Technical Details
  • Extending Organizations
  • Stripe Webhooks
  • CI Tests
  • Initial Setup
  • React Hooks
  • Auth Flow
  • API requests
  • Code Style
  • Clone the repository
  • Security Rules
  • User Permissions
  • Limitations
  • Project Structure
  • Third-Party Providers
  • Reading data from Storage
  • Running the application
  • Subscription Permissions
  • One-Time Payments
  • Running the App
  • Email Link Authentication
  • Uploading data to Storage
  • Security Rules
  • Migrate to Lemon Squeezy
  • Project Configuration
  • Multi-Factor Authentication
  • Writing your own Fetch
  • Translations and Locales
  • Coding Conventions
  • Environment Variables
  • Architecture and Folder Structure
    • Structure your Application
    • Data Model
  • Requiring Email verification
  • Sending Emails
  • Tailwind CSS and Styling
  • Validating API payload with Zod
  • Authentication
  • Onboarding Flow
  • Logging
  • Development: adding custom features
  • Prevent abuse with AppCheck
  • Enable CORS
  • Encrypting Secrets
  • User Roles
  • Firestore: Data Fetching
  • Custom React Hooks
  • Custom React Hooks
  • Firestore: Data Writing
  • Troubleshooting
  • Forms
  • Application Pages
  • API Routes
  • API Routes Validation
  • Translations
  • Adding pages to the Marketing Site
  • Deploying to Production
  • Updating to the latest version
This kit is no longer maintained.

Uploading data to Storage | Remix.js Firebase SaaS Kit

Learn how to upload your data to Firebase Storage in your Remix application

When uploading data to storage, the data needs to be secured so that it can only be accessed by the organization that created it; we need to add the organization ID to the file's custom data.

By doing so, we will be able to compare the custom data ID with the user's organization ID, and verify they match.

1) Add the Storage Provider

First, we need to wrap the component using Firebase Storage with the FirebaseStorageProvider, which is responsible for initializing the Firebase Storage SDK.

tsx
import FirebaseStorageProvider from '~/core/firebase/components/FirebaseStorageProvider';
<FirebaseStorageProvider>
<ComponentThatUsesStorage />
</FirebaseStorageProvider>

2) Using the Firebase Storage SDK

Now we can use the Firebase Storage SDK by using the useStorage hook. From Here, we can simply use the SDK described by the Firebase documentation.

tsx
const storage = useStorage();

Let's assume we have a function uploadFile that receives a File parameter, and that we need to upload to Firebase Storage:

tsx
import { ref, uploadBytes } from 'firebase/storage';
import { toast } from 'sonner';
import { useStorage } from 'reactfire';
import { useCurrentOrganization } from './use-current-organization';
function Component() {
const storage = useStorage();
const organization = useCurrentOrganization();
async function uploadFile(file: File) {
if (!organization) return;
const organizationId = organization.id;
const path = `/${organizationId}/uploads/${file.name}`;
const reference = ref(storage, path);
const promise = uploadBytes(reference, file, {
cacheControl: 'max-age=31536000',
customMetadata: {
organizationId,
},
});
return toast.promise(promise, {
success: `Yay, uploaded!`,
loading: `Uploading...`,
error: `Error :(`
});
}
return <UploadForm onFileChosen={uploadFile} />
}

3) Security Rules for Storage

As you can see from the code above, we are setting some custom metadata when we upload the file:

ts
customMetadata: {
organizationId,
},

By doing this, we're effectively telling the file to store organizationId as metadata on the file.

This will help us with the Storage security rules for disallowing users in other organizations from reading the file:

js
match /organizations/{organizationId}/{fileName=**} {
allow read: if resource.metadata.organizationId == request.auth.token.organizationId;
allow write: if request.auth.token.organizationId == organizationId;
}
On this page
  1. 1) Add the Storage Provider
    1. 2) Using the Firebase Storage SDK
      1. 3) Security Rules for Storage