This documentation provides a step-by-step guide to creating a Full Stack web application using the MERN (MongoDB, Express, React, Node.js) technology stack. The final product will be a Recipe App with user authentication.
Before you begin, make sure you have the following installed:
Node.js MongoDB Visual Studio Code (or any code editor of your choice)
- Create two folders in your project directory:
client
andserver
.
- Install Yarn globally:
npm install -g yarn
. - In the
client
directory, create a React app:yarn create react-app
.
Bug:
If you encounter issues, follow these steps to debug:
2.1 Install Yarn globally: npm install -g yarn
.
2.2 Add Yarn's path to your system environment variables.
- In the
server
directory, initialize your Node.js project:yarn init
(accept the default options). - Install required server dependencies:
yarn add express cors bcrypt jsonwebtoken mongoose
yarn add --dev nodemon
- Create a
src
folder in theserver
directory and add anindex.js
file. - If using ES6 import syntax, add
"type": "module"
to yourpackage.json
. - Use the functions in
index.js
and run the server:node src/index.js
to check if the server starts. - Add nodemon to your
package.json
for automatic server restart.
- Create a MongoDB database and connect it to your project.
- Ensure you have MongoDB installed.
- Remember your MongoDB password.
- Create a
models
folder inserver/src
and define aUsers.js
model with a schema. - Connect to MongoDB using MongoDB Compass and create a new database called
recipes
. - Create a
routes
folder inserver/src
and add ausers.js
route file. - Import the
userRouter
inindex.js
. - Implement the registration POST request in the routes.
- Manually add data to the database.
- Test posting data to the database and password hashing.
Create a login POST route and test it. Handle incorrect password errors and return a token and user ID on success.
- Start the React development server:
client> yarn start
. - Delete unnecessary files in the client/src folder and update
index.js
. - Install required packages:
yarn add react-router-dom axios react-cookie
.react-router-dom
for routing.axios
for fetching data.react-cookie
for working with cookies in React. - Set up routing in app.js.
- Create a
pages
folder and export pages in each file. - Link the pages to the routing. If you encounter errors, restart the app using
client> yarn start
. - Create a
components
folder and add anavbar.js
file inclient/src
. - Create and write the navigation logic.
- Add the navbar to
App.js
. - Create an
App.css
file. - Customize the components and edit the Register and Login UI.
Modify login to include a logout option for logged-in users.
- Add a
Recipes.js
model inserver/src/models
and define aRecipeSchema
. - Create a
recipes.js
route inserver/src/routes
with GET and POST routes. - Link the new route to
index.js
in theserver/src
folder. - Test adding recipes to the database using tools like Insomnia.
- Test retrieving recipes from the database.
- Import
UserModel
frommodels/Users.js
and add PUT and GET/savedRecipes/ids
and GET/savedRecipes
routes. - Add
savedRecipes
in theUserSchema
. - Create a UI for displaying recipes.
- Send data to the API and navigate to the home page.
- Display all recipes on the home page.
- Add functionality for saving recipes.
- Create a page to display saved recipes.
Secure your application by validating all requests using tokens.
Ensure that saved recipes are displayed only when a user is logged in.
After following the steps outlined in this documentation, you will have successfully built a MERN Recipe App with user authentication. Here are some key features and the expected final results of your project:
- User Authentication: Users can register and log in securely. Incorrect login attempts are handled with appropriate error messages.
- Database Integration: Your app is connected to a MongoDB database, allowing users to store and retrieve their recipes.
- Token-Based Authentication: Requests to your server are secured with tokens, ensuring that only authenticated users can access certain features.
- Recipe Management: Users can add, view, and save recipes. Recipes are stored securely in the database.
- Navigation: The app includes navigation to different pages and a user-friendly interface.
- Automatic Server Restart: Nodemon is configured to automatically restart the server on code changes.
User Registration and Login: Users can sign up for an account and log in. Upon successful login, they receive a token and user ID. Recipe Management: Users can create, view, and save recipes. Saved recipes are associated with the user's account. Authentication and Authorization: Access to certain features is restricted to authenticated users only. Route Navigation: Your app has multiple pages for registration, login, recipe creation, and viewing saved recipes. Users can navigate seamlessly. Secure API: The API is secure, and all requests are validated using tokens for user authentication and authorization. Database Integration: Data is stored securely in a MongoDB database. Responsive UI: Your app has a user-friendly and responsive user interface.
Blog link: https://dev.to/yungu23/mern-recipe-app-with-authentication-46fh