Database Setup
This document provides an overview of setting up the database for your project. We'll cover the necessary steps for both MongoDB (used with NextAuth) and Supabase.
- NextAuth with MongoDB
- Supabase
MongoDB Setup
When using NextAuth, MongoDB is used as the database. Follow these steps to set up MongoDB:
Setup
-
Create a new project and deploy a cluster on MongoDB Atlas:
- Go to MongoDB Atlas and create a new project.
- Deploy a new cluster.
-
Run a local database for your dev setup:
- Running a local instance of MongoDB can help you work offline and speed up development.
-
Allow connections from your computer and production deployment(s):
- In your MongoDB Atlas project, click [Network Access] then [+ Add IP Address].
- Enter
0.0.0.0/0in [Access List Entry]. This allows connections from anywhere.
-
Add your connection string to
.env.local:- Rename
.env.exampleto.env.local. - Add your MongoDB connection string to
MONGODB_URIin.env.local.
- Rename
Mongoose (Optional)
Mongoose makes it easier to deal with MongoDB and has some cool features.
- Models:
- Models are defined in the folder
/models. Add any new models there.
- Models are defined in the folder
Supabase Setup
When using Supabase, the Supabase DB is used as the database. Follow these steps to set up Supabase:
Setup
-
Create a profiles table:
-
In the Supabase SQL Editor, run the following query to add a users table:
create table public.users (
id uuid not null references auth.users on delete cascade,
email text,
primary key (id)
);
alter table public.users enable row level security;
-
-
Add Row Level Security (RLS) policies:
- Go to the new users table and add the following RLS policies:
- Enable read access for authenticated users only.
- Enable insert access for authenticated users only.
- Go to the new users table and add the following RLS policies: