# Full Stack LLD & Projects: FullStack-5: Project Part 2- (Creating User's & Admins Route) ## Agenda * Revise the concepts taught in the previous class. * Registration route creation * Login route creation for the user * JWT and Protected Route ## Login and Registration Route Creation >Before proceeding, show a glimpse of the project done till now. >Ask the students if they have any doubt uptil now. Now that we have written the code for registration, we will be going ahead with the login process. * Registration is a one-time process. * Login- After entering the username and password, we only need to check for the credentials if the user exists in the database or not >Ask the students which method should be used to create login route. Like for registration, we used the POST method, similarly, what should be used here? The answer is, we will be using the POST method for login route creation. It is so, because we are sending the data to the backend and then matching it. ### Steps **Step 1**- Create a post request with name `/login` and it will have a async callback. **Step 2**- We will have the first check for email whether it is present in the database or not. ```javascript const user = await User.findOne({email : req.body.email}) ``` The above line is used to find the user email in the particular list of emails. **Step 3**- If the user is not present, we will not allow the user to login and give an error message. We will set `success` as `false` and message as 'user does not exist'. ```javascript if(!user){ return res.send({ success : false, message : 'User does not exist' }) } ``` **Step 4**- Now we will be checking for password. We will check if the entered password matches the password in the database. In Bcrypt, there are two methods to compare- `compare` and `compareSync`. As we are using `await`, we will be using `compare`. It will compare the entered password into hash form with the password in the database. ```javascript const validPassword = await bcrypt.compare(req.body.password , user.password) ``` Here, the first parameter is the password we have entered and the second parameter is the password corresponding to the username. **Step 5**- If the passwords do not match, we will display an error message. ```javascript if(!validPassword){ return res.send({ success : false, message : 'Invalid Password' }) } ``` **Step 6**- If everything is successful, we will send a successful message that the user has been logged in successfully. ```javascript res.send({ success : true, message : 'User Logged in', }) ``` Now, let us test our code. The route in postman will be changed to `login`. We also do not require the `name` key in our data so we erase it and only keep the `email` and `password`. We send the data and we received 'success' message. >Also show the message after entering an incorrect email and password. Now, the data needs to be shown on the frontend. We need to link the client and the server. We will be splitting the terminal- zsh and open client and server in split view.  On the left is Client and on the right is Server. To run the server we will use the following commands one by one- ```javascript cd bookmyshow-project cd server server.js ``` Now, we have to link our server and client. In our `client` folder, we will go under `package.json` and add the following code- ```javascript "proxy": "http://localhost:8082" ``` The address is the server address which can be found from postman. Our client will now use the above address as proxy. Frontend should know what data should be there, what data is coming, what data to validate. We will be creating a folder `apicalls` under `client` folder. We will be using **axios** to make API calls. We will create a file called `index.js` under `apicalls` where we will set axios. We will import axios and create axios instance. ```javascript import axios from 'axios' export const axiosInstance = axios.create({ headers : { credentials : 'include', method : 'post', 'Content-Type' : 'application/json' } }) ``` The `create` method of axios is used to create an instance. We will add the above headers. Now we will set up the route for frontend with file name `user.js`. **Step 1**- We will require axios instance. As it is in the same folder, we will just use a `.`(dot) **Step 2**- Register a new user. We will create a function`RegisterUser` which will take in the payload. it is an async function. Inside it we will use a try-catch function. A variable `response` is created and we will be using `axiosInstance` and the method is `post`. The route which we will hit on is `/register` right now. Then the response data will be returned. ```javascript export const RegisterUser = async ()=>{ try { const response = await axiosInstance.post('/register' , payload) return response.data } catch (error) { return error } } ``` >Open `pages` folder and then `Register` folder and within it open `index.js`. **Payload**  Now in the above code, we can see that there are various fields and we have to enter some data in it. There is a button of `type="submit"` which sends the data which is entered by the user. That data is known as the **payload**. In our `registerUser` function, our route is `/register`, and the method is `axios.post`. The proxy that we have set- `"proxy": "http://localhost:8082"` is the server's proxy. The axios instance that we have created will look at the server/ proxy that we have made and inside our proxy we have `register` which will set the data. Basically that user will be registered at the backend. **Step 3**- * Now, we need to get our payload out and send it to the `register` function. In our `index.js` file opened before, we will be making changes to it. * We will be using the `onFinish` function by AntD. It is an AntD version of `onSubmit`. * We will implement a `onFinish()` function. Whatever data we get, we will call it as `values`. * We will also import the `registerUser` function. ```javascript import { RegisterUser } from '../../apicalls/users' ``` * We will have a try-catch block. So, inside the try block, we will try to send values to the `registerUser` function as payload values. * If it has been successfully submitted, there won't be any error and success message will be displayed. If it has an error, an error will be displayed. ```javascript const Register = () => { const onFinish= async (values)=>{ try { const response = await RegisterUser(values) if(response.success){ message.success(response.message) console.log(response.message) } else{ message.error(response.message) console.log(response.message) } } catch (error) { message.error(error) } } ``` We will also add payload as a parameter to ```javascript export const RegisterUser = async (payload) => { try { const response = await axiosInstance.post('/register' , payload) return response.data } catch (error) { return error } } ``` We will also add the `onFinish` prop to the Form Component. ```javascript