A template to create a user authentication using MongoDB
- Clone the repository:
https://github.com/yourusername/mongodb-user-auth.git
or download zip folder and extract it.
- Change to the project directory:
cd mongodb-user-auth
-
Create a New Project: Sign up or log in at MongoDB and create a new project.
-
Create a Cluster: Follow the steps to create a new cluster. For a free tier, you can select the shared cluster option which is sufficient for development purposes.
-
Create a Database User:
- Navigate to the "Database Access" section under "Security".
- Add a new database user with read and write access to any database. Remember the username and password as you will need them for the connection string.
-
Whitelist Your IP Address:
- Go to the "Network Access" section.
- Add your current IP address to allow connections to the database.
-
Connect Your Application:
- In the root of your project, create a
.env
file and add the following lines, replacing placeholders with your actual data:MONGODB_URI=your_mongodb_connection_string SECRET_KEY=your_secret_key
- Find your connection string in the "Connect" section of your cluster.
- Replace
your_mongodb_connection_string
in your.env
file with your actual connection string formatted as follows:mongodb+srv://username:password@your-cluster-url/test?retryWrites=true&w=majority
- Replace
your_secret_key
with a secret key of your choice for JWT.
- In the root of your project, create a
- Navigate to the server directory:
cd server
- Install the necessary packages:
npm install
- Start the development server:
npm start
- Navigate to the client directory:
cd client
- Install the necessary packages:
npm install
- Start the development server:
npm start