Node Auth Tutorial (JWT) #2 - Auth Routes & Controllers

Net Ninja · Beginner ·🔧 Backend Engineering ·5y ago
Skills: API Design70%

Key Takeaways

Creates authentication routes and controller functions using an MVC approach in a Node.js tutorial

Full Transcript

all right then gang so before we actually code any routes or controller functions for our authentication process i want to go over all of the different routes and handler functions that we're going to be making and also how we're going to structure those a little bit too just so we've got the big picture of what we're aiming for in the back of our minds now we're also going to be adopting an mvc approach to creating this application now that stands for model view controller and it basically just means that we're going to be extracting all of our auth route handler functions into a separate auth controller file it just helps with the organization of our code now if you want to learn more about mvc in node and express definitely check out my node.js crash course tutorial right here on youtube first of all the link is going to be down below so what are the different auth routes that we're going to be creating going to look like well basically it's going to be these five routes here and we're gonna have first of all one to get a sign up page so that is to forward slash sign up this is the route itself it's gonna be a get request because we're getting a resource and that resource is the sign up page and it's gonna return that to the browser so that will contain the form to sign up with we're also going to have one for forward slash login which is also a get request to get the login page again with a form to log in this time now after that we'll have a forward slash sign up route again the same as this but this time this is a post request so it's okay that these are the same route because they can be handled differently because of the type of request this one is a get one this one is a post one and this is gonna post data that a user tries to sign up with their email and the password to the server to create a new user in the database we're gonna have a post request to forward slash login as well to authenticate a current user so after they fill in their details on the login page press submit that sends a post request to the server to take those login details check them against those stored in the database and then log them in and finally we're going to have a logout get route and that is basically just going to take the user and log them out of the application now to begin with in this lesson we're just going to focus on these four right here for signing up and logging in and we'll handle this one in a later tutorial so let's start to flesh out these routes in the code so then the first thing i want to do is create a route file for our authentication routes so instead of dumping everything inside app.js where it then becomes messy we're going to extract those routes into its own separate file and we're going to place that into a folder called routes so the idea being the larger your application becomes for every separate section of your website then you'd have a separate routes file so we're going to create a route file for our authentication routes so let's create that by saying auth routes dot js and then the first thing we need to do inside here is require the router from the express package so when we create a separate file for routes like this we don't just say app.get and then place our routes what we need to do is actually require the router from the express package then export that route at the end once we've attached routes to it and then we can plug in that router into our application so i'll show you how all of that works now we're going to say first of all const and we're going to destructure the router from the express package like so so now we have that we can create a new instance of that router by saying const and you can call this what you want i've called it lowercase router and set it equal to the thing we imported and then invoke that so this is creating a new instance of the router for us and all we then do is attach different requests to this router so for example i can say router dot get and then we're gonna get forward slash sign up and then we'd have some kind of function which fires in response to that so this is much like the routes we already have over here except instead of using app we're using the router this time that we've created right here now at the end what we're going to do is say module dot exports is equal to the router that we create so this thing right here with any of the handlers attached to it and we export that so then we take this and we import it inside app.js and we can use those routes but we'll do that later on for now let's create these different handlers so we have this one right here router.get so it's a get request handler to forward slash sign up so remember this is going to send back the sign up view now we've not created that we'll do that in a minute but first of all let's copy this and i'm going to paste it in a few times so we can change these so we want also a post request handler for forward slash sign up and in here remember we're gonna then communicate with the database eventually and add a new user and then we want a get request for forward slash login and also a post request for forward slash login as well now i did say earlier we will have a logout request handler as well but we're not going to do that in this video we'll do that later on so that's basically it that's all of our author outs for now and what we typically do is handle those inside these functions right here but we're not going to do that inside this file and this is where this whole mvc approach comes into play we're going to create a separate auth controller file and that controller file is going to have in all the different functions which we use right here just to keep our logic separate from the actual routes so again what i'm going to do is create a new folder this time call it controllers and inside that i'm going to create a new file called auth controller.js so all of those functions those handler functions are going to live inside this file now we want to export each one of these functions as well so what i'll do is attach each one of these to module dot exports and then i'll say what each function is going to be called so first of all let's create a function for this one right here so it's a sign up route and it's a get request so let's call this sign up underscore get and set that equal to a function remember in these handler functions we have the request and the response object available to us and inside all i'm going to do is render a view so response dot render and eventually this view will be a sign up view now we'll create that momentarily but first of all let's create the rest of these functions so i'm going to copy that and again i'm going to paste this down here a few times because we've got four in total and this next one is going to be for login underscore get like so and this time we want to render the login view which we'll create in a minute and then down here this is going to be sign up post to handle this one the post request to forward slash sign up and instead of rendering a view here we're eventually going to create a new user in the database for now let's just say response dot send so we're just sending some text to the screen and that text is going to be new sign up okay and then down here we want one final one and this is going to be for login underscore post and we'll do the same thing here response.send and user login all right so we have all of these different controller functions right here and they're all being exported from this file now we need to import them into this file and register them right here instead of these empty functions so let's first of all import that up at the top i'm going to say under this one const auth controller is equal to require and then we want to come out of this folder first of all so dot dot forward slash then into the controllers folder and then we want the auth controller like that now down here what we can do instead of these empty functions is just reference this auth controller which is the module.export object right here and remember on that we have all of these different functions so i could say now we want the auth controller and then dot whatever function that i want to handle this request so this is a get request to forward slash sign up so that is sign up get so now when we get a get request for this route it's gonna fire this function right here to handle that so let's do the same thing for these dudes so auth controller dots and this is sign up post and then down here we want off controller dot login get and then finally auth controller dot login post this time so that's all there is to it so now we've done that we need these views right here that we're rendering the sign up view and also the login view now automatically express an egs is going to look inside the views folder so we need to create those inside here so let me create a new file and i'm going to call this signup.ejs and all i'm going to do inside this is register the header partial that we have the footer one and then just do an h1 in between those so let me grab this thing from the home view and paste that at the top and also at the bottom and change this to footer and then right here let's do an h1 that says sign up so dead simple and then let's copy all of this and create a new file called login.ejs and inside there paste it and this time say log in okay so now we have our views which we're trying to render right here we have all of these different controller functions which we're registering for each route the only thing left that we have to do is register these routes in our application because at the minute they're just kind of floating around in space and not doing anything because we've not registered them for our application right here so let's import them first of all and to do that i'm going to say const auth routes is equal to require and we want to go into the routes folder and we want the auth routes all right so now we have those all we need to do is grab them right here and at the bottom i'm going to say app.use to use this router and i'm just going to paste it in so that is basically the same as placing all of these routes right here here but this way we're keeping all of our authentication routes separate from the main application code all right so let me save that and this is the moment of truth let me try out these two routes the get ones right here from the browser all right so if we go to forward slash sign up then we get the sign up page and if we go to forward slash login then we get the login page so all of that works now we also want to be able to test out the other routes too the post ones which are this one right here and also this one now eventually the post requests are going to be sent from the browser after a user submits either the sign up or the login form but we don't have those yet so for now we need a quick way to test them and i'll be using a tool called postman to do that in the next lesson

Original Description

Hey gang, in this node auth tutorial we'll start to create our authentication routes & controller functions. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/node-express-jwt-auth 🐱‍💻 🐱‍💻 Other Related Free Courses & Links: + Node.js Cash Course - https://www.youtube.com/watch?v=zb3Qk8SG5Ms&list=PL4cUxeGkcC9jsz4LDYc6kv3ymONOKxwBU + Modern JS Tutorial Preview - https://www.youtube.com/playlist?list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ + JWT Debugger - https://jwt.io/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
I built a native Android app in an afternoon, and I've never written a line of Kotlin
Learn how to build a native Android app without prior Kotlin knowledge, leveraging modern tools and frameworks to streamline development
Dev.to · Tilde A. Thurium
📰
Vibe Coding Is Real Now — Here’s How to Do It Without Wrecking Your Codebase
Learn how to apply vibe coding effectively to speed up feature development without compromising code quality, and why discipline is key to its success
Medium · Programming
📰
How to build your first MCP server in 10 minutes
Learn to build a Minecraft server using MCP in under 10 minutes for a seamless gaming experience
Dev.to · GrahamduesCN
📰
Revisiting My Software Engineering Journey
Learn how browsers communicate with servers using HTTP and HTTPS protocols, essential for software engineers to build scalable web applications
Medium · JavaScript
Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →