1 00:00:00,000 --> 00:00:04,646 [MUSIC] 2 00:00:04,646 --> 00:00:09,660 A major part of any user authentication system is the registration process. 3 00:00:09,660 --> 00:00:12,970 After all, you can't very well authenticate a user's identity 4 00:00:12,970 --> 00:00:16,880 until you've added a way to capture and store user information. 5 00:00:16,880 --> 00:00:19,410 In this section of the course I'll show you how to create 6 00:00:19,410 --> 00:00:22,830 a registration form that connects to a Mongo data store. 7 00:00:22,830 --> 00:00:25,110 Users will be able to sign up on your site and 8 00:00:25,110 --> 00:00:27,040 their information will be added to the database. 9 00:00:28,220 --> 00:00:32,320 First let's define the routes users will take to see the registration form and 10 00:00:32,320 --> 00:00:34,200 submit the registration information. 11 00:00:35,370 --> 00:00:38,350 We already have three routes in our application. 12 00:00:38,350 --> 00:00:40,200 We'll add two more routes now. 13 00:00:40,200 --> 00:00:44,480 They use the same URI or endpoint, /register. 14 00:00:44,480 --> 00:00:48,640 When the user requests that endpoint using the HTTP GET method, 15 00:00:48,640 --> 00:00:50,930 they'll see the registration form. 16 00:00:50,930 --> 00:00:54,550 When they use POST to send their sign up information to that same endpoint, 17 00:00:54,550 --> 00:00:58,510 the application will add that data to the database. 18 00:00:58,510 --> 00:00:59,930 Let's program these routes now. 19 00:01:01,320 --> 00:01:05,110 We have some basic routes already defined in the routes directory 20 00:01:05,110 --> 00:01:06,660 in the file called index. 21 00:01:06,660 --> 00:01:08,120 js. 22 00:01:08,120 --> 00:01:12,175 I'll start by adding a get route to display the registration form, 23 00:01:12,175 --> 00:01:19,320 router.get and then we pass it a string /register, that's the end point. 24 00:01:21,110 --> 00:01:24,180 We had a callback function as the second argument, 25 00:01:24,180 --> 00:01:28,140 the callback tells Express what to do when a user requests this end point. 26 00:01:29,290 --> 00:01:33,880 It accepts three arguments, the request object which has all the information 27 00:01:33,880 --> 00:01:38,780 related to the request that was sent from the client, the response object 28 00:01:38,780 --> 00:01:43,320 which lets us send data back to the user, and a function called next 29 00:01:43,320 --> 00:01:47,210 which tells express to continue on to the next piece of middleware. 30 00:01:47,210 --> 00:01:51,400 In other words, what function should Express run after this callback? 31 00:01:51,400 --> 00:01:54,440 I'll talk more about next in middleware later in this course. 32 00:01:54,440 --> 00:01:57,860 So if those are new concepts to you, don't worry about them for now. 33 00:01:57,860 --> 00:02:00,550 For this route we're not concerned about the requests. 34 00:02:00,550 --> 00:02:03,380 The user is just asking for the registration form. 35 00:02:03,380 --> 00:02:05,110 So we use the response. 36 00:02:05,110 --> 00:02:09,220 That's the res parameter here, to send back our response. 37 00:02:09,220 --> 00:02:11,800 Because we haven't created the registration form yet, 38 00:02:11,800 --> 00:02:14,370 let's just send back a message to see if the route works. 39 00:02:22,060 --> 00:02:23,220 Let's check it out. 40 00:02:23,220 --> 00:02:24,180 I'll save this file. 41 00:02:25,470 --> 00:02:28,310 And switch to my terminal or console program. 42 00:02:28,310 --> 00:02:30,510 I need to make sure I'm in the correct directory. 43 00:02:30,510 --> 00:02:33,615 So I'll print out my working directory by typing pwd. 44 00:02:34,730 --> 00:02:37,670 If you're using the Windows console you can use the cd command. 45 00:02:39,020 --> 00:02:45,590 Then I'll start the app by typing node app.js. 46 00:02:45,590 --> 00:02:47,140 This will start the express app and 47 00:02:47,140 --> 00:02:50,040 keep the process running in this terminal window. 48 00:02:50,040 --> 00:02:52,860 The apps running on a local host using port 3000. 49 00:02:52,860 --> 00:02:58,530 I'll open a web browser and enter localhost:3000. 50 00:02:58,530 --> 00:03:04,847 When I click the Sign Up button I'm routed to the /register endpoint. 51 00:03:06,280 --> 00:03:07,700 And I see the Register today! 52 00:03:07,700 --> 00:03:09,180 message. Great, that route's working so 53 00:03:09,180 --> 00:03:09,780 let's add a post route. 54 00:03:09,780 --> 00:03:11,040 The post route to register is where we receive 55 00:03:15,510 --> 00:03:17,870 the information entered into the registration form. 56 00:03:19,280 --> 00:03:22,156 We'll use that data to create a new user in the database. 57 00:03:33,012 --> 00:03:33,750 Again. 58 00:03:33,750 --> 00:03:38,650 Let's use res.send with a simple text message, so that we can test this route. 59 00:03:38,650 --> 00:03:43,190 Unfortunately, we can't test it yet, we need to create a registration form first. 60 00:03:43,190 --> 00:03:45,880 Then we'll be able to post information to this route. 61 00:03:45,880 --> 00:03:47,380 We'll build that form soon. 62 00:03:47,380 --> 00:03:51,633 But first let me teach you two other ways to start up an express application.