Laravel Authentication Tutorial #4 - Registering New Users

Net Ninja · Beginner ·🔧 Backend Engineering ·1y ago

Key Takeaways

The video tutorial covers Laravel authentication, specifically registering new users, using tools like Laravel, AuthController, and Tailwind for styling validation errors. It demonstrates how to validate user input, hash passwords, and log users in using Laravel's authentication system.

Full Transcript

all right then so now we've hooked the registration and login forms up to a couple of routes to handle post requests from those forms and in turn those routes are hooked up to the or controller where we can write the logic to actually handle those requests so we've got an action called login to handle requests from the login form and we've got one called register to handle requests from the register form now in this lesson we're just going to be focused on the register action to register new users so then inside this action we get access to the request object as an argument which is of type request okay and on this request object we're able to access and validate the user input values from the form and that's the first thing we want to do right we want to validate the user input to make sure they're registering with maybe a unique email a password that's long enough Etc so the way we can do this is by making a new variable which I'm going to call validated call it what you want and we can set that equal to the request object and then we'll use a method on that called validate now this method takes an array as an argument and inside this array we can use key value pairs to specify validation rules for each input field so the key would be the input name attribute from the form and the value would be the validation criteria for that input for example we've got an input with a name attribute value of name and we could use that as the first key right here and then as an argument we'll have a string of different validation criteria the first one could be that it's required so we type out required then to add another we use a pipe and then the next one which would be that it must be maybe a string value okay then we can add a pipe to specify another after this which could be that the max number of characters for the name is 255 and then the value we submit for the name field must pass all these validation checks okay okay so the next input field had the name attribute of email so we'll use that as the next key and then as a value we'll use a string again and also say that this field is required then we'll say it must be an email and larel checks this according to its own allowed email patterns for us so we don't have to worry about making any kind of regular expressions or anything like that ourselves and finally we'll say the email must be unique within the users table so we can say unique then a colon and then users to specify the table name to do that cool so now we just need to validate the password so let's make another key with the name of password and for the value of that we'll do another string and we'll say it's required again then a pipe then we'll say it must be a string then another pipe to say that the minimum length is eight characters long and then finally I'll add another pipe and say the password must be confirmed now this last check is really useful and it saves us a lot of time so in the register form we had two inputs for the password right we had the first one with the name attribute of password and the second one with a name attribute of password underscore confirmation and when we created this form I said the name value of the second password input was very important and that it must be called password uncore confirmation and this is why because when we specify in the validate method that the password must be confirmed laravel automatically looks for a second password input where the name value is password uncore confirmation and it compares the password input value to that to make sure they're both the same but for that to work the name attribute value must be called password uncore confirmation so that's nice larel automatically checks the password Fields match for us and I think we've done all the valid a checks here all we have to do now is create the new user to do that we'll use the user model and we'll also make sure we're using the correct usern name space and then we'll use the create method on that user model and by the way when we create a new larl application larl automatically creates this user model for us along with a user table okay so as an argument we just need to pass in the validated variable which contains all the validated fields that a user needs and when we do this laral saves that new user to the user's table now by the way this validated method right here it actually returns an array of key value pairs where the key is the name of the field like name password Etc and the value is the input value of that so that's what we're passing into the create method and that's what it expects all right so now we've validated the user input oh one other thing sorry we um saved the password which is a string right but a nice little feature of more recent versions of LEL is that it automatically hashes the password for us so it's not going to be storing those passwords as plain text in the table which is good right so we've validated the user input and we've created the new user now what well even though we've saved that user to the database that's pretty much all we've done right we've not authenticated or logged that user in yet we've not created a session cookie for them either so we need to do that next and larl makes this really simple for us because it comes fully baked with its own authentication Service that we can use now technically this or service is called a facad which is basically a fancy way of saying it's a programmatic interface for some other complex system and in Lal there's a few different facades for different things and they're essentially just classes that we can use to do a bunch of stuff easily so we'll be using the or faad to log users in really easily and then LL does all of the heaven lifting in the background for us it generates a session token and a cookie for the user and it keeps track of whether a user is currently authenticated or not to use it we can start typing or and then you'll see this name space with the word facade in it so make sure you select that one to use it and then on this or class we can then use a method called log in and we can invoke that and pass a user model instance into it now we don't actually have a user instance anywhere here but this create method returns one for US based on the user it's creating so we can just store the return of that in a variable called user and then we can take that and we can pass it into the login method and that's it my friends that's all we need to do laravel then takes care of everything else for us now there is just one more thing I want to do in this action and that is to redirect the user back to the index page so we can say down here return then use the redirect function and invoke it and we'll tack on the route method to specify the name of the route we want to redirect to and in our case that is the ninjas do index route okay so we'll try this out in a moment but I want to handle one more thing before we do and that is any validation errors if there are any so when we use the validate method up here if some of the validation fails then larl redirects us back to the page that we were on the register page in this case and it sends those errors back to so that we can use them within that page view to display them to the user so I'm going to head to that register page view and I'm going to scroll right to the bottom of the form and I'm going to paste in a little snippet of code now if you've taken my Lal beginners course this should look very familiar because we did something very similar what we're doing is a little of check to see if we have any errors and if we do then we output a UL tag with a few Tailwind classes so when I said that the validation method sends back some errors into the view if there's any errors present then this is going to be true right here we'll have errors okay so inside here we Loop through all those validation errors that we have and we output each one as its own Li tag so now if say the passwords don't match for example or the email Isn't unique then we're going to list those ER provided by LL right here at the bottom of the form all right so I'm on the register page and we're going to try registering a new account so I'm going to sign up with Yoshi the email is Yoshi net Ninja dodev for the password I'm going to say pw12 3 and then down here PW now I wanted to do this to show you that we are going to get validation errors and we'll see those at the bottom so the first one is that the password isn't 8 characters long and secondly they don't match let's try then pw1 2 3 45 6 which is eight characters pw1 2 3 4 56 and then register hopefully we will get redirected to the homepage now and this will work awesome okay and then just back over here I wanted to open up the database so let's go and have a look at that just so we can see that new user and in fact I created two users one off screen as well so we can see now we have this one Mario and also Yosi awesome and we can see that these passwords are hashed cool

Original Description

🔥🔥 Get (early) access to the entire Net Ninja Pro library, including exclusive courses and masterclasses, here - https://netninja.dev/p/net-ninja-pro/ 📂🥷🏼 Access the course files on GitHub: https://github.com/iamshaunjp/laravel-auth 📂🥷🏼 Starter project on GitHub: https://github.com/iamshaunjp/laravel-auth/tree/starter-project 🧠🥷🏼 Laravel 11 for Beginners: https://www.youtube.com/watch?v=DKnn8TlJ4MA&list=PL4cUxeGkcC9gF5Gez17eHcDIxrpVSBuVt 🧠🥷🏼 PHP & MySQL Crash Course: https://www.youtube.com/watch?v=pWG7ajC_OVo&list=PL4cUxeGkcC9gksOX3Kd9KPo-O68ncT05o 🔗🐄 Download & install Herd: https://herd.laravel.com/ 🔗👇 Laravel docs: https://laravel.com/docs/11.x/readme
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

This video tutorial teaches how to register new users in a Laravel application, covering validation, password hashing, and session management. It provides a step-by-step guide on how to implement these features using Laravel's authentication system and Tailwind for styling validation errors.

Key Takeaways
  1. Create a new variable to hold the validated user input
  2. Use the validate method on the request object to validate the user input based on specified rules
  3. Create a new user using the User model and the create method
  4. Log the user in using Laravel's authentication system
  5. Generate a session token and cookie for the user
  6. Redirect the user back to the index page after successful registration
  7. Loop through validation errors and output each one as a Li tag
  8. Register a new account to demonstrate validation errors
  9. Open the database to view the new user
💡 Laravel's authentication system provides a convenient and secure way to handle user registration and login, including password hashing and session management.

Related Reads

Up next
Indian Express Editorial Analysis by Chandan Sharma - 1 JULY 2026 | UPSC Current Affairs 2026
StudyIQ IAS
Watch →