DEV Community

YUN GU
YUN GU

Posted on

MERN Recipe App with Authentication

Build a Full stack web application with MERN technology.

Final pages:
Image description

Image description

Image description

Image description

Image description

Image description

Here we go, we create projects step by step.

Create two folders in project folder: client and server folder

  1. npm install -g yarn
  2. Client/ yarn create react-app . Bug:

Image description

Debug:
1) npm install -g yarn
2) 系统属性-环境变量-系统变量-path-add yarn path

Image description

  1. server> yarn init -> asked questions don’t need answer, just enter
  2. server> yarn add express cors bcrypt jsonwebtoken mongoose
  3. server> yarn add --dev nodemon
  4. add src folder in server and add index.js in src folder
  5. either is ok. But if we use the import, we must add ("type": "module",) in package.json

Image description

  1. use the functions in index.js, and run server> node src/index.js to check whether server started.

Image description

  1. add this to package.json, so that everytime nodemon started use src/index.js, we don’t need to use it manually.

Image description

  1. create mongoDB database and link it to vscode, keep in mind to install mongoDB first:

Image description

Image description

  1. make request to database: 1) create models folder in server/src, and create Users.js in models folder, import mongoose, define schema and export usermodel in this file

Image description

2) connect mongoDB compass, and create new database: recipes

Image description

3) create routes folder in server/src, and create users.js in routes folder,

Image description

4) import userRouter in index.js

Image description

5) get register post in routes:

Image description

6) add data in database manually

Image description

7) try to post a data to database:

Image description

8) add authentication and authorization functionality

Image description

9) delete the data created by myself manually, and try to add data by using API, the password was hashed randomly.

Image description

Image description

  1. create login post and test it. If password is wrong will give error message, if correct will give token and userID:

Image description

Image description

Image description

Image description

  1. Back to Frontend to create recipe: 1) client> yarn start

Image description

2) delete some files in client/src, and delete some lines in index.js and

Image description

Image description

Image description

3) install some packages in react:
client> yarn add react-router-dom axios react-cookie
react-router-dom is used to create routes and different pages inside of our website
axios is used to fetching data
react-cookie is used to deal with cookies in react

4) set routes system in app.js
Image description

5) create pages folder and some files in this folder
Image description

6) export pages in each file:

Image description
Image description
Image description
Image description

7) link to pages, if have error, try to restart the page by using: client> yarn start
Image description
Image description
Image description

8) create components folder and navbar.js file in client/src
Image description

9) create and write navigate file
Image description

10) add navbar to App.js
Image description
Image description

11) write App.css file

Image description
Image description

12) edit components page, edit Register and Login UI:
Image description
Image description

  1. Make API requests
    Image description
    Image description
    Image description
    Image description

  2. Modify login to logout, when logging in:
    Image description
    Image description
    Image description

  3. Creating API for recipes:
    1) Add Recipes.js in server/src/models, and define RecipeSchema in this file:
    Image description

2) add recipes.js in server/src/routes, and define get & post routes in this file:
Image description

3) link routes to index.js in server/src folder
Image description

4) test adding recipes to database in insomnia:
Image description
Image description

5) test getting recipes from database:
Image description

6) import { UserModel } from "../models/Users.js", and add put, get "/savedRecipes/ids", get "/savedRecipes" routes
Image description

7) add savedRecipes in UserSchema:
Image description

8) create UI for recipe
Image description
Image description
Image description

9) send data to API
Image description
Image description
Image description
Image description

10) navigate to home page
Image description

11) display all recipes on the home page
Image description
Image description

12) add save recipe functionality:
Image description
Image description

13) display Saved Recipes page:
Image description
Image description

  1. Backend token to validate all the request:

    Image description
    Image description

  2. Display saved recipes only when user logged in.
    Image description

Top comments (0)