Handle the registration request on the back end with Node.js.
- [Instructor] It's been a while since we've been in our backend project, but let's open up our server Js. Next, let's create a new router that will contain all of our auth routes. So let's copy line 14 and paste it below and then let's create a variable for our auth route. Then let's go down to line 34, copy that, and paste it below as well and then let's change it to /auth for the first parameter and then pass in our auth router. Next let's create our first auth register route, so I'll go below our last API route on line 29 and I'll add a new auth.post and it will have a register end point and inside its body we'll just do a quick console log test.
And let's quickly give that a try with Postman and make sure you have post selected from the drop down and our correct path of /auth/register then let's hit send. Now back in our server let's view the terminal and then we should see our registered console log and so our new auth router with the register route is working. The nice thing about using routers is if we ever wanted to, at some point in the future, to take out all of our auth routes and put them into their own auth.js file we could then import that and simply attach it to our express app with app.use and passing in the variable we get in from that import.
Next let's test this out with Angular. So instead of register, I'll output to the console request.body then let's go to our Angular app and fill out the form and if we viewed inside the terminal we can see we're getting all the details but notice we are also getting the confirm password which is something only the front that needs for safety and we should not make it available to the back end. Let's remove it before we send the register data.
So let's go back to our front end and before we make a call to http.post on line 12 we'll add a new line above and we'll type in delete user.confirmPassword and let's give that a try. So back in our app let's put in all the details in our register form and let's take a look at our server and as you can see now we're getting all the details but without the confirm password we had originally.
Next we need to actually store our users somewhere on the back end. Since I don't want to take away focus from Node and Angular, I will not introduce a database and we will simply use an array to store the users. So let's create the users variable and initialize it with an empty array so let's scroll to the top and underneath our messages we'll add our users as well. So after line five I'll add a new line and I'll add a users variable and I'll set that to an empty array then we can simply push the new user to this array in our request route.
So let's scroll down and I'll replace line 36 with users.push and I'll pass in the request .body since that contains the user. One thing to note is that in a production setting we would obviously need to use a database and we would not store the password unencrypted as we are now. Next we will need to focus a little bit on authentication and how to know that the user is who they say they are by using a token.
- Setting up the infrastructure
- Displaying data in Angular 2
- Refining your layout with Angular Material
- Getting your data from Node.js
- Saving data to a list
- Creating the component
- Getting your input data
- Creating reactive forms in Angular
- Creating a security middleware
Skill Level Intermediate
Q: This course was updated on 10/17/2017. What changed?
A: We updated five videos to reflect changes to the setup and data retrieval and display processes in Angular 2. For example, instead of using a Git Angular template, the updated course uses the Angular CLI to generate a starting template, for a smooth runtime experience.