Cloudinary
Using Cloudinary for secure file uploads and management in StarterKitPro.
Default Storage Provider
StarterKitPro uses Cloudinary as the default cloud storage provider for handling file uploads like user avatars or product images. It offers a generous free tier and robust features.
Alternative: AWS S3
While Cloudinary is the default, switching to AWS S3 is straightforward and can typically be done in under a minute if needed. See the S3 documentation for details.
1. Setup
To use Cloudinary, you need to configure your API credentials in the environment variables.
Environment Variables
Create or update your .env.local
file with your Cloudinary account details:
Obtaining Credentials
You can find these credentials in your Cloudinary account dashboard:
- Sign up or Log in to your Cloudinary account.
- Navigate to the Dashboard.
- Your
Cloud Name
,API Key
, andAPI Secret
will be displayed prominently on the dashboard page, usually under the "Account Details" or similar section. - Copy these values into your
.env.local
file.
2. Centralized File Management
StarterKitPro provides a centralized helper module located at lib/cloudinary.ts
for interacting with the Cloudinary API. This module simplifies common tasks like uploading, deleting, and replacing files.
Using this centralized module ensures consistency and helps prevent "ghost files" – files that might remain orphaned in your Cloudinary storage after being deleted from your application's database or records. The helper functions manage the interaction with the Cloudinary API directly.
3. Secure Server Actions
For seamless integration with your frontend components, StarterKitPro includes pre-built Server Actions in actions/file-actions.ts
. These actions leverage the lib/cloudinary.ts
helper and provide secure endpoints for:
- Uploading new files: Handles the upload process from the client to Cloudinary.
- Deleting existing files: Removes files from Cloudinary.
- Replacing files: Deletes an old file (identified by its public ID) and uploads a new one in its place.
Security with Auth.js
All file management actions within actions/file-actions.ts
are protected
using Auth.js. This ensures that only authenticated users with the
appropriate permissions can upload, delete, or modify files, preventing
unauthorized access.
You can call these Server Actions directly from your client-side components (e.g., forms, profile settings) to manage user-uploaded files securely and efficiently.