This is Part 5 in the Full-Stack Next.js Development with Nhost series. This part focuses on file storage, upload operations, and permission-based file access control in a production application using Next.js App Router patterns.
Full-Stack Next.js Development with Nhost
1. Create Project
Set up your Nhost project
2. Protected Routes
Route protection basics
3. User Authentication
Complete auth flow
4. GraphQL Operations
CRUD operations with GraphQL
5. File Uploads
Current - File upload and management
Prerequisites
- Complete the GraphQL Operations part first
- The project from the previous part set up and running
What You’ll Build
By the end of this part, you’ll have:- A personal bucket so users can upload their own private files
- File upload functionality
- File management interface for viewing and deleting files
- Security permissions ensuring users can only access their own files
Step-by-Step Guide
1
Create a Personal Storage Bucket
First, we’ll create a storage bucket where users can upload their personal files.In your Nhost project dashboard:- Navigate to Database
- Change to schema.storage, then buckets
-
Now click on
+ Insert
on the top right corner. -
As id set
personal
, leave the rest of the fields blank and click on Insert at the bottom
2
Configure Storage Permissions
Now we need to set up permissions for the storage bucket to ensure theuser
role can only upload, view, and delete their own files.To upload files we need to grant permissions to insert on the table 
storage.files
. Because we want to allow uploading files only to the personal
bucket we will be using the bucket_id eq personal
as a custom check. In addition, we are configuring a preset uploaded_by_user_id = X-Hasura-User-id
, this will automatically extract the user_id from the session and set the column accordingly. Then we can use this in other permissions to allow downloading files and deleting them.
You can read more about storage permissions here
3
Create the File Upload System
Now let’s implement the Next.js file upload functionality using server actions, API routes, and client components. We’ll use server actions for upload and delete operations, and API routes for file viewing and downloading.First, let’s create server actions to handle file upload and delete operations. Server actions run on the server and provide a secure way to handle file operations.
src/app/files/actions.ts
4
Update Navigation Component
Add a link to the files page in the server-side navigation component.src/components/Navigation.tsx
5
Test Your File Upload System
Run your Next.js development server and test all the functionality:- Server-Side Protection: Try accessing
/files
while logged out - the middleware will redirect you to the home page. - File Upload Flow: Sign in and navigate to the Files page using the navigation link. The server component will fetch your existing files, and the client component will handle uploads.
- Upload Different File Types: Upload various file types (images, documents, PDFs, etc.) to test the file type handling.
- View and Delete Files: Test the view functionality for different file types - images and PDFs will open in new tabs, while other files will download.
- User Isolation: Sign in with different accounts to verify users can only see their own files due to storage permissions.
- Server-Side Rendering: Notice how your file list loads immediately on page refresh since it’s fetched on the server, unlike client-only React apps.
Key Features Implemented
Server/Client Architecture
Server/Client Architecture
Clear separation between server components for data fetching and client components for user interactions, following Next.js App Router best practices.
Storage Bucket & Permissions
Storage Bucket & Permissions
Dedicated personal storage bucket with proper configuration for user file isolation, leveraging Nhost’s permission system.
Server-Side Data Fetching
Server-Side Data Fetching
Files are fetched on the server and passed to client components, providing immediate data on page load without loading states.
File Upload Interface
File Upload Interface
User-friendly upload interface with file selection, preview, and progress feedback using client-side interactions.
File Management
File Management
Complete file listing with metadata, viewing capabilities, and deletion functionality with proper state management.
File Type Handling
File Type Handling
Intelligent handling of different file types with appropriate viewing/download behavior for various media types.
Route Protection
Route Protection
Automatic route protection through Next.js middleware, ensuring only authenticated users can access file upload functionality.
Error Handling
Error Handling
Comprehensive error handling with server-side error detection and client-side user feedback for all operations.