My very first personnal project 🥳
Tags: beginners, react, node, webdev
Published: 2020-08-10 08:09:09

Ok, I don't know if I'm ready or not to fly alone... Maybe I won't be able to ever finish this project but I don't care !

Here I am, in front of this empty directory with nobody to help me (except the good old friend [Google](www.google.com)).

## 1. Planning the app

### a. Idea
I'm not diving into something I have no chance to complete here. 
The idea is really simple, it is going to be a simple app where you can authenticate and sell products. Registered people can add some information about them and have access to their orders. Finally, registered user can also send an order to buy any product available.

### b. Features 
-Authentication
-Add/delete a product.
-Browse products.
-Order a product.
-Access to orders.

### c. Frontend
The frontend part will be written in React.Js, I think the context API will be enough to manage my app state.
There will be 4 pages :
-(/auth) sign up and login.
-(/profile/products) manage products.
-(/profile/orders) manage orders.
-(/) browse products.

### d. Backend
The backend is a Rest API built with Node/Express and I'm using MongoDB for storing data.
My API endpoints :
-(GET)(api/products) send all products.
-(GET)(api/products/:userId) send products for current user.
-(POST)(api/products) add a product.
-(DELETE)(api/products/:pid) delete a product.
-(POST)(api/users/signup) sign up.
-(POST)(api/users/login) login.
-(GET)(api/orders/:uid) send user orders.
-(POST)(api/orders) add new order.

## 2. Process

### a. Frontend
-Brand new React project made with 
``` npx create-react-app ```

-I create my pages and start routing with react-router-dom package.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eb29jfbd92pqfxotd2q0.png)

-Let's design my core components, I'm working on a really simple design because I prefer to focus on the logic.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1ca74070cd5vfnja92zg.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wh3zyxsh8w7qnmk8thb9.png)

-Most of my components state is managed by react hooks, so far it works fine for getting/adding/deleting products.

-Just finished a Modal component which include a backdrop. They are both rendered with ReactDOM.createPortal();

-From now, I can use my Form component to send data to my backend, I'm still missing input validation though...

-After surfing on the internet for a little while I did manage to implement React context in my app to manage user authentication state.

-Users can stay logged in as long as they want, I'm storing user id & token in local storage.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mgg4580ug1se6siewrks.png)

### b. Backend
-Let's start with my app.js file.

-I'm installing Express, BodyParser, nodemon, cors and Mongoose.

-Cors and BodyParser enabled, it is time to create my routes and connect to MongoDb.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/79ebf9z6yqayurjg7mbk.png)

-Here I have to build the best possible mongoose models for my users, orders and products.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vtsk2adkw26vyicx8zzo.png)

-Serious work starts now, I am building my controllers and testing my endpoints with [Postman](https://www.postman.com/)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8nvklyjwbw8ojroq5bk6.png)

-Testing my error handling middleware.

-Products and Users routes looks good.

-Referring to my previous project I successfully added relations between my MongoDB documents. Adding a product or an order to the database properly update user's data.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o6uwv4einv6go2vwpord.png)

### c. Connection between front & back
-Thanks to axios, I can start fetching dummy data from my backend to make sure everything is working fine.

-Authentication seems to work, I'm trying to send back token with JWT from my backend to the frontend and store it somewhere.

-Added a middleware to check if user's token is real and protect routes that require the user to be authenticated. 

## 3. Build & Deploy
-Pushed backend to heroku with CLI (I am always so surprised how fast and easy it is).

-Configuring environment variables and testing all routes.

-Frontend is hosted by firebase and everything is working as expected.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pksk8cshkswgd7q5wi7u.png)

[This fullstack project is now live](https://fullstack-project1-mern.web.app/) 🤩✅

No matter how buggy and dummy is this app, I enjoyed myself so much working on it! I am also really proud that I could implement all features I wanted.

I could spend time improving UX, fixing layout bugs and adding feature (damn, there is still no input validation at all) but I can't wait to start another project and try something new such as [Material-UI](https://material-ui.com/).

Once again, every tip to improve my way of coding or writing is more than welcome 😇

Thank you for reading !

Source code:  {% github KillianFrappartDev/FullStack-Project1 no-readme %}